Search in sources :

Example 1 with EclipseAST

use of lombok.eclipse.EclipseAST 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 EclipseAST

use of lombok.eclipse.EclipseAST 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)

Example 3 with EclipseAST

use of lombok.eclipse.EclipseAST in project lombok by rzwitserloot.

the class PatchDelegate method handleDelegateForType.

public static boolean handleDelegateForType(ClassScope scope) {
    if (TransformEclipseAST.disableLombok)
        return false;
    if (!hasDelegateMarkedFieldsOrMethods(scope.referenceContext))
        return false;
    List<ClassScopeEntry> stack = visited.get();
    StringBuilder corrupted = null;
    for (ClassScopeEntry entry : stack) {
        if (corrupted != null) {
            corrupted.append(" -> ").append(nameOfScope(entry.scope));
        } else if (entry.scope == scope) {
            corrupted = new StringBuilder().append(nameOfScope(scope));
        }
    }
    if (corrupted != null) {
        boolean found = false;
        String path = corrupted.toString();
        for (ClassScopeEntry entry : stack) {
            if (!found && entry.scope == scope)
                found = true;
            if (found)
                entry.corruptedPath = path;
        }
    } else {
        ClassScopeEntry entry = new ClassScopeEntry(scope);
        stack.add(entry);
        try {
            TypeDeclaration decl = scope.referenceContext;
            if (decl != null) {
                CompilationUnitDeclaration cud = scope.compilationUnitScope().referenceContext;
                EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                List<BindingTuple> methodsToDelegate = new ArrayList<BindingTuple>();
                fillMethodBindingsForFields(cud, scope, methodsToDelegate);
                if (entry.corruptedPath != null) {
                    eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
                } else {
                    generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.FIELD);
                }
                methodsToDelegate.clear();
                fillMethodBindingsForMethods(cud, scope, methodsToDelegate);
                if (entry.corruptedPath != null) {
                    eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
                } else {
                    generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.METHOD);
                }
            }
        } finally {
            stack.remove(stack.size() - 1);
        }
    }
    return false;
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) ArrayList(java.util.ArrayList) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 4 with EclipseAST

use of lombok.eclipse.EclipseAST in project lombok by rzwitserloot.

the class PatchExtensionMethod method getTypeNode.

public static EclipseNode getTypeNode(TypeDeclaration decl) {
    CompilationUnitDeclaration cud = decl.scope.compilationUnitScope().referenceContext;
    EclipseAST astNode = TransformEclipseAST.getAST(cud, false);
    EclipseNode node = astNode.get(decl);
    if (node == null) {
        astNode = TransformEclipseAST.getAST(cud, true);
        node = astNode.get(decl);
    }
    return node;
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) EclipseNode(lombok.eclipse.EclipseNode) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST)

Aggregations

EclipseAST (lombok.eclipse.EclipseAST)4 TransformEclipseAST (lombok.eclipse.TransformEclipseAST)4 ArrayList (java.util.ArrayList)3 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 ClassLiteralAccess (org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)2 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 EclipseNode (lombok.eclipse.EclipseNode)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1