Example usage for org.eclipse.jdt.internal.compiler.ast ASTNode HasAbstractMethods

List of usage examples for org.eclipse.jdt.internal.compiler.ast ASTNode HasAbstractMethods

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast ASTNode HasAbstractMethods.

Prototype

int HasAbstractMethods

To view the source code for org.eclipse.jdt.internal.compiler.ast ASTNode HasAbstractMethods.

Click Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult)
        throws JavaModelException {
    SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
    if (typeInfo.isAnonymousMember())
        throw new AnonymousMemberFound();
    /* create type declaration - can be member type */
    TypeDeclaration type = new TypeDeclaration(compilationResult);
    if (typeInfo.getEnclosingType() == null) {
        if (typeHandle.isAnonymous()) {
            type.name = CharOperation.NO_CHAR;
            type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        } else {//from ww w. j  a  v a  2 s . c om
            if (typeHandle.isLocal()) {
                type.bits |= ASTNode.IsLocalType;
            }
        }
    } else {
        type.bits |= ASTNode.IsMemberType;
    }
    if ((type.bits & ASTNode.IsAnonymousType) == 0) {
        type.name = typeInfo.getName();
    }
    type.name = typeInfo.getName();
    int start, end; // only positions available
    type.sourceStart = start = typeInfo.getNameSourceStart();
    type.sourceEnd = end = typeInfo.getNameSourceEnd();
    type.modifiers = typeInfo.getModifiers();
    type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
    type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
    type.bodyEnd = type.declarationSourceEnd;

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        type.annotations = convertAnnotations(typeHandle);
    }
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
       must internalize type variables and observe any parameterization of super class
       and/or super interfaces in order to be able to detect overriding in the presence
       of generics.
     */
    char[][] typeParameterNames = typeInfo.getTypeParameterNames();
    if (typeParameterNames.length > 0) {
        int parameterCount = typeParameterNames.length;
        char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
        type.typeParameters = new TypeParameter[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start,
                    end);
        }
    }

    /* set superclass and superinterfaces */
    if (typeInfo.getSuperclassName() != null) {
        type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end,
                true /* include generics */);
        type.superclass.bits |= ASTNode.IsSuperType;
    }
    char[][] interfaceNames = typeInfo.getInterfaceNames();
    int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
    if (interfaceCount > 0) {
        type.superInterfaces = new TypeReference[interfaceCount];
        for (int i = 0; i < interfaceCount; i++) {
            type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end,
                    true /* include generics */);
            type.superInterfaces[i].bits |= ASTNode.IsSuperType;
        }
    }
    /* convert member types */
    if ((this.flags & MEMBER_TYPE) != 0) {
        SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
        int sourceMemberTypeCount = sourceMemberTypes.length;
        type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
        for (int i = 0; i < sourceMemberTypeCount; i++) {
            type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
            type.memberTypes[i].enclosingType = type;
        }
    }

    /* convert intializers and fields*/
    InitializerElementInfo[] initializers = null;
    int initializerCount = 0;
    if ((this.flags & LOCAL_TYPE) != 0) {
        initializers = typeInfo.getInitializers();
        initializerCount = initializers.length;
    }
    SourceField[] sourceFields = null;
    int sourceFieldCount = 0;
    if ((this.flags & FIELD) != 0) {
        sourceFields = typeInfo.getFieldHandles();
        sourceFieldCount = sourceFields.length;
    }
    int length = initializerCount + sourceFieldCount;
    if (length > 0) {
        type.fields = new FieldDeclaration[length];
        for (int i = 0; i < initializerCount; i++) {
            type.fields[i] = convert(initializers[i], compilationResult);
        }
        int index = 0;
        for (int i = initializerCount; i < length; i++) {
            type.fields[i] = convert(sourceFields[index++], type, compilationResult);
        }
    }

    /* convert methods - need to add default constructor if necessary */
    boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
    boolean needMethod = (this.flags & METHOD) != 0;
    if (needConstructor || needMethod) {

        SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
        int sourceMethodCount = sourceMethods.length;

        /* source type has a constructor ?           */
        /* by default, we assume that one is needed. */
        int extraConstructor = 0;
        int methodCount = 0;
        int kind = TypeDeclaration.kind(type.modifiers);
        boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL
                || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
        if (!isAbstract) {
            extraConstructor = needConstructor ? 1 : 0;
            for (int i = 0; i < sourceMethodCount; i++) {
                if (sourceMethods[i].isConstructor()) {
                    if (needConstructor) {
                        extraConstructor = 0; // Does not need the extra constructor since one constructor already exists.
                        methodCount++;
                    }
                } else if (needMethod) {
                    methodCount++;
                }
            }
        } else {
            methodCount = needMethod ? sourceMethodCount : 0;
        }
        type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
        if (extraConstructor != 0) { // add default constructor in first position
            type.methods[0] = type.createDefaultConstructor(false, false);
        }
        int index = 0;
        boolean hasAbstractMethods = false;
        for (int i = 0; i < sourceMethodCount; i++) {
            SourceMethod sourceMethod = sourceMethods[i];
            SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo();
            boolean isConstructor = methodInfo.isConstructor();
            if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
                hasAbstractMethods = true;
            }
            if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
                AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
                if (isAbstract || method.isAbstract()) { // fix-up flag
                    method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
                }
                type.methods[extraConstructor + index++] = method;
            }
        }
        if (hasAbstractMethods)
            type.bits |= ASTNode.HasAbstractMethods;
    }

    return type;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

