Search in sources :

Example 46 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.

the class ReorgCorrectionsSubProcessor method importNotFoundProposals.

public static void importNotFoundProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    ImportDeclaration importDeclaration = (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
    if (importDeclaration == null) {
        return;
    }
    if (!importDeclaration.isOnDemand()) {
        Name name = importDeclaration.getName();
        if (importDeclaration.isStatic() && name.isQualifiedName()) {
            name = ((QualifiedName) name).getQualifier();
        }
        int kind = JavaModelUtil.is50OrHigher(cu.getJavaProject()) ? SimilarElementsRequestor.REF_TYPES : SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
        UnresolvedElementsSubProcessor.addNewTypeProposals(cu, name, kind, IProposalRelevance.IMPORT_NOT_FOUND_NEW_TYPE, proposals);
    }
    String name = ASTNodes.asString(importDeclaration.getName());
    if (importDeclaration.isOnDemand()) {
        //$NON-NLS-1$
        name = JavaModelUtil.concatenateName(name, "*");
    }
    addProjectSetupFixProposal(context, problem, name, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 47 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.

the class ImportRemover method divideTypeRefs.

private void divideTypeRefs(List<SimpleName> importNames, List<SimpleName> staticNames, List<SimpleName> removedRefs, List<SimpleName> unremovedRefs) {
    final List<int[]> removedStartsEnds = new ArrayList<int[]>();
    fRoot.accept(new ASTVisitor(true) {

        int fRemovingStart = -1;

        @Override
        public void preVisit(ASTNode node) {
            Object property = node.getProperty(PROPERTY_KEY);
            if (property == REMOVED) {
                if (fRemovingStart == -1) {
                    fRemovingStart = node.getStartPosition();
                } else {
                    /*
						 * Bug in client code: REMOVED node should not be nested inside another REMOVED node without
						 * an intermediate RETAINED node.
						 * Drop REMOVED property to prevent problems later (premature end of REMOVED section).
						 */
                    node.setProperty(PROPERTY_KEY, null);
                }
            } else if (property == RETAINED) {
                if (fRemovingStart != -1) {
                    removedStartsEnds.add(new int[] { fRemovingStart, node.getStartPosition() });
                    fRemovingStart = -1;
                } else {
                    /*
						 * Bug in client code: RETAINED node should not be nested inside another RETAINED node without
						 * an intermediate REMOVED node and must have an enclosing REMOVED node.
						 * Drop RETAINED property to prevent problems later (premature restart of REMOVED section).
						 */
                    node.setProperty(PROPERTY_KEY, null);
                }
            }
            super.preVisit(node);
        }

        @Override
        public void postVisit(ASTNode node) {
            Object property = node.getProperty(PROPERTY_KEY);
            if (property == RETAINED) {
                int end = node.getStartPosition() + node.getLength();
                fRemovingStart = end;
            } else if (property == REMOVED) {
                if (fRemovingStart != -1) {
                    int end = node.getStartPosition() + node.getLength();
                    removedStartsEnds.add(new int[] { fRemovingStart, end });
                    fRemovingStart = -1;
                }
            }
            super.postVisit(node);
        }
    });
    for (Iterator<SimpleName> iterator = importNames.iterator(); iterator.hasNext(); ) {
        SimpleName name = iterator.next();
        if (isInRemoved(name, removedStartsEnds))
            removedRefs.add(name);
        else
            unremovedRefs.add(name);
    }
    for (Iterator<SimpleName> iterator = staticNames.iterator(); iterator.hasNext(); ) {
        SimpleName name = iterator.next();
        if (isInRemoved(name, removedStartsEnds))
            removedRefs.add(name);
        else
            unremovedRefs.add(name);
    }
    for (Iterator<ImportDeclaration> iterator = fInlinedStaticImports.iterator(); iterator.hasNext(); ) {
        ImportDeclaration importDecl = iterator.next();
        Name name = importDecl.getName();
        if (name instanceof QualifiedName)
            name = ((QualifiedName) name).getName();
        removedRefs.add((SimpleName) name);
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 48 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project lombok by rzwitserloot.

the class PatchValEclipse method createValAnnotation.

public static MarkerAnnotation createValAnnotation(AST ast, Annotation original, int start, int end) {
    MarkerAnnotation out = null;
    try {
        out = Reflection.markerAnnotationConstructor.newInstance(ast);
    } catch (InstantiationException e) {
        throw Lombok.sneakyThrow(e);
    } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
    } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e);
    }
    if (out != null) {
        SimpleName valName = ast.newSimpleName("val");
        valName.setSourceRange(start, end - start + 1);
        if (original.type instanceof SingleTypeReference) {
            out.setTypeName(valName);
            setIndex(valName, 1);
        } else {
            SimpleName lombokName = ast.newSimpleName("lombok");
            lombokName.setSourceRange(start, end - start + 1);
            setIndex(lombokName, 1);
            setIndex(valName, 2);
            QualifiedName fullName = ast.newQualifiedName(lombokName, valName);
            setIndex(fullName, 1);
            fullName.setSourceRange(start, end - start + 1);
            out.setTypeName(fullName);
        }
        out.setSourceRange(start, end - start + 1);
    }
    return out;
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 49 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project processing by processing.

the class CompletionGenerator method resolveExpression3rdParty.

/**
   * Finds the type of the expression in foo.bar().a().b, this would give me the
   * type of b if it exists in return type of a(). If noCompare is true,
   * it'll return type of a()
   * @param nearestNode
   * @param astNode
   * @return
   */
public static ClassMember resolveExpression3rdParty(PreprocessedSketch ps, ASTNode nearestNode, ASTNode astNode, boolean noCompare) {
    log("Resolve 3rdParty expr-- " + getNodeAsString(astNode) + " nearest node " + getNodeAsString(nearestNode));
    if (astNode == null)
        return null;
    ClassMember scopeParent;
    SimpleType stp;
    if (astNode instanceof SimpleName) {
        ASTNode decl = findDeclaration2(((SimpleName) astNode), nearestNode);
        if (decl != null) {
            // see if locally defined
            log(getNodeAsString(astNode) + " found decl -> " + getNodeAsString(decl));
            {
                if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
                    TypeDeclaration td = (TypeDeclaration) decl;
                    return new ClassMember(ps, td);
                }
            }
            {
                // Handle "array." x "array[1]."
                Type type = extracTypeInfo2(decl);
                if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
                    // No array access, we want members of the array itself
                    Type elementType = ((ArrayType) type).getElementType();
                    // Get name of the element class
                    String name = "";
                    if (elementType.isSimpleType()) {
                        Class<?> c = findClassIfExists(ps, elementType.toString());
                        if (c != null)
                            name = c.getName();
                    } else if (elementType.isPrimitiveType()) {
                        name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
                    }
                    // Convert element class to array class
                    Class<?> arrayClass = getArrayClass(name, ps.classLoader);
                    return arrayClass == null ? null : new ClassMember(arrayClass);
                }
            }
            return new ClassMember(ps, extracTypeInfo(decl));
        } else {
            // or in a predefined class?
            Class<?> tehClass = findClassIfExists(ps, astNode.toString());
            if (tehClass != null) {
                return new ClassMember(tehClass);
            }
        }
        astNode = astNode.getParent();
    }
    switch(astNode.getNodeType()) {
        //TODO: Notice the redundancy in the 3 cases, you can simplify things even more.
        case ASTNode.FIELD_ACCESS:
            FieldAccess fa = (FieldAccess) astNode;
            if (fa.getExpression() == null) {
                // TODO: Check for existence of 'new' keyword. Could be a ClassInstanceCreation
                // Local code or belongs to super class
                log("FA,Not implemented.");
                return null;
            } else {
                if (fa.getExpression() instanceof SimpleName) {
                    stp = extracTypeInfo(findDeclaration2((SimpleName) fa.getExpression(), nearestNode));
                    if (stp == null) {
                        /*The type wasn't found in local code, so it might be something like
             * log(), or maybe belonging to super class, etc.
             */
                        Class<?> tehClass = findClassIfExists(ps, fa.getExpression().toString());
                        if (tehClass != null) {
                            // so look for method in this class.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), fa.getName().toString());
                        }
                        log("FA resolve 3rd par, Can't resolve " + fa.getExpression());
                        return null;
                    }
                    log("FA, SN Type " + getNodeAsString(stp));
                    scopeParent = definedIn3rdPartyClass(ps, stp.getName().toString(), "THIS");
                } else {
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, fa.getExpression(), noCompare);
                }
                log("FA, ScopeParent " + scopeParent);
                return definedIn3rdPartyClass(ps, scopeParent, fa.getName().toString());
            }
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation mi = (MethodInvocation) astNode;
            ASTNode temp = findDeclaration2(mi.getName(), nearestNode);
            if (temp instanceof MethodDeclaration) {
                // method is locally defined
                log(mi.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp)));
                {
                    // Handle "array." x "array[1]."
                    Type type = extracTypeInfo2(temp);
                    if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
                        // No array access, we want members of the array itself
                        Type elementType = ((ArrayType) type).getElementType();
                        // Get name of the element class
                        String name = "";
                        if (elementType.isSimpleType()) {
                            Class<?> c = findClassIfExists(ps, elementType.toString());
                            if (c != null)
                                name = c.getName();
                        } else if (elementType.isPrimitiveType()) {
                            name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
                        }
                        // Convert element class to array class
                        Class<?> arrayClass = getArrayClass(name, ps.classLoader);
                        return arrayClass == null ? null : new ClassMember(arrayClass);
                    }
                }
                return new ClassMember(ps, extracTypeInfo(temp));
            }
            if (mi.getExpression() == null) {
                //        if()
                //Local code or belongs to super class
                log("MI,Not implemented.");
                return null;
            } else {
                if (mi.getExpression() instanceof SimpleName) {
                    ASTNode decl = findDeclaration2((SimpleName) mi.getExpression(), nearestNode);
                    if (decl != null) {
                        if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
                            TypeDeclaration td = (TypeDeclaration) decl;
                            return new ClassMember(ps, td);
                        }
                        stp = extracTypeInfo(decl);
                        if (stp == null) {
                            /*The type wasn't found in local code, so it might be something like
             * System.console()., or maybe belonging to super class, etc.
             */
                            Class<?> tehClass = findClassIfExists(ps, mi.getExpression().toString());
                            if (tehClass != null) {
                                // so look for method in this class.
                                return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
                            }
                            log("MI resolve 3rd par, Can't resolve " + mi.getExpression());
                            return null;
                        }
                        log("MI, SN Type " + getNodeAsString(stp));
                        ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
                        if (typeDec == null) {
                            log(stp.getName() + " couldn't be found locally..");
                            Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
                            if (tehClass != null) {
                                // so look for method in this class.
                                return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
                            }
                        //return new ClassMember(findClassIfExists(stp.getName().toString()));
                        }
                        //scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
                        return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), mi.getName().toString());
                    }
                } else {
                    log("MI EXP.." + getNodeAsString(mi.getExpression()));
                    //          return null;
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, mi.getExpression(), noCompare);
                    log("MI, ScopeParent " + scopeParent);
                    return definedIn3rdPartyClass(ps, scopeParent, mi.getName().toString());
                }
            }
            break;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qn = (QualifiedName) astNode;
            ASTNode temp2 = findDeclaration2(qn.getName(), nearestNode);
            if (temp2 instanceof FieldDeclaration) {
                // field is locally defined
                log(qn.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp2)));
                return new ClassMember(ps, extracTypeInfo(temp2));
            }
            if (qn.getQualifier() == null) {
                log("QN,Not implemented.");
                return null;
            } else {
                if (qn.getQualifier() instanceof SimpleName) {
                    stp = extracTypeInfo(findDeclaration2(qn.getQualifier(), nearestNode));
                    if (stp == null) {
                        /*The type wasn't found in local code, so it might be something like
             * log(), or maybe belonging to super class, etc.
             */
                        Class<?> tehClass = findClassIfExists(ps, qn.getQualifier().toString());
                        if (tehClass != null) {
                            // note how similar thing is called on line 690. Check check.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
                        }
                        log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
                        return null;
                    }
                    log("QN, SN Local Type " + getNodeAsString(stp));
                    //scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
                    ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
                    if (typeDec == null) {
                        log(stp.getName() + " couldn't be found locally..");
                        Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
                        if (tehClass != null) {
                            // note how similar thing is called on line 690. Check check.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
                        }
                        log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
                        return null;
                    }
                    return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), qn.getName().toString());
                } else {
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, qn.getQualifier(), noCompare);
                    log("QN, ScopeParent " + scopeParent);
                    return definedIn3rdPartyClass(ps, scopeParent, qn.getName().toString());
                }
            }
        case ASTNode.ARRAY_ACCESS:
            ArrayAccess arac = (ArrayAccess) astNode;
            return resolveExpression3rdParty(ps, nearestNode, arac.getArray(), noCompare);
        default:
            log("Unaccounted type " + getNodeAsString(astNode));
            break;
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ASTNode(org.eclipse.jdt.core.dom.ASTNode) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 50 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project AutoRefactor by JnRouvignac.

