Search in sources :

Example 31 with TypeDeclaration

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

the class HandleFieldDefaults method generateFieldDefaultsForType.

public boolean generateFieldDefaultsForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean makeFinal, boolean checkForTypeLevelFieldDefaults) {
    if (checkForTypeLevelFieldDefaults) {
        if (hasAnnotation(FieldDefaults.class, typeNode)) {
            //The annotation will make it happen, so we can skip it.
            return true;
        }
    }
    TypeDeclaration typeDecl = null;
    if (typeNode.get() instanceof TypeDeclaration)
        typeDecl = (TypeDeclaration) typeNode.get();
    int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
    boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
    if (typeDecl == null || notAClass) {
        pos.addError("@FieldDefaults is only supported on a class or an enum.");
        return false;
    }
    for (EclipseNode field : typeNode.down()) {
        if (field.getKind() != Kind.FIELD)
            continue;
        FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
        if (!filterField(fieldDecl, false))
            continue;
        Class<?> t = field.get().getClass();
        if (t == FieldDeclaration.class) {
            // There are various other things that extend FieldDeclaration that really
            // aren't field declarations. Typing 'ma' in an otherwise blank class is a
            // CompletionOnFieldType object (extends FieldDeclaration). If we mess with the
            // modifiers of such a thing, you take away template suggestions such as
            // 'main method'. See issue 411.
            setFieldDefaultsForField(field, pos.get(), level, makeFinal);
        }
    }
    return true;
}
Also used : EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 32 with TypeDeclaration

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

the class HandleGetter method generateGetterForType.

public boolean generateGetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelGetter) {
    if (checkForTypeLevelGetter) {
        if (hasAnnotation(Getter.class, typeNode)) {
            //The annotation will make it happen, so we can skip it.
            return true;
        }
    }
    TypeDeclaration typeDecl = null;
    if (typeNode.get() instanceof TypeDeclaration)
        typeDecl = (TypeDeclaration) typeNode.get();
    int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
    boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
    if (typeDecl == null || notAClass) {
        pos.addError("@Getter is only supported on a class, an enum, or a field.");
        return false;
    }
    for (EclipseNode field : typeNode.down()) {
        if (fieldQualifiesForGetterGeneration(field))
            generateGetterForField(field, pos.get(), level, false);
    }
    return true;
}
Also used : EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 33 with TypeDeclaration

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

the class HandleHelper method handle.