private TypeDeclaration convert(IType type, IType alreadyComputedMember,
        TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException {
    /* create type declaration - can be member type */
    TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult);

    if (type.getDeclaringType() != null) {
        typeDeclaration.bits |= ASTNode.IsMemberType;
    }/*w  w  w . ja  va 2s .com*/
    typeDeclaration.name = type.getElementName().toCharArray();
    typeDeclaration.modifiers = type.getFlags();

    /* set superclass and superinterfaces */
    if (type.getSuperclassName() != null) {
        TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature());
        if (typeReference != null) {
            typeDeclaration.superclass = typeReference;
            typeDeclaration.superclass.bits |= ASTNode.IsSuperType;
        }
    }

    String[] interfaceTypes = type.getSuperInterfaceTypeSignatures();
    int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length;
    typeDeclaration.superInterfaces = new TypeReference[interfaceCount];
    int count = 0;
    for (int i = 0; i < interfaceCount; i++) {
        TypeReference typeReference = createTypeReference(interfaceTypes[i]);
        if (typeReference != null) {
            typeDeclaration.superInterfaces[count] = typeReference;
            typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType;
        }
    }
    if (count != interfaceCount) {
        System.arraycopy(typeDeclaration.fields, 0,
                typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {

        /* convert type parameters */
        ITypeParameter[] typeParameters = type.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }

            typeDeclaration.typeParameters = typeParams;
        }
    }

    /* convert member types */
    IType[] memberTypes = type.getTypes();
    int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
    typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount];
    for (int i = 0; i < memberTypeCount; i++) {
        if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName()
                .equals(memberTypes[i].getFullyQualifiedName())) {
            typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration;
        } else {
            typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null);
        }
        typeDeclaration.memberTypes[i].enclosingType = typeDeclaration;
    }

    /* convert fields */
    IField[] fields = type.getFields();
    int fieldCount = fields == null ? 0 : fields.length;
    typeDeclaration.fields = new FieldDeclaration[fieldCount];
    count = 0;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration fieldDeclaration = convert(fields[i], type);
        if (fieldDeclaration != null) {
            typeDeclaration.fields[count++] = fieldDeclaration;
        }
    }
    if (count != fieldCount) {
        System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0,
                count);
    }

    /* convert methods - need to add default constructor if necessary */
    IMethod[] methods = type.getMethods();
    int methodCount = methods == null ? 0 : methods.length;

    /* source type has a constructor ?           */
    /* by default, we assume that one is needed. */
    int neededCount = 1;
    for (int i = 0; i < methodCount; i++) {
        if (methods[i].isConstructor()) {
            neededCount = 0;
            // Does not need the extra constructor since one constructor already exists.
            break;
        }
    }
    boolean isInterface = type.isInterface();
    neededCount = isInterface ? 0 : neededCount;
    typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount];
    if (neededCount != 0) { // add default constructor in first position
        typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false);
    }
    boolean hasAbstractMethods = false;
    count = 0;
    for (int i = 0; i < methodCount; i++) {
        AbstractMethodDeclaration method = convert(methods[i], type);
        if (method != null) {
            boolean isAbstract;
            if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag
                method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
            }
            if (isAbstract) {
                hasAbstractMethods = true;
            }
            typeDeclaration.methods[neededCount + (count++)] = method;
        }
    }
    if (count != methodCount) {
        System.arraycopy(typeDeclaration.methods, 0,
                typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0,
                count + neededCount);
    }
    if (hasAbstractMethods) {
        typeDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    return typeDeclaration;
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void dispatchDeclarationInto(int length) {
    /* they are length on this.astStack that should go into
       methods fields constructors lists of the typeDecl
            //  w  w w.ja  v  a 2  s .c o m
       Return if there is a constructor declaration in the methods declaration */

    // Looks for the size of each array .

    if (length == 0)
        return;
    int[] flag = new int[length + 1]; //plus one -- see <HERE>
    int size1 = 0, size2 = 0, size3 = 0;
    boolean hasAbstractMethods = false;
    for (int i = length - 1; i >= 0; i--) {
        ASTNode astNode = this.astStack[this.astPtr--];
        if (astNode instanceof AbstractMethodDeclaration) {
            //methods and constructors have been regrouped into one single list
            flag[i] = 2;
            size2++;
            if (((AbstractMethodDeclaration) astNode).isAbstract()) {
                hasAbstractMethods = true;
            }
        } else if (astNode instanceof TypeDeclaration) {
            flag[i] = 3;
            size3++;
        } else {
            //field
            flag[i] = 1;
            size1++;
        }
    }

    //arrays creation
    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];
    if (size1 != 0) {
        typeDecl.fields = new FieldDeclaration[size1];
    }
    if (size2 != 0) {
        typeDecl.methods = new AbstractMethodDeclaration[size2];
        if (hasAbstractMethods)
            typeDecl.bits |= ASTNode.HasAbstractMethods;
    }
    if (size3 != 0) {
        typeDecl.memberTypes = new TypeDeclaration[size3];
    }

    //arrays fill up
    size1 = size2 = size3 = 0;
    int flagI = flag[0], start = 0;
    int length2;
    for (int end = 0; end <= length; end++) //<HERE> the plus one allows to
    {
        if (flagI != flag[end]) //treat the last element as a ended flag.....
        { //array copy
            switch (flagI) {
            case 1:
                size1 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, typeDecl.fields, size1 - length2,
                        length2);
                break;
            case 2:
                size2 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, typeDecl.methods, size2 - length2,
                        length2);
                break;
            case 3:
                size3 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, typeDecl.memberTypes, size3 - length2,
                        length2);
                break;
            }
            flagI = flag[start = end];
        }
    }

    if (typeDecl.memberTypes != null) {
        for (int i = typeDecl.memberTypes.length - 1; i >= 0; i--) {
            typeDecl.memberTypes[i].enclosingType = typeDecl;
        }
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void dispatchDeclarationIntoEnumDeclaration(int length) {

    if (length == 0)
        return;//from w ww .j a v a  2  s  . co m
    int[] flag = new int[length + 1]; //plus one -- see <HERE>
    int size1 = 0, size2 = 0, size3 = 0;
    TypeDeclaration enumDeclaration = (TypeDeclaration) this.astStack[this.astPtr - length];
    boolean hasAbstractMethods = false;
    int enumConstantsCounter = 0;
    for (int i = length - 1; i >= 0; i--) {
        ASTNode astNode = this.astStack[this.astPtr--];
        if (astNode instanceof AbstractMethodDeclaration) {
            //methods and constructors have been regrouped into one single list
            flag[i] = 2;
            size2++;
            if (((AbstractMethodDeclaration) astNode).isAbstract()) {
                hasAbstractMethods = true;
            }
        } else if (astNode instanceof TypeDeclaration) {
            flag[i] = 3;
            size3++;
        } else if (astNode instanceof FieldDeclaration) {
            flag[i] = 1;
            size1++;
            if (((FieldDeclaration) astNode).getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
                enumConstantsCounter++;
            }
        }
    }

    //arrays creation
    if (size1 != 0) {
        enumDeclaration.fields = new FieldDeclaration[size1];
    }
    if (size2 != 0) {
        enumDeclaration.methods = new AbstractMethodDeclaration[size2];
        if (hasAbstractMethods)
            enumDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    if (size3 != 0) {
        enumDeclaration.memberTypes = new TypeDeclaration[size3];
    }

    //arrays fill up
    size1 = size2 = size3 = 0;
    int flagI = flag[0], start = 0;
    int length2;
    for (int end = 0; end <= length; end++) //<HERE> the plus one allows to
    {
        if (flagI != flag[end]) //treat the last element as a ended flag.....
        { //array copy
            switch (flagI) {
            case 1:
                size1 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, enumDeclaration.fields,
                        size1 - length2, length2);
                break;
            case 2:
                size2 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, enumDeclaration.methods,
                        size2 - length2, length2);
                break;
            case 3:
                size3 += (length2 = end - start);
                System.arraycopy(this.astStack, this.astPtr + start + 1, enumDeclaration.memberTypes,
                        size3 - length2, length2);
                break;
            }
            flagI = flag[start = end];
        }
    }

    if (enumDeclaration.memberTypes != null) {
        for (int i = enumDeclaration.memberTypes.length - 1; i >= 0; i--) {
            enumDeclaration.memberTypes[i].enclosingType = enumDeclaration;
        }
    }
    enumDeclaration.enumConstantsCounter = enumConstantsCounter;
}