the class ForLoopHelper method buildForLoopContent.

private static ForLoopContent buildForLoopContent(final Expression loopVar, final Expression containerVar) {
    if (!(loopVar instanceof Name)) {
        return null;
    }
    if (containerVar instanceof MethodInvocation) {
        final MethodInvocation mi = (MethodInvocation) containerVar;
        final Name containerVarName = as(mi.getExpression(), Name.class);
        if (containerVarName != null && isMethod(mi, "java.util.Collection", "size")) {
            return ForLoopContent.indexedCollection(containerVarName, (Name) loopVar);
        }
    } else if (containerVar instanceof QualifiedName) {
        final QualifiedName containerVarName = (QualifiedName) containerVar;
        if (isArrayLength(containerVarName)) {
            Name containerVariable = ((QualifiedName) containerVar).getQualifier();
            return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
        }
    }
    return null;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)68 SimpleName (org.eclipse.jdt.core.dom.SimpleName)47 ASTNode (org.eclipse.jdt.core.dom.ASTNode)36 Name (org.eclipse.jdt.core.dom.Name)26 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)22 Expression (org.eclipse.jdt.core.dom.Expression)20 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)18 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)17 IBinding (org.eclipse.jdt.core.dom.IBinding)16 SimpleType (org.eclipse.jdt.core.dom.SimpleType)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 ArrayList (java.util.ArrayList)13 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)12 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)10 Type (org.eclipse.jdt.core.dom.Type)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)9 IType (org.eclipse.jdt.core.IType)8 Assignment (org.eclipse.jdt.core.dom.Assignment)8