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

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

Introduction

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

Prototype

int IsLocalType

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

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   w ww .  j  a  v  a2 s  .c o  m
            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:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitClassDecl(final lombok.ast.ClassDecl node, final Void p) {
    final TypeDeclaration typeDeclaration = new TypeDeclaration(
            ((CompilationUnitDeclaration) sourceNode.top().get()).compilationResult);
    setGeneratedByAndCopyPos(typeDeclaration, source, posHintOf(node));
    typeDeclaration.modifiers = modifiersFor(node.getModifiers());
    if (node.isInterface())
        typeDeclaration.modifiers |= AccInterface;
    typeDeclaration.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    if (node.isLocal())
        typeDeclaration.bits |= ASTNode.IsLocalType;
    if (node.isAnonymous()) {
        typeDeclaration.bits |= ASTNode.IsAnonymousType;
    }/*from w  w  w. j  a  va2 s .  c om*/
    if (Is.empty(node.getName())) {
        typeDeclaration.name = CharOperation.NO_CHAR;
    } else {
        typeDeclaration.name = node.getName().toCharArray();
    }
    typeDeclaration.annotations = toArray(build(node.getAnnotations()), new Annotation[0]);
    typeDeclaration.typeParameters = toArray(build(node.getTypeParameters()), new TypeParameter[0]);
    typeDeclaration.fields = toArray(build(node.getFields()), new FieldDeclaration[0]);
    typeDeclaration.methods = toArray(build(node.getMethods()), new AbstractMethodDeclaration[0]);
    typeDeclaration.memberTypes = toArray(build(node.getMemberTypes()), new TypeDeclaration[0]);
    typeDeclaration.superInterfaces = toArray(build(node.getSuperInterfaces()), new TypeReference[0]);
    typeDeclaration.superclass = build(node.getSuperclass());
    for (final FieldDeclaration field : Each.elementIn(typeDeclaration.fields)) {
        if (isEnumConstant(field) || (field.modifiers & Modifier.STATIC) != 0) {
            typeDeclaration.addClinit();
            break;
        }
    }
    return typeDeclaration;
}

From source file:lombok.eclipse.handlers.HandleRelations.java

License:Open Source License

private static FieldDeclaration createField(Object anno, EclipseNode fieldNode, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    FieldDeclaration result = null;/*  w w w  . j ava 2 s  .  c  o m*/
    FieldDeclaration fieldDecl = (FieldDeclaration) fieldNode.get();

    String relatedFieldName = null;
    boolean isOneToOne = false;
    boolean isUnique = false;

    String baseTypeName = new String(((TypeDeclaration) fieldNode.up().get()).name);

    char[] qualifiedRelationTypeName = null;
    char[] singleRelationTypeName = null;
    TypeReference fieldType = null;
    TypeReference baseType = createTypeReference(baseTypeName.split("\\."), p);
    setGeneratedBy(baseType, source);
    TypeReference referenceType = fieldDecl.type;

    if (anno instanceof OneToOne) {
        isOneToOne = true;
        relatedFieldName = ((OneToOne) anno).field();

        qualifiedRelationTypeName = OneToOneRelation.class.getName().toCharArray();
        singleRelationTypeName = OneToOneRelation.class.getSimpleName().toCharArray();
    } else {
        relatedFieldName = ((OneToMany) anno).field();
        isUnique = ((OneToMany) anno).unique();

        if (referenceType instanceof ParameterizedSingleTypeReference) {
            referenceType = ((ParameterizedSingleTypeReference) referenceType).typeArguments[0];
        } else if (referenceType instanceof ParameterizedQualifiedTypeReference) {
            ParameterizedQualifiedTypeReference type = (ParameterizedQualifiedTypeReference) referenceType;
            referenceType = type.typeArguments[type.typeArguments.length - 1][0];
        }

        qualifiedRelationTypeName = OneToManyRelation.class.getName().toCharArray();
        singleRelationTypeName = OneToManyRelation.class.getSimpleName().toCharArray();
    }

    addImportIfNotExists((CompilationUnitDeclaration) fieldNode.top().get(), qualifiedRelationTypeName, source);

    fieldType = new ParameterizedSingleTypeReference(singleRelationTypeName,
            new TypeReference[] { baseType, referenceType }, 0, p);
    setGeneratedBy(fieldType, source);
    fieldType.sourceStart = pS;
    fieldType.sourceEnd = fieldType.statementEnd = pE;

    CompilationResult compResult = ((CompilationUnitDeclaration) fieldNode.top().get()).compilationResult;

    final TypeDeclaration typeDeclaration = new TypeDeclaration(compResult);
    setGeneratedBy(typeDeclaration, source);
    typeDeclaration.name = CharOperation.NO_CHAR;
    typeDeclaration.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
    typeDeclaration.bodyStart = source.sourceStart;
    typeDeclaration.bodyEnd = source.sourceEnd;
    typeDeclaration.declarationSourceStart = source.sourceStart;
    typeDeclaration.declarationSourceEnd = source.sourceEnd;
    typeDeclaration.methods = new AbstractMethodDeclaration[] {
            createGetReferencedKeyMethod(source, relatedFieldName, isOneToOne, baseType, referenceType,
                    compResult),
            createSetReferencedObjectMethod(fieldDecl, source, relatedFieldName, isOneToOne, isUnique, baseType,
                    referenceType, (CompilationUnitDeclaration) fieldNode.top().get()),
            createSetRelatedIdMethod(source, relatedFieldName, isOneToOne, baseType, referenceType,
                    compResult) };

    typeDeclaration.addClinit();

    QualifiedAllocationExpression allocation = new QualifiedAllocationExpression(typeDeclaration);
    setGeneratedBy(allocation, source);
    allocation.sourceStart = pS;
    allocation.sourceEnd = allocation.statementEnd = pE;
    allocation.type = fieldType;

    result = new FieldDeclaration(toUpperCase(new String(fieldDecl.name)).toCharArray(), 0, -1);
    setGeneratedBy(result, source);
    result.declarationSourceEnd = -1;
    result.type = fieldType;
    result.initialization = allocation;
    result.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccFinal
            | ClassFileConstants.AccStatic;

    return result;
}

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

