Search in sources :

Example 11 with PrimitiveType

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

the class GenerateForLoopAssistProposal method getForInitializer.

/**
	 * Generates a {@link VariableDeclarationExpression}, which initializes the loop variable to
	 * iterate over an array.
	 * 
	 * @param ast the current {@link AST} instance
	 * @param loopVariableName the name of the variable which should be initialized
	 * @return a filled {@link VariableDeclarationExpression}, declaring a int variable, which is
	 *         initializes with 0
	 */
private VariableDeclarationExpression getForInitializer(AST ast, SimpleName loopVariableName) {
    // initializing fragment
    VariableDeclarationFragment firstDeclarationFragment = ast.newVariableDeclarationFragment();
    firstDeclarationFragment.setName(loopVariableName);
    NumberLiteral startIndex = ast.newNumberLiteral();
    firstDeclarationFragment.setInitializer(startIndex);
    // declaration
    VariableDeclarationExpression variableDeclaration = ast.newVariableDeclarationExpression(firstDeclarationFragment);
    PrimitiveType variableType = ast.newPrimitiveType(PrimitiveType.INT);
    variableDeclaration.setType(variableType);
    return variableDeclaration;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 12 with PrimitiveType

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

the class TypeContextChecker method parseSuperType.

private static Type parseSuperType(String superType, boolean isInterface) {
    if (!superType.trim().equals(superType)) {
        return null;
    }
    StringBuffer cuBuff = new StringBuffer();
    if (isInterface)
        //$NON-NLS-1$
        cuBuff.append("class __X__ implements ");
    else
        //$NON-NLS-1$
        cuBuff.append("class __X__ extends ");
    int offset = cuBuff.length();
    //$NON-NLS-1$
    cuBuff.append(superType).append(" {}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    Map<String, String> options = new HashMap<String, String>();
    JavaModelUtil.setComplianceOptions(options, JavaModelUtil.VERSION_LATEST);
    p.setCompilerOptions(options);
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    ASTNode selected = NodeFinder.perform(cu, offset, superType.length());
    if (selected instanceof Name)
        selected = selected.getParent();
    if (selected.getStartPosition() != offset || selected.getLength() != superType.length() || !(selected instanceof Type) || selected instanceof PrimitiveType) {
        return null;
    }
    Type type = (Type) selected;
    String typeNodeRange = cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
    if (!superType.equals(typeNodeRange)) {
        return null;
    }
    return type;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) HashMap(java.util.HashMap) ASTNode(org.eclipse.jdt.core.dom.ASTNode) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 13 with PrimitiveType

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

PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 Type (org.eclipse.jdt.core.dom.Type)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)7 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 IType (org.eclipse.jdt.core.IType)4 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)4 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 AST (org.eclipse.jdt.core.dom.AST)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Block (org.eclipse.jdt.core.dom.Block)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 Javadoc (org.eclipse.jdt.core.dom.Javadoc)3 Name (org.eclipse.jdt.core.dom.Name)3 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 ArrayList (java.util.ArrayList)2