Search in sources :

Example 41 with TypeDeclaration

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

the class EclipseHandlerUtil method cloneSelfType.

public static TypeReference cloneSelfType(EclipseNode context, ASTNode source) {
    int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
    long p = (long) pS << 32 | pE;
    EclipseNode type = context;
    TypeReference result = null;
    while (type != null && type.getKind() != Kind.TYPE) type = type.up();
    if (type != null && type.get() instanceof TypeDeclaration) {
        TypeDeclaration typeDecl = (TypeDeclaration) type.get();
        if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
            TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
            int idx = 0;
            for (TypeParameter param : typeDecl.typeParameters) {
                TypeReference typeRef = new SingleTypeReference(param.name, (long) param.sourceStart << 32 | param.sourceEnd);
                if (source != null)
                    setGeneratedBy(typeRef, source);
                refs[idx++] = typeRef;
            }
            result = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p);
        } else {
            result = new SingleTypeReference(((TypeDeclaration) type.get()).name, p);
        }
    }
    if (result != null && source != null)
        setGeneratedBy(result, source);
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 42 with TypeDeclaration

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

the class EclipseHandlerUtil method injectType.

/**
	 * Adds an inner type (class, interface, enum) to the given type. Cannot inject top-level types.
	 * 
	 * @param typeNode parent type to inject new type into
	 * @param type New type (class, interface, etc) to inject.
	 */
public static EclipseNode injectType(final EclipseNode typeNode, final TypeDeclaration type) {
    type.annotations = addSuppressWarningsAll(typeNode, type, type.annotations);
    type.annotations = addGenerated(typeNode, type, type.annotations);
    TypeDeclaration parent = (TypeDeclaration) typeNode.get();
    if (parent.memberTypes == null) {
        parent.memberTypes = new TypeDeclaration[] { type };
    } else {
        TypeDeclaration[] newArray = new TypeDeclaration[parent.memberTypes.length + 1];
        System.arraycopy(parent.memberTypes, 0, newArray, 0, parent.memberTypes.length);
        newArray[parent.memberTypes.length] = type;
        parent.memberTypes = newArray;
    }
    return typeNode.add(type, Kind.TYPE);
}
Also used : TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 43 with TypeDeclaration

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

use of org.eclipse.jdt.internal.compiler.ast.TypeDeclaration 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)

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