License:Open Source License

protected void consumeAllocationHeader() {
    // ClassInstanceCreationExpression ::= 'new' ClassType '(' ArgumentListopt ')' ClassBodyopt

    // ClassBodyopt produces a null item on the astStak if it produces NO class body
    // An empty class body produces a 0 on the length stack.....

    if (this.currentElement == null) {
        return; // should never occur, this consumeRule is only used in recovery mode
    }/*w  ww  .ja v a  2  s  .c o  m*/
    if (this.currentToken == TokenNameLBRACE) {
        // beginning of an anonymous type
        TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult);
        anonymousType.name = CharOperation.NO_CHAR;
        anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        anonymousType.sourceStart = this.intStack[this.intPtr--];
        anonymousType.declarationSourceStart = anonymousType.sourceStart;
        anonymousType.sourceEnd = this.rParenPos; // closing parenthesis
        QualifiedAllocationExpression alloc = new QualifiedAllocationExpression(anonymousType);
        alloc.type = getTypeReference(0);
        alloc.sourceStart = anonymousType.sourceStart;
        alloc.sourceEnd = anonymousType.sourceEnd;
        this.lastCheckPoint = anonymousType.bodyStart = this.scanner.currentPosition;
        this.currentElement = this.currentElement.add(anonymousType, 0);
        this.lastIgnoredToken = -1;
        this.currentToken = 0; // opening brace already taken into account
        return;
    }
    this.lastCheckPoint = this.scanner.startPosition; // force to restart at this exact position
    this.restartRecovery = true; // request to restart from here on
}

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

License:Open Source License

