Search in sources :

Example 1 with ClassLiteralAccess

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

the class PatchDelegate method fillMethodBindingsForFields.

private static void fillMethodBindingsForFields(CompilationUnitDeclaration cud, ClassScope scope, List<BindingTuple> methodsToDelegate) {
    TypeDeclaration decl = scope.referenceContext;
    if (decl == null)
        return;
    if (decl.fields != null)
        for (FieldDeclaration field : decl.fields) {
            if (field.annotations == null)
                continue;
            for (Annotation ann : field.annotations) {
                if (!isDelegate(ann, decl))
                    continue;
                if (Annotation_applied.getAndSet(ann, true))
                    continue;
                if ((field.modifiers & ClassFileConstants.AccStatic) != 0) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                List<ClassLiteralAccess> rawTypes = rawTypes(ann, "types");
                List<ClassLiteralAccess> excludedRawTypes = rawTypes(ann, "excludes");
                List<BindingTuple> methodsToExclude = new ArrayList<BindingTuple>();
                List<BindingTuple> methodsToDelegateForThisAnn = new ArrayList<BindingTuple>();
                try {
                    for (ClassLiteralAccess cla : excludedRawTypes) {
                        addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet<String>(), field.name, ann);
                    }
                    Set<String> banList = new HashSet<String>();
                    for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized));
                    if (rawTypes.isEmpty()) {
                        addAllMethodBindings(methodsToDelegateForThisAnn, field.type.resolveType(decl.initializerScope), banList, field.name, ann);
                    } else {
                        for (ClassLiteralAccess cla : rawTypes) {
                            addAllMethodBindings(methodsToDelegateForThisAnn, cla.type.resolveType(decl.initializerScope), banList, field.name, ann);
                        }
                    }
                } catch (DelegateRecursion e) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(String.format(RECURSION_NOT_ALLOWED, new String(e.member), new String(e.type)));
                    break;
                }
                // Not doing this right now because of problems - see commented-out-method for info.
                // removeExistingMethods(methodsToDelegate, decl, scope);
                String dupe = containsDuplicates(methodsToDelegateForThisAnn);
                if (dupe != null) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError("The method '" + dupe + "' is being delegated by more than one specified type.");
                } else {
                    methodsToDelegate.addAll(methodsToDelegateForThisAnn);
                }
            }
        }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) HashSet(java.util.HashSet)

Example 2 with ClassLiteralAccess

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

the class PatchExtensionMethod method getApplicableExtensionMethods.

static List<Extension> getApplicableExtensionMethods(EclipseNode typeNode, Annotation ann, TypeBinding receiverType) {
    List<Extension> extensions = new ArrayList<Extension>();
    if ((typeNode != null) && (ann != null) && (receiverType != null)) {
        BlockScope blockScope = ((TypeDeclaration) typeNode.get()).initializerScope;
        EclipseNode annotationNode = typeNode.getNodeFor(ann);
        AnnotationValues<ExtensionMethod> annotation = createAnnotation(ExtensionMethod.class, annotationNode);
        boolean suppressBaseMethods = false;
        try {
            suppressBaseMethods = annotation.getInstance().suppressBaseMethods();
        } catch (AnnotationValueDecodeFail fail) {
            fail.owner.setError(fail.getMessage(), fail.idx);
        }
        for (Object extensionMethodProvider : annotation.getActualExpressions("value")) {
            if (extensionMethodProvider instanceof ClassLiteralAccess) {
                TypeBinding binding = ((ClassLiteralAccess) extensionMethodProvider).type.resolveType(blockScope);
                if (binding == null)
                    continue;
                if (!binding.isClass() && !binding.isEnum())
                    continue;
                Extension e = new Extension();
                e.extensionMethods = getApplicableExtensionMethodsDefinedInProvider(typeNode, (ReferenceBinding) binding, receiverType);
                e.suppressBaseMethods = suppressBaseMethods;
                extensions.add(e);
            }
        }
    }
    return extensions;
}
Also used : AnnotationValueDecodeFail(lombok.core.AnnotationValues.AnnotationValueDecodeFail) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ArrayList(java.util.ArrayList) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ExtensionMethod(lombok.experimental.ExtensionMethod) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)

Example 3 with ClassLiteralAccess

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

the class HandleLog method processAnnotation.

