Search in sources :

Example 1 with QualifiedType

use of org.eclipse.jdt.core.dom.QualifiedType in project flux by eclipse.

the class ASTNodes method getTypeName.

/**
	 * Returns the simple name of the type, followed by array dimensions.
	 * Skips qualifiers, type arguments, and type annotations.
	 * <p>
	 * Does <b>not</b> work for WildcardTypes, etc.!
	 * 
	 * @param type a type that has a simple name
	 * @return the simple name, followed by array dimensions
	 * @see #getSimpleNameIdentifier(Name)
	 * @since 3.10
	 */
public static String getTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(PrimitiveType node) {
            buffer.append(node.getPrimitiveTypeCode().toString());
            return false;
        }

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(getSimpleNameIdentifier(node.getName()));
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                //$NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 2 with QualifiedType

use of org.eclipse.jdt.core.dom.QualifiedType in project bndtools by bndtools.

the class AbstractBuildErrorDetailsHandler method createMethodMarkerData.

/**
     * Create a marker on a Java Method
     *
     * @param javaProject
     * @param className
     *            - the fully qualified class name (e.g java.lang.String)
     * @param methodName
     * @param methodSignature
     *            - signatures are in "internal form" e.g. (Ljava.lang.Integer;[Ljava/lang/String;Z)V
     * @param markerAttributes
     *            - attributes that should be included in the marker, typically a message. The start and end points for
     *            the marker are added by this method.
     * @param hasResolutions
     *            - true if the marker will have resolutions
     * @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
     * @throws JavaModelException
     */
public static final MarkerData createMethodMarkerData(IJavaProject javaProject, final String className, final String methodName, final String methodSignature, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast == null)
        return null;
    ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(MethodDeclaration methodDecl) {
            if (matches(ast, methodDecl, methodName, methodSignature)) {
                // Create the marker attribs here
                markerAttributes.put(IMarker.CHAR_START, methodDecl.getStartPosition());
                markerAttributes.put(IMarker.CHAR_END, methodDecl.getStartPosition() + methodDecl.getLength());
            }
            return false;
        }

        private boolean matches(CompilationUnit ast, MethodDeclaration methodDecl, String methodName, String signature) {
            if ("<init>".equals(methodName)) {
                if (!methodDecl.isConstructor()) {
                    return false;
                }
            } else if (!methodDecl.getName().getIdentifier().equals(methodName)) {
                return false;
            }
            return getSignature(ast, methodDecl).equals(signature);
        }

        private String getSignature(CompilationUnit ast, MethodDeclaration methodDecl) {
            StringBuilder signatureBuilder = new StringBuilder("(");
            for (@SuppressWarnings("unchecked") Iterator<SingleVariableDeclaration> it = methodDecl.parameters().iterator(); it.hasNext(); ) {
                SingleVariableDeclaration decl = it.next();
                appendType(ast, signatureBuilder, decl.getType(), decl.getExtraDimensions());
            }
            signatureBuilder.append(")");
            appendType(ast, signatureBuilder, methodDecl.getReturnType2(), 0);
            return signatureBuilder.toString();
        }

        private void appendType(CompilationUnit ast, StringBuilder signatureBuilder, Type typeToAdd, int extraDimensions) {
            for (int i = 0; i < extraDimensions; i++) {
                signatureBuilder.append('[');
            }
            Type rovingType = typeToAdd;
            if (rovingType == null) {
                //A special return type for constructors, nice one Eclipse...
                signatureBuilder.append("V");
            } else {
                if (rovingType.isArrayType()) {
                    ArrayType type = (ArrayType) rovingType;
                    int depth = type.getDimensions();
                    for (int i = 0; i < depth; i++) {
                        signatureBuilder.append('[');
                    }
                    //We still need to add the array component type, which might be primitive or a reference
                    rovingType = type.getElementType();
                }
                // Type erasure means that we should ignore parameters
                if (rovingType.isParameterizedType()) {
                    rovingType = ((ParameterizedType) rovingType).getType();
                }
                if (rovingType.isPrimitiveType()) {
                    PrimitiveType type = (PrimitiveType) rovingType;
                    signatureBuilder.append(PRIMITIVES_TO_SIGNATURES.get(type.getPrimitiveTypeCode()));
                } else if (rovingType.isSimpleType()) {
                    SimpleType type = (SimpleType) rovingType;
                    String name;
                    if (type.getName().isQualifiedName()) {
                        name = type.getName().getFullyQualifiedName();
                    } else {
                        name = getFullyQualifiedNameForSimpleName(ast, type.getName());
                    }
                    name = name.replace('.', '/');
                    signatureBuilder.append("L").append(name).append(";");
                } else if (rovingType.isQualifiedType()) {
                    QualifiedType type = (QualifiedType) rovingType;
                    String name = type.getQualifier().toString().replace('.', '/') + '/' + type.getName().getFullyQualifiedName().replace('.', '/');
                    signatureBuilder.append("L").append(name).append(";");
                } else {
                    throw new IllegalArgumentException("We hit an unknown type " + rovingType);
                }
            }
        }

        private String getFullyQualifiedNameForSimpleName(CompilationUnit ast, Name typeName) {
            String name = typeName.getFullyQualifiedName();
            @SuppressWarnings("unchecked") List<ImportDeclaration> ids = ast.imports();
            for (ImportDeclaration id : ids) {
                if (id.isStatic())
                    continue;
                if (id.isOnDemand()) {
                    String packageName = id.getName().getFullyQualifiedName();
                    try {
                        if (ast.getJavaElement().getJavaProject().findType(packageName + "." + name) != null) {
                            name = packageName + '.' + name;
                        }
                    } catch (JavaModelException e) {
                    }
                } else {
                    String importName = id.getName().getFullyQualifiedName();
                    if (importName.endsWith("." + name)) {
                        name = importName;
                        break;
                    }
                }
            }
            if (name.indexOf('.') < 0) {
                try {
                    if (ast.getJavaElement().getJavaProject().findType(name) == null) {
                        name = "java.lang." + name;
                    }
                } catch (JavaModelException e) {
                }
            }
            return name;
        }
    });
    if (!markerAttributes.containsKey(IMarker.CHAR_START))
        return null;
    return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) IType(org.eclipse.jdt.core.IType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) Iterator(java.util.Iterator) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) List(java.util.List)