protected void consumeAnnotationTypeDeclarationHeaderName() {
    // consumeAnnotationTypeDeclarationHeader ::= Modifiers '@' PushModifiers interface Identifier
    // consumeAnnotationTypeDeclarationHeader ::= '@' PushModifiers interface Identifier
    TypeDeclaration annotationTypeDeclaration = new TypeDeclaration(this.compilationUnit.compilationResult);
    if (this.nestedMethod[this.nestedType] == 0) {
        if (this.nestedType != 0) {
            annotationTypeDeclaration.bits |= ASTNode.IsMemberType;
        }/*w ww.ja va 2 s  .c o m*/
    } else {
        // Record that the block has a declaration for local types
        annotationTypeDeclaration.bits |= ASTNode.IsLocalType;
        markEnclosingMemberWithLocalType();
        blockReal();
    }

    //highlight the name of the type
    long pos = this.identifierPositionStack[this.identifierPtr];
    annotationTypeDeclaration.sourceEnd = (int) pos;
    annotationTypeDeclaration.sourceStart = (int) (pos >>> 32);
    annotationTypeDeclaration.name = this.identifierStack[this.identifierPtr--];
    this.identifierLengthPtr--;

    //compute the declaration source too
    // 'interface' push two int positions: the beginning of the class token and its end.
    // we want to keep the beginning position but get rid of the end position
    // it is only used for the ClassLiteralAccess positions.
    this.intPtr--; // remove the start position of the interface token
    this.intPtr--; // remove the end position of the interface token

    annotationTypeDeclaration.modifiersSourceStart = this.intStack[this.intPtr--];
    annotationTypeDeclaration.modifiers = this.intStack[this.intPtr--] | ClassFileConstants.AccAnnotation
            | ClassFileConstants.AccInterface;
    if (annotationTypeDeclaration.modifiersSourceStart >= 0) {
        annotationTypeDeclaration.declarationSourceStart = annotationTypeDeclaration.modifiersSourceStart;
        this.intPtr--; // remove the position of the '@' token as we have modifiers
    } else {
        int atPosition = this.intStack[this.intPtr--];
        // remove the position of the '@' token as we don't have modifiers
        annotationTypeDeclaration.declarationSourceStart = atPosition;
    }

    // Store secondary info
    if ((annotationTypeDeclaration.bits & ASTNode.IsMemberType) == 0
            && (annotationTypeDeclaration.bits & ASTNode.IsLocalType) == 0) {
        if (this.compilationUnit != null && !CharOperation.equals(annotationTypeDeclaration.name,
                this.compilationUnit.getMainTypeName())) {
            annotationTypeDeclaration.bits |= ASTNode.IsSecondaryType;
        }
    }

    // consume annotations
    int length;
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1,
                annotationTypeDeclaration.annotations = new Annotation[length], 0, length);
    }
    annotationTypeDeclaration.bodyStart = annotationTypeDeclaration.sourceEnd + 1;

    // javadoc
    annotationTypeDeclaration.javadoc = this.javadoc;
    this.javadoc = null;
    pushOnAstStack(annotationTypeDeclaration);
    if (!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        problemReporter().invalidUsageOfAnnotationDeclarations(annotationTypeDeclaration);
    }

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = annotationTypeDeclaration.bodyStart;
        this.currentElement = this.currentElement.add(annotationTypeDeclaration, 0);
        this.lastIgnoredToken = -1;
    }
}

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

License:Open Source License

