Example usage for org.eclipse.jdt.internal.compiler.ast TypeDeclaration addClinit

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeDeclaration addClinit

Introduction

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

Prototype

public final void addClinit() 

Source Link

Document

This method is responsible for adding a method declaration to the type method collections.

Usage

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  ww . j av  a 2s.co m*/
    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.EclipseHandlerUtil.java

License:Open Source License

/**
 * Inserts a field into an existing type. The type must represent a {@code TypeDeclaration}.
 *//*from   w  w  w.  ja  va2s  .c o  m*/
public static EclipseNode injectField(EclipseNode type, FieldDeclaration field) {
    TypeDeclaration parent = (TypeDeclaration) type.get();

    if (parent.fields == null) {
        parent.fields = new FieldDeclaration[1];
        parent.fields[0] = field;
    } else {
        int size = parent.fields.length;
        FieldDeclaration[] newArray = new FieldDeclaration[size + 1];
        System.arraycopy(parent.fields, 0, newArray, 0, size);
        int index = 0;
        for (; index < size; index++) {
            FieldDeclaration f = newArray[index];
            if (isEnumConstant(f) || isGenerated(f))
                continue;
            break;
        }
        System.arraycopy(newArray, index, newArray, index + 1, size - index);
        newArray[index] = field;
        parent.fields = newArray;
    }

    if (isEnumConstant(field) || (field.modifiers & Modifier.STATIC) != 0) {
        if (!hasClinit(parent)) {
            parent.addClinit();
        }
    }

    return type.add(field, Kind.FIELD);
}

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;//from w  w w .jav a2 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:lombok.eclipse.handlers.HandleUtilityClass.java

License:Open Source License

private void changeModifiersAndGenerateConstructor(EclipseNode typeNode, EclipseNode annotationNode) {
    TypeDeclaration classDecl = (TypeDeclaration) typeNode.get();

    boolean makeConstructor = true;

    classDecl.modifiers |= ClassFileConstants.AccFinal;

    boolean markStatic = true;
    boolean requiresClInit = false;
    boolean alreadyHasClinit = false;

    if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
        markStatic = false;/*  w  w w.  j  av  a  2s.  c o  m*/
    if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
        TypeDeclaration typeDecl = (TypeDeclaration) typeNode.up().get();
        if ((typeDecl.modifiers & ClassFileConstants.AccInterface) != 0)
            markStatic = false;
    }

    if (markStatic)
        classDecl.modifiers |= ClassFileConstants.AccStatic;

    for (EclipseNode element : typeNode.down()) {
        if (element.getKind() == Kind.FIELD) {
            FieldDeclaration fieldDecl = (FieldDeclaration) element.get();
            if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) == 0) {
                requiresClInit = true;
                fieldDecl.modifiers |= ClassFileConstants.AccStatic;
            }
        } else if (element.getKind() == Kind.METHOD) {
            AbstractMethodDeclaration amd = (AbstractMethodDeclaration) element.get();
            if (amd instanceof ConstructorDeclaration) {
                ConstructorDeclaration constrDecl = (ConstructorDeclaration) element.get();
                if (getGeneratedBy(constrDecl) == null
                        && (constrDecl.bits & ASTNode.IsDefaultConstructor) == 0) {
                    element.addError("@UtilityClasses cannot have declared constructors.");
                    makeConstructor = false;
                    continue;
                }
            } else if (amd instanceof MethodDeclaration) {
                amd.modifiers |= ClassFileConstants.AccStatic;
            } else if (amd instanceof Clinit) {
                alreadyHasClinit = true;
            }
        } else if (element.getKind() == Kind.TYPE) {
            ((TypeDeclaration) element.get()).modifiers |= ClassFileConstants.AccStatic;
        }
    }

    if (makeConstructor)
        createPrivateDefaultConstructor(typeNode, annotationNode);
    if (requiresClInit && !alreadyHasClinit)
        classDecl.addClinit();
}

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

License:Open Source License

protected void consumeAnnotationTypeDeclaration() {
    int length;/*from w  ww.ja  v  a2 s .c o  m*/
    if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) {
        //there are length declarations
        //dispatch according to the type of the declarations
        dispatchDeclarationInto(length);
    }

    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];

    //convert constructor that do not have the type's name into methods
    typeDecl.checkConstructors(this);

    //always add <clinit> (will be remove at code gen time if empty)
    if (this.scanner.containsAssertKeyword) {
        typeDecl.bits |= ASTNode.ContainsAssertion;
    }
    typeDecl.addClinit();
    typeDecl.bodyEnd = this.endStatementPosition;
    if (length == 0 && !containsComment(typeDecl.bodyStart, typeDecl.bodyEnd)) {
        typeDecl.bits |= ASTNode.UndocumentedEmptyBlock;
    }
    typeDecl.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition);
}

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