@Override
public void handle(AnnotationValues<Helper> annotation, Annotation ast, EclipseNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
    EclipseNode annotatedType = annotationNode.up();
    EclipseNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
    Statement[] origStatements = getStatementsFromAstNode(containingBlock == null ? null : containingBlock.get());
    if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
        annotationNode.addError("@Helper is legal only on method-local classes.");
        return;
    }
    TypeDeclaration annotatedType_ = (TypeDeclaration) annotatedType.get();
    int indexOfType = -1;
    for (int i = 0; i < origStatements.length; i++) {
        if (origStatements[i] == annotatedType_) {
            indexOfType = i;
            break;
        }
    }
    final List<String> knownMethodNames = new ArrayList<String>();
    for (AbstractMethodDeclaration methodOfHelper : annotatedType_.methods) {
        if (!(methodOfHelper instanceof MethodDeclaration))
            continue;
        char[] name = methodOfHelper.selector;
        if (name != null && name.length > 0 && name[0] != '<')
            knownMethodNames.add(new String(name));
    }
    Collections.sort(knownMethodNames);
    final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
    final char[] helperName = new char[annotatedType_.name.length + 1];
    final boolean[] helperUsed = new boolean[1];
    helperName[0] = '$';
    System.arraycopy(annotatedType_.name, 0, helperName, 1, helperName.length - 1);
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(MessageSend messageSend, BlockScope scope) {
            if (messageSend.receiver instanceof ThisReference) {
                if ((((ThisReference) messageSend.receiver).bits & ASTNode.IsImplicitThis) == 0)
                    return true;
            } else if (messageSend.receiver != null)
                return true;
            char[] name = messageSend.selector;
            if (name == null || name.length == 0 || name[0] == '<')
                return true;
            String n = new String(name);
            if (Arrays.binarySearch(knownMethodNames_, n) < 0)
                return true;
            messageSend.receiver = new SingleNameReference(helperName, messageSend.nameSourcePosition);
            helperUsed[0] = true;
            return true;
        }
    };
    for (int i = indexOfType + 1; i < origStatements.length; i++) {
        origStatements[i].traverse(visitor, null);
    }
    if (!helperUsed[0]) {
        annotationNode.addWarning("No methods of this helper class are ever used.");
        return;
    }
    Statement[] newStatements = new Statement[origStatements.length + 1];
    System.arraycopy(origStatements, 0, newStatements, 0, indexOfType + 1);
    System.arraycopy(origStatements, indexOfType + 1, newStatements, indexOfType + 2, origStatements.length - indexOfType - 1);
    LocalDeclaration decl = new LocalDeclaration(helperName, 0, 0);
    decl.modifiers |= ClassFileConstants.AccFinal;
    AllocationExpression alloc = new AllocationExpression();
    alloc.type = new SingleTypeReference(annotatedType_.name, 0L);
    decl.initialization = alloc;
    decl.type = new SingleTypeReference(annotatedType_.name, 0L);
    SetGeneratedByVisitor sgbvVisitor = new SetGeneratedByVisitor(annotationNode.get());
    decl.traverse(sgbvVisitor, null);
    newStatements[indexOfType + 1] = decl;
    setStatementsOfAstNode(containingBlock.get(), newStatements);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ASTVisitor(org.eclipse.jdt.internal.compiler.ASTVisitor) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 34 with TypeDeclaration

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

the class HandleLog method selfType.

public static ClassLiteralAccess selfType(EclipseNode type, Annotation source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    TypeDeclaration typeDeclaration = (TypeDeclaration) type.get();
    TypeReference typeReference = new SingleTypeReference(typeDeclaration.name, p);
    setGeneratedBy(typeReference, source);
    ClassLiteralAccess result = new ClassLiteralAccess(source.sourceEnd, typeReference);
    setGeneratedBy(result, source);
    return result;
}
Also used : SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)

Example 35 with TypeDeclaration

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

the class HandleUtilityClass method createPrivateDefaultConstructor.

private void createPrivateDefaultConstructor(EclipseNode typeNode, EclipseNode sourceNode) {
    ASTNode source = sourceNode.get();
    TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;
    ConstructorDeclaration constructor = new ConstructorDeclaration(((CompilationUnitDeclaration) typeNode.top().get()).compilationResult);
    constructor.modifiers = ClassFileConstants.AccPrivate;
    constructor.selector = typeDeclaration.name;
    constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    constructor.constructorCall.sourceStart = source.sourceStart;
    constructor.constructorCall.sourceEnd = source.sourceEnd;
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
    constructor.arguments = null;
    AllocationExpression exception = new AllocationExpression();
    setGeneratedBy(exception, source);
    long[] ps = new long[JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION.length];
    Arrays.fill(ps, p);
    exception.type = new QualifiedTypeReference(JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION, ps);
    setGeneratedBy(exception.type, source);
    exception.arguments = new Expression[] { new StringLiteral(UNSUPPORTED_MESSAGE, source.sourceStart, source.sourceEnd, 0) };
    setGeneratedBy(exception.arguments[0], source);
    ThrowStatement throwStatement = new ThrowStatement(exception, source.sourceStart, source.sourceEnd);
    setGeneratedBy(throwStatement, source);
    constructor.statements = new Statement[] { throwStatement };
    injectMethod(typeNode, constructor);
}
Also used : ExplicitConstructorCall(org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)44 EclipseNode (lombok.eclipse.EclipseNode)25 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)16 ArrayList (java.util.ArrayList)14 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)12 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)11 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)7 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)6 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)6 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)6 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)5 ClassLiteralAccess (org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)5 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)5 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)5 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)5 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)5 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)5 TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)5 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)4 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)4