protected void consumeAnnotationTypeDeclarationHeaderNameWithTypeParameters() {
    // consumeAnnotationTypeDeclarationHeader ::= Modifiers '@' PushModifiers interface Identifier TypeParameters
    // consumeAnnotationTypeDeclarationHeader ::= '@' PushModifiers interface Identifier TypeParameters
    TypeDeclaration annotationTypeDeclaration = new TypeDeclaration(this.compilationUnit.compilationResult);
    // consume type parameters
    int length = this.genericsLengthStack[this.genericsLengthPtr--];
    this.genericsPtr -= length;
    System.arraycopy(this.genericsStack, this.genericsPtr + 1,
            annotationTypeDeclaration.typeParameters = new TypeParameter[length], 0, length);

    problemReporter().invalidUsageOfTypeParametersForAnnotationDeclaration(annotationTypeDeclaration);

    annotationTypeDeclaration.bodyStart = annotationTypeDeclaration.typeParameters[length
            - 1].declarationSourceEnd + 1;

    //   annotationTypeDeclaration.typeParameters = null;

    this.listTypeParameterLength = 0;

    if (this.nestedMethod[this.nestedType] == 0) {
        if (this.nestedType != 0) {
            annotationTypeDeclaration.bits |= ASTNode.IsMemberType;
        }/*w ww . ja va 2  s.  c o  m*/
    } else {
        // Record that the block has a declaration for local types
        annotationTypeDeclaration.bits |= ASTNode.IsLocalType;
        markEnclosingMemberWithLocalType();
        blockReal();
    }

    //highlight the name of the type
    long pos = this.identifierPositionStack[this.identifierPtr];
    annotationTypeDeclaration.sourceEnd = (int) pos;
    annotationTypeDeclaration.sourceStart = (int) (pos >>> 32);
    annotationTypeDeclaration.name = this.identifierStack[this.identifierPtr--];
    this.identifierLengthPtr--;

    //compute the declaration source too
    // 'interface' push two int positions: the beginning of the class token and its end.
    // we want to keep the beginning position but get rid of the end position
    // it is only used for the ClassLiteralAccess positions.
    this.intPtr--; // remove the start position of the interface token
    this.intPtr--; // remove the end position of the interface token

    annotationTypeDeclaration.modifiersSourceStart = this.intStack[this.intPtr--];
    annotationTypeDeclaration.modifiers = this.intStack[this.intPtr--] | ClassFileConstants.AccAnnotation
            | ClassFileConstants.AccInterface;
    if (annotationTypeDeclaration.modifiersSourceStart >= 0) {
        annotationTypeDeclaration.declarationSourceStart = annotationTypeDeclaration.modifiersSourceStart;
        this.intPtr--; // remove the position of the '@' token as we have modifiers
    } else {
        int atPosition = this.intStack[this.intPtr--];
        // remove the position of the '@' token as we don't have modifiers
        annotationTypeDeclaration.declarationSourceStart = atPosition;
    }

    // Store secondary info
    if ((annotationTypeDeclaration.bits & ASTNode.IsMemberType) == 0
            && (annotationTypeDeclaration.bits & ASTNode.IsLocalType) == 0) {
        if (this.compilationUnit != null && !CharOperation.equals(annotationTypeDeclaration.name,
                this.compilationUnit.getMainTypeName())) {
            annotationTypeDeclaration.bits |= ASTNode.IsSecondaryType;
        }
    }

    // consume annotations
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1,
                annotationTypeDeclaration.annotations = new Annotation[length], 0, length);
    }
    // javadoc
    annotationTypeDeclaration.javadoc = this.javadoc;
    this.javadoc = null;
    pushOnAstStack(annotationTypeDeclaration);
    if (!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        problemReporter().invalidUsageOfAnnotationDeclarations(annotationTypeDeclaration);
    }

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = annotationTypeDeclaration.bodyStart;
        this.currentElement = this.currentElement.add(annotationTypeDeclaration, 0);
        this.lastIgnoredToken = -1;
    }
}

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

License:Open Source License

protected void consumeClassHeaderName1() {
    // ClassHeaderName1 ::= Modifiersopt 'class' 'Identifier'
    TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
    if (this.nestedMethod[this.nestedType] == 0) {
        if (this.nestedType != 0) {
            typeDecl.bits |= ASTNode.IsMemberType;
        }/*from   w  w w  .  java2 s .  co m*/
    } else {
        // Record that the block has a declaration for local types
        typeDecl.bits |= ASTNode.IsLocalType;
        markEnclosingMemberWithLocalType();
        blockReal();
    }

    //highlight the name of the type
    long pos = this.identifierPositionStack[this.identifierPtr];
    typeDecl.sourceEnd = (int) pos;
    typeDecl.sourceStart = (int) (pos >>> 32);
    typeDecl.name = this.identifierStack[this.identifierPtr--];
    this.identifierLengthPtr--;

    //compute the declaration source too
    // 'class' and 'interface' push two int positions: the beginning of the class token and its end.
    // we want to keep the beginning position but get rid of the end position
    // it is only used for the ClassLiteralAccess positions.
    typeDecl.declarationSourceStart = this.intStack[this.intPtr--];
    this.intPtr--; // remove the end position of the class token

    typeDecl.modifiersSourceStart = this.intStack[this.intPtr--];
    typeDecl.modifiers = this.intStack[this.intPtr--];
    if (typeDecl.modifiersSourceStart >= 0) {
        typeDecl.declarationSourceStart = typeDecl.modifiersSourceStart;
    }

    // Store secondary info
    if ((typeDecl.bits & ASTNode.IsMemberType) == 0 && (typeDecl.bits & ASTNode.IsLocalType) == 0) {
        if (this.compilationUnit != null
                && !CharOperation.equals(typeDecl.name, this.compilationUnit.getMainTypeName())) {
            typeDecl.bits |= ASTNode.IsSecondaryType;
        }
    }

    // consume annotations
    int length;
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1,
                typeDecl.annotations = new Annotation[length], 0, length);
    }
    typeDecl.bodyStart = typeDecl.sourceEnd + 1;
    pushOnAstStack(typeDecl);

    this.listLength = 0; // will be updated when reading super-interfaces
    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = typeDecl.bodyStart;
        this.currentElement = this.currentElement.add(typeDecl, 0);
        this.lastIgnoredToken = -1;
    }
    // javadoc
    typeDecl.javadoc = this.javadoc;
    this.javadoc = null;
}

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