Example 3 with QualifiedType

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

the class PotentialProgrammingProblemsFix method getSelectedName.

private static SimpleName getSelectedName(CompilationUnit compilationUnit, IProblemLocation problem) {
    final ASTNode selection = problem.getCoveredNode(compilationUnit);
    if (selection == null)
        return null;
    Name name = null;
    if (selection instanceof SimpleType) {
        name = ((SimpleType) selection).getName();
    } else if (selection instanceof NameQualifiedType) {
        name = ((NameQualifiedType) selection).getName();
    } else if (selection instanceof QualifiedType) {
        name = ((QualifiedType) selection).getName();
    } else if (selection instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) selection;
        final Type raw = type.getType();
        if (raw instanceof SimpleType)
            name = ((SimpleType) raw).getName();
        else if (raw instanceof NameQualifiedType)
            name = ((NameQualifiedType) raw).getName();
        else if (raw instanceof QualifiedType)
            name = ((QualifiedType) raw).getName();
    } else if (selection instanceof Name) {
        name = (Name) selection;
    }
    if (name == null)
        return null;
    if (name.isSimpleName()) {
        return (SimpleName) name;
    } else {
        return ((QualifiedName) name).getName();
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) IType(org.eclipse.jdt.core.IType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) 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) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 4 with QualifiedType

use of org.eclipse.jdt.core.dom.QualifiedType in project flux by eclipse.

the class ASTResolving method internalGetPossibleTypeKinds.

private static int internalGetPossibleTypeKinds(ASTNode node) {
    int kind = SimilarElementsRequestor.ALL_TYPES;
    int mask = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
    ASTNode parent = node.getParent();
    while (parent instanceof QualifiedName) {
        if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
            return SimilarElementsRequestor.REF_TYPES;
        }
        node = parent;
        parent = parent.getParent();
        mask = SimilarElementsRequestor.REF_TYPES;
    }
    while (parent instanceof Type) {
        if (parent instanceof QualifiedType) {
            if (node.getLocationInParent() == QualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof NameQualifiedType) {
            if (node.getLocationInParent() == NameQualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof ParameterizedType) {
            if (node.getLocationInParent() == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            mask &= SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
        } else if (parent instanceof WildcardType) {
            if (node.getLocationInParent() == WildcardType.BOUND_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
        }
        node = parent;
        parent = parent.getParent();
    }
    switch(parent.getNodeType()) {
        case ASTNode.TYPE_DECLARATION:
            if (node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else if (node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            }
            break;
        case ASTNode.ENUM_DECLARATION:
            kind = SimilarElementsRequestor.INTERFACES;
            break;
        case ASTNode.METHOD_DECLARATION:
            if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (node.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
                kind = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
            }
            break;
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            kind = SimilarElementsRequestor.PRIMITIVETYPES | SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS;
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.THROW_STATEMENT:
            kind = SimilarElementsRequestor.CLASSES;
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            if (((ClassInstanceCreation) parent).getAnonymousClassDeclaration() == null) {
                kind = SimilarElementsRequestor.CLASSES;
            } else {
                kind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
            }
            break;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            int superParent = parent.getParent().getNodeType();
            if (superParent == ASTNode.CATCH_CLAUSE) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (superParent == ASTNode.ENHANCED_FOR_STATEMENT) {
                kind = SimilarElementsRequestor.REF_TYPES;
            }
            break;
        case ASTNode.TAG_ELEMENT:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.MARKER_ANNOTATION:
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
        case ASTNode.NORMAL_ANNOTATION:
            kind = SimilarElementsRequestor.ANNOTATIONS;
            break;
        case ASTNode.TYPE_PARAMETER:
            if (((TypeParameter) parent).typeBounds().indexOf(node) > 0) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else {
                kind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            break;
        case ASTNode.TYPE_LITERAL:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        default:
    }
    return kind & mask;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 5 with QualifiedType

use of org.eclipse.jdt.core.dom.QualifiedType in project flux by eclipse.

the class ASTNodes method getQualifiedTypeName.

/**
	 * Returns the (potentially qualified) name of a type, followed by array dimensions.
	 * Skips type arguments and type annotations.
	 * 
	 * @param type a type that has a name
	 * @return the name, followed by array dimensions
	 * @since 3.10
	 */
public static String getQualifiedTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(node.getName().getFullyQualifiedName());
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            node.getQualifier().accept(this);
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getQualifier().getFullyQualifiedName());
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                //$NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)4 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 Type (org.eclipse.jdt.core.dom.Type)3 IType (org.eclipse.jdt.core.IType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Name (org.eclipse.jdt.core.dom.Name)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 Iterator (java.util.Iterator)1 List (java.util.List)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1