License:Open Source License

protected void consumeClassDeclaration() {
    // ClassDeclaration ::= ClassHeader ClassBody

    int length;//  w  ww.j  a va2  s.co m
    if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) {
        //there are length declarations
        //dispatch according to the type of the declarations
        dispatchDeclarationInto(length);
    }

    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];

    //convert constructor that do not have the type's name into methods
    boolean hasConstructor = typeDecl.checkConstructors(this);

    //add the default constructor when needed (interface don't have it)
    if (!hasConstructor) {
        switch (TypeDeclaration.kind(typeDecl.modifiers)) {
        case TypeDeclaration.CLASS_DECL:
        case TypeDeclaration.ENUM_DECL:
            boolean insideFieldInitializer = false;
            if (this.diet) {
                for (int i = this.nestedType; i > 0; i--) {
                    if (this.variablesCounter[i] > 0) {
                        insideFieldInitializer = true;
                        break;
                    }
                }
            }
            typeDecl.createDefaultConstructor(!this.diet || insideFieldInitializer, true);
        }
    }
    //always add <clinit> (will be remove at code gen time if empty)
    if (this.scanner.containsAssertKeyword) {
        typeDecl.bits |= ASTNode.ContainsAssertion;
    }
    typeDecl.addClinit();
    typeDecl.bodyEnd = this.endStatementPosition;
    if (length == 0 && !containsComment(typeDecl.bodyStart, typeDecl.bodyEnd)) {
        typeDecl.bits |= ASTNode.UndocumentedEmptyBlock;
    }

    typeDecl.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition);
}

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

License:Open Source License

protected void consumeEnumDeclaration() {
    // EnumDeclaration ::= EnumHeader ClassHeaderImplementsopt EnumBody
    int length;//from ww  w  .j a v a 2s. c  o m
    if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) {
        //there are length declarations
        //dispatch according to the type of the declarations
        dispatchDeclarationIntoEnumDeclaration(length);
    }

    TypeDeclaration enumDeclaration = (TypeDeclaration) this.astStack[this.astPtr];

    //convert constructor that do not have the type's name into methods
    boolean hasConstructor = enumDeclaration.checkConstructors(this);

    //add the default constructor when needed
    if (!hasConstructor) {
        boolean insideFieldInitializer = false;
        if (this.diet) {
            for (int i = this.nestedType; i > 0; i--) {
                if (this.variablesCounter[i] > 0) {
                    insideFieldInitializer = true;
                    break;
                }
            }
        }
        enumDeclaration.createDefaultConstructor(!this.diet || insideFieldInitializer, true);
    }

    //always add <clinit> (will be remove at code gen time if empty)
    if (this.scanner.containsAssertKeyword) {
        enumDeclaration.bits |= ASTNode.ContainsAssertion;
    }
    enumDeclaration.addClinit();
    enumDeclaration.bodyEnd = this.endStatementPosition;
    if (length == 0 && !containsComment(enumDeclaration.bodyStart, enumDeclaration.bodyEnd)) {
        enumDeclaration.bits |= ASTNode.UndocumentedEmptyBlock;
    }

    enumDeclaration.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition);
}

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

License:Open Source License

protected void consumeInterfaceDeclaration() {
    // see consumeClassDeclaration in case of changes: duplicated code
    // InterfaceDeclaration ::= InterfaceHeader InterfaceBody
    int length;/*  w w  w . ja  va2  s  .co m*/
    if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) {
        //there are length declarations
        //dispatch.....according to the type of the declarations
        dispatchDeclarationInto(length);
    }

    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];

    //convert constructor that do not have the type's name into methods
    typeDecl.checkConstructors(this);

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=212713, 
    // reject initializers that have been tolerated by the grammar.
    FieldDeclaration[] fields = typeDecl.fields;
    int fieldCount = fields == null ? 0 : fields.length;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration field = fields[i];
        if (field instanceof Initializer) {
            problemReporter().interfaceCannotHaveInitializers(typeDecl.name, field);
        }
    }

    //always add <clinit> (will be remove at code gen time if empty)
    if (this.scanner.containsAssertKeyword) {
        typeDecl.bits |= ASTNode.ContainsAssertion;
    }
    typeDecl.addClinit();
    typeDecl.bodyEnd = this.endStatementPosition;
    if (length == 0 && !containsComment(typeDecl.bodyStart, typeDecl.bodyEnd)) {
        typeDecl.bits |= ASTNode.UndocumentedEmptyBlock;
    }
    typeDecl.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition);
}