License:Open Source License

protected void consumeEnterAnonymousClassBody(boolean qualified) {
    // EnterAnonymousClassBody ::= $empty
    TypeReference typeReference = getTypeReference(0);

    TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult);
    anonymousType.name = CharOperation.NO_CHAR;
    anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
    QualifiedAllocationExpression alloc = new QualifiedAllocationExpression(anonymousType);
    markEnclosingMemberWithLocalType();/*from www  .  j  av  a 2s . co  m*/
    pushOnAstStack(anonymousType);

    alloc.sourceEnd = this.rParenPos; //the position has been stored explicitly
    int argumentLength;
    if ((argumentLength = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        this.expressionPtr -= argumentLength;
        System.arraycopy(this.expressionStack, this.expressionPtr + 1,
                alloc.arguments = new Expression[argumentLength], 0, argumentLength);
    }

    if (qualified) {
        this.expressionLengthPtr--;
        alloc.enclosingInstance = this.expressionStack[this.expressionPtr--];
    }

    alloc.type = typeReference;

    anonymousType.sourceEnd = alloc.sourceEnd;
    //position at the type while it impacts the anonymous declaration
    anonymousType.sourceStart = anonymousType.declarationSourceStart = alloc.type.sourceStart;
    alloc.sourceStart = this.intStack[this.intPtr--];
    pushOnExpressionStack(alloc);

    anonymousType.bodyStart = this.scanner.currentPosition;
    this.listLength = 0; // will be updated when reading super-interfaces

    // flush the comments related to the anonymous
    this.scanner.commentPtr = -1;

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = anonymousType.bodyStart;
        this.currentElement = this.currentElement.add(anonymousType, 0);
        if (!(this.currentElement instanceof RecoveredAnnotation)) {
            this.currentToken = 0; // opening brace already taken into account
        } else {
            this.ignoreNextOpeningBrace = true;
            this.currentElement.bracketBalance++;
        }
        this.lastIgnoredToken = -1;
    }
}

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

License:Open Source License

protected void consumeEnumConstantHeader() {
    FieldDeclaration enumConstant = (FieldDeclaration) this.astStack[this.astPtr];
    boolean foundOpeningBrace = this.currentToken == TokenNameLBRACE;
    if (foundOpeningBrace) {
        // qualified allocation expression
        TypeDeclaration anonymousType = new TypeDeclaration(this.compilationUnit.compilationResult);
        anonymousType.name = CharOperation.NO_CHAR;
        anonymousType.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        final int start = this.scanner.startPosition;
        anonymousType.declarationSourceStart = start;
        anonymousType.sourceStart = start;
        anonymousType.sourceEnd = start; // closing parenthesis
        anonymousType.modifiers = 0;//from ww w . j av  a2  s . c  om
        anonymousType.bodyStart = this.scanner.currentPosition;
        markEnclosingMemberWithLocalType();
        consumeNestedType();
        this.variablesCounter[this.nestedType]++;
        pushOnAstStack(anonymousType);
        QualifiedAllocationExpression allocationExpression = new QualifiedAllocationExpression(anonymousType);
        allocationExpression.enumConstant = enumConstant;

        // fill arguments if needed
        int length;
        if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
            this.expressionPtr -= length;
            System.arraycopy(this.expressionStack, this.expressionPtr + 1,
                    allocationExpression.arguments = new Expression[length], 0, length);
        }
        enumConstant.initialization = allocationExpression;
    } else {
        AllocationExpression allocationExpression = new AllocationExpression();
        allocationExpression.enumConstant = enumConstant;
        // fill arguments if needed
        int length;
        if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
            this.expressionPtr -= length;
            System.arraycopy(this.expressionStack, this.expressionPtr + 1,
                    allocationExpression.arguments = new Expression[length], 0, length);
        }
        enumConstant.initialization = allocationExpression;
    }
    // initialize the starting position of the allocation expression
    enumConstant.initialization.sourceStart = enumConstant.declarationSourceStart;

    // recovery
    if (this.currentElement != null) {
        if (foundOpeningBrace) {
            TypeDeclaration anonymousType = (TypeDeclaration) this.astStack[this.astPtr];
            this.currentElement = this.currentElement.add(anonymousType, 0);
            this.lastCheckPoint = anonymousType.bodyStart;
            this.lastIgnoredToken = -1;
            this.currentToken = 0; // opening brace already taken into account
        } else {
            if (this.currentToken == TokenNameSEMICOLON) {
                RecoveredType currentType = currentRecoveryType();
                if (currentType != null) {
                    currentType.insideEnumConstantPart = false;
                }
            }
            this.lastCheckPoint = this.scanner.startPosition; // force to restart at this exact position
            this.lastIgnoredToken = -1;
            this.restartRecovery = true;
        }
    }
}

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

