Search in sources :

Example 21 with SimpleType

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

the class UnresolvedElementsSubProcessor method getTypeProposals.

public static void getTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
    if (kind == SimilarElementsRequestor.REF_TYPES) {
        addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
    }
    while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        selectedNode = selectedNode.getParent();
    }
    Name node = null;
    if (selectedNode instanceof SimpleType) {
        node = ((SimpleType) selectedNode).getName();
    } else if (selectedNode instanceof NameQualifiedType) {
        node = ((NameQualifiedType) selectedNode).getName();
    } else if (selectedNode instanceof ArrayType) {
        Type elementType = ((ArrayType) selectedNode).getElementType();
        if (elementType.isSimpleType()) {
            node = ((SimpleType) elementType).getName();
        } else if (elementType.isNameQualifiedType()) {
            node = ((NameQualifiedType) elementType).getName();
        } else {
            return;
        }
    } else if (selectedNode instanceof Name) {
        node = (Name) selectedNode;
    } else {
        return;
    }
    // change to similar type proposals
    addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
    while (node.getParent() instanceof QualifiedName) {
        node = (Name) node.getParent();
    }
    if (selectedNode != node) {
        kind = evauateTypeKind(node, cu.getJavaProject());
    }
    if ((kind & (SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES)) != 0) {
        // only propose annotations when there are no other suggestions
        kind &= ~SimilarElementsRequestor.ANNOTATIONS;
    }
    addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
    ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), proposals);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleType(org.eclipse.jdt.core.dom.SimpleType) IType(org.eclipse.jdt.core.IType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 22 with SimpleType

use of org.eclipse.jdt.core.dom.SimpleType in project generator by mybatis.

the class NewJavaFileVisitorTest method testRegularClass.

@Test
public void testRegularClass() throws IOException {
    InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTable.java.src");
    CompilationUnit cu = getCompilationUnitFromResource(resource);
    NewJavaFileVisitor visitor = new NewJavaFileVisitor();
    cu.accept(visitor);
    assertThat(visitor.getImports().size(), is(1));
    assertThat(visitor.getNewNodes().size(), is(50));
    assertThat(visitor.getSuperclass().isSimpleType(), is(true));
    assertThat(((SimpleType) visitor.getSuperclass()).getName().getFullyQualifiedName(), is("AwfulTableKey"));
    assertThat(visitor.getSuperInterfaceTypes().size(), is(0));
    assertThat(visitor.isInterface(), is(false));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleType(org.eclipse.jdt.core.dom.SimpleType) InputStream(java.io.InputStream) NewJavaFileVisitor(org.mybatis.generator.eclipse.core.merge.NewJavaFileVisitor) Test(org.junit.Test)

Example 23 with SimpleType

use of org.eclipse.jdt.core.dom.SimpleType 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)

Aggregations

SimpleType (org.eclipse.jdt.core.dom.SimpleType)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)13 Type (org.eclipse.jdt.core.dom.Type)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)9 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)8 ArrayList (java.util.ArrayList)7 Name (org.eclipse.jdt.core.dom.Name)7 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 List (java.util.List)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 Expression (org.eclipse.jdt.core.dom.Expression)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 IType (org.eclipse.jdt.core.IType)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)4