public static void processAnnotation(LoggingFramework framework, AnnotationValues<? extends java.lang.annotation.Annotation> annotation, Annotation source, EclipseNode annotationNode, String loggerTopic) {
    EclipseNode owner = annotationNode.up();
    switch(owner.getKind()) {
        case TYPE:
            String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME);
            if (logFieldName == null)
                logFieldName = "log";
            boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC));
            TypeDeclaration typeDecl = null;
            if (owner.get() instanceof TypeDeclaration)
                typeDecl = (TypeDeclaration) owner.get();
            int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
            boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
            if (typeDecl == null || notAClass) {
                annotationNode.addError(framework.getAnnotationAsString() + " is legal only on classes and enums.");
                return;
            }
            if (fieldExists(logFieldName, owner) != MemberExistsResult.NOT_EXISTS) {
                annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
                return;
            }
            ClassLiteralAccess loggingType = selfType(owner, source);
            FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName, useStatic, loggerTopic);
            fieldDeclaration.traverse(new SetGeneratedByVisitor(source), typeDecl.staticInitializerScope);
            // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217
            // injectFieldSuppressWarnings(owner, fieldDeclaration);
            injectField(owner, fieldDeclaration);
            owner.rebuild();
            break;
        default:
            break;
    }
}
Also used : EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 4 with ClassLiteralAccess

use of org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess 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 5 with ClassLiteralAccess

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

the class PatchDelegate method fillMethodBindingsForMethods.

private static void fillMethodBindingsForMethods(CompilationUnitDeclaration cud, ClassScope scope, List<BindingTuple> methodsToDelegate) {
    TypeDeclaration decl = scope.referenceContext;
    if (decl == null)
        return;
    if (decl.methods != null)
        for (AbstractMethodDeclaration methodDecl : decl.methods) {
            if (methodDecl.annotations == null)
                continue;
            for (Annotation ann : methodDecl.annotations) {
                if (!isDelegate(ann, decl))
                    continue;
                if (Annotation_applied.getAndSet(ann, true))
                    continue;
                if (!(methodDecl instanceof MethodDeclaration)) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                if (methodDecl.arguments != null) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                if ((methodDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                MethodDeclaration method = (MethodDeclaration) methodDecl;
                List<ClassLiteralAccess> rawTypes = rawTypes(ann, "types");
                List<ClassLiteralAccess> excludedRawTypes = rawTypes(ann, "excludes");
                List<BindingTuple> methodsToExclude = new ArrayList<BindingTuple>();
                List<BindingTuple> methodsToDelegateForThisAnn = new ArrayList<BindingTuple>();
                try {
                    for (ClassLiteralAccess cla : excludedRawTypes) {
                        addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet<String>(), method.selector, ann);
                    }
                    Set<String> banList = new HashSet<String>();
                    for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized));
                    if (rawTypes.isEmpty()) {
                        if (method.returnType == null)
                            continue;
                        addAllMethodBindings(methodsToDelegateForThisAnn, method.returnType.resolveType(decl.initializerScope), banList, method.selector, ann);
                    } else {
                        for (ClassLiteralAccess cla : rawTypes) {
                            addAllMethodBindings(methodsToDelegateForThisAnn, cla.type.resolveType(decl.initializerScope), banList, method.selector, ann);
                        }
                    }
                } catch (DelegateRecursion e) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(String.format(RECURSION_NOT_ALLOWED, new String(e.member), new String(e.type)));
                    break;
                }
                // Not doing this right now because of problems - see commented-out-method for info.
                // removeExistingMethods(methodsToDelegate, decl, scope);
                String dupe = containsDuplicates(methodsToDelegateForThisAnn);
                if (dupe != null) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError("The method '" + dupe + "' is being delegated by more than one specified type.");
                } else {
                    methodsToDelegate.addAll(methodsToDelegateForThisAnn);
                }
            }
        }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) HashSet(java.util.HashSet)

Aggregations

ClassLiteralAccess (org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)5 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)5 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 EclipseAST (lombok.eclipse.EclipseAST)2 EclipseNode (lombok.eclipse.EclipseNode)2 TransformEclipseAST (lombok.eclipse.TransformEclipseAST)2 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)2 AnnotationValueDecodeFail (lombok.core.AnnotationValues.AnnotationValueDecodeFail)1 ExtensionMethod (lombok.experimental.ExtensionMethod)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 BlockScope (org.eclipse.jdt.internal.compiler.lookup.BlockScope)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1