Search in sources :

Example 1 with Clinit

use of org.eclipse.jdt.internal.compiler.ast.Clinit in project lombok by rzwitserloot.

the class HandleUtilityClass method changeModifiersAndGenerateConstructor.

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;
    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();
}
Also used : Clinit(org.eclipse.jdt.internal.compiler.ast.Clinit) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 2 with Clinit

use of org.eclipse.jdt.internal.compiler.ast.Clinit in project lombok by rzwitserloot.

the class EclipseNode method traverse.

/**
	 * Visits this node and all child nodes depth-first, calling the provided visitor's visit methods.
	 */
public void traverse(EclipseASTVisitor visitor) {
    if (!this.isCompleteParse() && visitor.getClass().isAnnotationPresent(DeferUntilPostDiet.class))
        return;
    switch(getKind()) {
        case COMPILATION_UNIT:
            visitor.visitCompilationUnit(this, (CompilationUnitDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitCompilationUnit(this, (CompilationUnitDeclaration) get());
            break;
        case TYPE:
            visitor.visitType(this, (TypeDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitType(this, (TypeDeclaration) get());
            break;
        case FIELD:
            visitor.visitField(this, (FieldDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitField(this, (FieldDeclaration) get());
            break;
        case INITIALIZER:
            visitor.visitInitializer(this, (Initializer) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitInitializer(this, (Initializer) get());
            break;
        case METHOD:
            if (get() instanceof Clinit)
                return;
            visitor.visitMethod(this, (AbstractMethodDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethod(this, (AbstractMethodDeclaration) get());
            break;
        case ARGUMENT:
            AbstractMethodDeclaration method = (AbstractMethodDeclaration) up().get();
            visitor.visitMethodArgument(this, (Argument) get(), method);
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethodArgument(this, (Argument) get(), method);
            break;
        case LOCAL:
            visitor.visitLocal(this, (LocalDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitLocal(this, (LocalDeclaration) get());
            break;
        case ANNOTATION:
            switch(up().getKind()) {
                case TYPE:
                    visitor.visitAnnotationOnType((TypeDeclaration) up().get(), this, (Annotation) get());
                    break;
                case FIELD:
                    visitor.visitAnnotationOnField((FieldDeclaration) up().get(), this, (Annotation) get());
                    break;
                case METHOD:
                    visitor.visitAnnotationOnMethod((AbstractMethodDeclaration) up().get(), this, (Annotation) get());
                    break;
                case ARGUMENT:
                    visitor.visitAnnotationOnMethodArgument((Argument) parent.get(), (AbstractMethodDeclaration) parent.directUp().get(), this, (Annotation) get());
                    break;
                case LOCAL:
                    visitor.visitAnnotationOnLocal((LocalDeclaration) parent.get(), this, (Annotation) get());
                    break;
                default:
                    throw new AssertionError("Annotation not expected as child of a " + up().getKind());
            }
            break;
        case STATEMENT:
            visitor.visitStatement(this, (Statement) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitStatement(this, (Statement) get());
            break;
        default:
            throw new AssertionError("Unexpected kind during node traversal: " + getKind());
    }
}
Also used : Clinit(org.eclipse.jdt.internal.compiler.ast.Clinit) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Aggregations

AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)2 Clinit (org.eclipse.jdt.internal.compiler.ast.Clinit)2 EclipseNode (lombok.eclipse.EclipseNode)1 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1