License:Open Source License

protected void consumeEnumHeaderName() {
    // EnumHeaderName ::= Modifiersopt 'enum' Identifier
    TypeDeclaration enumDeclaration = new TypeDeclaration(this.compilationUnit.compilationResult);
    if (this.nestedMethod[this.nestedType] == 0) {
        if (this.nestedType != 0) {
            enumDeclaration.bits |= ASTNode.IsMemberType;
        }/*from  w  ww. j av a  2  s .co m*/
    } else {
        // Record that the block has a declaration for local types
        //      markEnclosingMemberWithLocalType();
        blockReal();
    }
    //highlight the name of the type
    long pos = this.identifierPositionStack[this.identifierPtr];
    enumDeclaration.sourceEnd = (int) pos;
    enumDeclaration.sourceStart = (int) (pos >>> 32);
    enumDeclaration.name = this.identifierStack[this.identifierPtr--];
    this.identifierLengthPtr--;

    //compute the declaration source too
    // 'class' and 'interface' push two int positions: the beginning of the class token and its end.
    // we want to keep the beginning position but get rid of the end position
    // it is only used for the ClassLiteralAccess positions.
    enumDeclaration.declarationSourceStart = this.intStack[this.intPtr--];
    this.intPtr--; // remove the end position of the class token

    enumDeclaration.modifiersSourceStart = this.intStack[this.intPtr--];
    enumDeclaration.modifiers = this.intStack[this.intPtr--] | ClassFileConstants.AccEnum;
    if (enumDeclaration.modifiersSourceStart >= 0) {
        enumDeclaration.declarationSourceStart = enumDeclaration.modifiersSourceStart;
    }

    // Store secondary info
    if ((enumDeclaration.bits & ASTNode.IsMemberType) == 0
            && (enumDeclaration.bits & ASTNode.IsLocalType) == 0) {
        if (this.compilationUnit != null
                && !CharOperation.equals(enumDeclaration.name, this.compilationUnit.getMainTypeName())) {
            enumDeclaration.bits |= ASTNode.IsSecondaryType;
        }
    }

    // consume annotations
    int length;
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1,
                enumDeclaration.annotations = new Annotation[length], 0, length);
    }
    //   if (this.currentToken == TokenNameLBRACE) {
    //      enumDeclaration.bodyStart = this.scanner.currentPosition;
    //   }
    enumDeclaration.bodyStart = enumDeclaration.sourceEnd + 1;
    pushOnAstStack(enumDeclaration);

    this.listLength = 0; // will be updated when reading super-interfaces

    if (!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        //TODO this code will be never run while 'enum' is an identifier in 1.3 scanner
        problemReporter().invalidUsageOfEnumDeclarations(enumDeclaration);
    }

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = enumDeclaration.bodyStart;
        this.currentElement = this.currentElement.add(enumDeclaration, 0);
        this.lastIgnoredToken = -1;
    }
    // javadoc
    enumDeclaration.javadoc = this.javadoc;
    this.javadoc = null;
}