Search in sources :

Example 1 with TypeDeclaration

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

the class HandleFactory method createElement.

/**
	 * Create handle by adding child to parent obtained by recursing into parent scopes.
	 */
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;
    switch(scope.kind) {
        case Scope.COMPILATION_UNIT_SCOPE:
            newElement = unit;
            break;
        case Scope.CLASS_SCOPE:
            IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            switch(parentElement.getElementType()) {
                case IJavaElement.COMPILATION_UNIT:
                    newElement = ((ICompilationUnit) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.TYPE:
                    newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.FIELD:
                case IJavaElement.INITIALIZER:
                case IJavaElement.METHOD:
                    IMember member = (IMember) parentElement;
                    if (member.isBinary()) {
                        return null;
                    } else {
                        newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                        // increment occurrence count if collision is detected
                        if (newElement != null) {
                            while (!existingElements.add(newElement)) ((SourceRefElement) newElement).occurrenceCount++;
                        }
                    }
                    break;
            }
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
            break;
        case Scope.METHOD_SCOPE:
            if (scope.isLambdaScope()) {
                parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
                LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
                if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && !(expression.descriptor instanceof ProblemMethodBinding)) {
                    // chain in lambda element only if resolved properly.
                    //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    knownScopes.put(scope, newElement);
                    return newElement;
                }
                return parentElement;
            }
            IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            MethodScope methodScope = (MethodScope) scope;
            if (methodScope.isInsideInitializer()) {
                // inside field or initializer, must find proper one
                TypeDeclaration type = methodScope.referenceType();
                int occurenceCount = 1;
                int length = type.fields == null ? 0 : type.fields.length;
                for (int i = 0; i < length; i++) {
                    FieldDeclaration field = type.fields[i];
                    if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
                        switch(field.getKind()) {
                            case AbstractVariableDeclaration.FIELD:
                            case AbstractVariableDeclaration.ENUM_CONSTANT:
                                newElement = parentType.getField(new String(field.name));
                                break;
                            case AbstractVariableDeclaration.INITIALIZER:
                                newElement = parentType.getInitializer(occurenceCount);
                                break;
                        }
                        break;
                    } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                        occurenceCount++;
                    }
                }
            } else {
                // method element
                AbstractMethodDeclaration method = methodScope.referenceMethod();
                newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
                if (newElement != null) {
                    knownScopes.put(scope, newElement);
                }
            }
            break;
        case Scope.BLOCK_SCOPE:
            // standard block, no element per se
            newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            break;
    }
    return newElement;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodScope(org.eclipse.jdt.internal.compiler.lookup.MethodScope) IMember(org.eclipse.jdt.core.IMember) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) SourceRefElement(org.eclipse.jdt.internal.core.SourceRefElement) IType(org.eclipse.jdt.core.IType)

Example 2 with TypeDeclaration

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

the class SourceTypeConverter method convert.

/*
	 * Convert an initializerinfo into a parsed initializer declaration
	 */
private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException {
    Block block = new Block(0);
    Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault);
    int start = initializerInfo.getDeclarationSourceStart();
    int end = initializerInfo.getDeclarationSourceEnd();
    initializer.sourceStart = initializer.declarationSourceStart = start;
    initializer.sourceEnd = initializer.declarationSourceEnd = end;
    initializer.modifiers = initializerInfo.getModifiers();
    /* convert local and anonymous types */
    IJavaElement[] children = initializerInfo.getChildren();
    int typesLength = children.length;
    if (typesLength > 0) {
        Statement[] statements = new Statement[typesLength];
        for (int i = 0; i < typesLength; i++) {
            SourceType type = (SourceType) children[i];
            TypeDeclaration localType = convert(type, compilationResult);
            if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                expression.type = localType.superclass;
                localType.superclass = null;
                localType.superInterfaces = null;
                localType.allocation = expression;
                statements[i] = expression;
            } else {
                statements[i] = localType;
            }
        }
        block.statements = statements;
    }
    return initializer;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer) Initializer(org.eclipse.jdt.internal.compiler.ast.Initializer) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Block(org.eclipse.jdt.internal.compiler.ast.Block) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 3 with TypeDeclaration

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

the class SourceTypeConverter method convert.

/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 */
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;
    final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
    if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
        // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        } else {
            return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
        }
    }
    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();
    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
			 * We used a temporary types collection to prevent this.unit.types from being null during a call to
			 * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
			 */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ISourceImport(org.eclipse.jdt.internal.compiler.env.ISourceImport) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) CompilationUnitElementInfo(org.eclipse.jdt.internal.core.CompilationUnitElementInfo) ImportDeclaration(org.eclipse.jdt.internal.core.ImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 4 with TypeDeclaration

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

the class PatchDelegate method checkConflictOfTypeVarNames.

public static void checkConflictOfTypeVarNames(BindingTuple binding, EclipseNode typeNode) throws CantMakeDelegates {
    TypeVariableBinding[] typeVars = binding.parameterized.typeVariables();
    if (typeVars == null || typeVars.length == 0)
        return;
    Set<String> usedInOurType = new HashSet<String>();
    EclipseNode enclosingType = typeNode;
    while (enclosingType != null) {
        if (enclosingType.getKind() == Kind.TYPE) {
            TypeParameter[] typeParameters = ((TypeDeclaration) enclosingType.get()).typeParameters;
            if (typeParameters != null) {
                for (TypeParameter param : typeParameters) {
                    if (param.name != null)
                        usedInOurType.add(new String(param.name));
                }
            }
        }
        enclosingType = enclosingType.up();
    }
    Set<String> usedInMethodSig = new HashSet<String>();
    for (TypeVariableBinding var : typeVars) {
        char[] sourceName = var.sourceName();
        if (sourceName != null)
            usedInMethodSig.add(new String(sourceName));
    }
    usedInMethodSig.retainAll(usedInOurType);
    if (usedInMethodSig.isEmpty())
        return;
    // We might be delegating a List<T>, and we are making method <T> toArray(). A conflict is possible.
    // But only if the toArray method also uses type vars from its class, otherwise we're only shadowing,
    // which is okay as we'll add a @SuppressWarnings.
    TypeVarFinder finder = new TypeVarFinder();
    finder.visitRaw(binding.base);
    Set<String> names = new HashSet<String>(finder.getTypeVariables());
    names.removeAll(usedInMethodSig);
    if (!names.isEmpty()) {
        // We have a confirmed conflict. We could dig deeper as this may still be a false alarm, but its already an exceedingly rare case.
        CantMakeDelegates cmd = new CantMakeDelegates();
        cmd.conflicted = usedInMethodSig;
        throw cmd;
    }
}
Also used : TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) HashSet(java.util.HashSet)

Example 5 with TypeDeclaration

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

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