Search in sources :

Example 1 with AnnotationMethodDeclaration

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

the class Util method getUnresolvedJavaElement.

/**
     * Return the java element corresponding to the given compiler binding.
     */
public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
    JavaElement unresolvedJavaElement = getUnresolvedJavaElement(methodBinding.declaringClass, workingCopyOwner, bindingsToNodes);
    if (unresolvedJavaElement == null || unresolvedJavaElement.getElementType() != IJavaElement.TYPE) {
        return null;
    }
    IType declaringType = (IType) unresolvedJavaElement;
    org.eclipse.jdt.internal.compiler.ast.ASTNode node = bindingsToNodes == null ? null : bindingsToNodes.get(methodBinding);
    if (node != null && !declaringType.isBinary()) {
        if (node instanceof AnnotationMethodDeclaration) {
            // node is an AnnotationMethodDeclaration
            AnnotationMethodDeclaration typeMemberDeclaration = (AnnotationMethodDeclaration) node;
            // annotation type members don't have parameters
            return (JavaElement) declaringType.getMethod(String.valueOf(typeMemberDeclaration.selector), CharOperation.NO_STRINGS);
        } else {
            // node is an MethodDeclaration
            MethodDeclaration methodDeclaration = (MethodDeclaration) node;
            Argument[] arguments = methodDeclaration.arguments;
            String[] parameterSignatures;
            if (arguments != null) {
                parameterSignatures = new String[arguments.length];
                for (int i = 0; i < arguments.length; i++) {
                    Argument argument = arguments[i];
                    TypeReference typeReference = argument.type;
                    int arrayDim = typeReference.dimensions();
                    String typeSig = Signature.createTypeSignature(CharOperation.concatWith(typeReference.getTypeName(), '.'), false);
                    if (arrayDim > 0) {
                        typeSig = Signature.createArraySignature(typeSig, arrayDim);
                    }
                    parameterSignatures[i] = typeSig;
                }
            } else {
                parameterSignatures = CharOperation.NO_STRINGS;
            }
            return (JavaElement) declaringType.getMethod(String.valueOf(methodDeclaration.selector), parameterSignatures);
        }
    } else {
        // case of method not in the created AST, or a binary method
        org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = methodBinding.original();
        String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
        boolean isBinary = declaringType.isBinary();
        ReferenceBinding enclosingType = original.declaringClass.enclosingType();
        // Static inner types' constructors don't get receivers (https://bugs.eclipse.org/bugs/show_bug.cgi?id=388137)
        boolean isInnerBinaryTypeConstructor = isBinary && original.isConstructor() && !original.declaringClass.isStatic() && enclosingType != null;
        TypeBinding[] parameters = original.parameters;
        int length = parameters == null ? 0 : parameters.length;
        int declaringIndex = isInnerBinaryTypeConstructor ? 1 : 0;
        String[] parameterSignatures = new String[declaringIndex + length];
        if (isInnerBinaryTypeConstructor)
            parameterSignatures[0] = new String(enclosingType.genericTypeSignature()).replace('/', '.');
        for (int i = 0; i < length; i++) {
            char[] signature = parameters[i].genericTypeSignature();
            if (isBinary) {
                signature = CharOperation.replaceOnCopy(signature, '/', '.');
            } else {
                signature = toUnresolvedTypeSignature(signature);
            }
            parameterSignatures[declaringIndex + i] = new String(signature);
        }
        IMethod result = declaringType.getMethod(selector, parameterSignatures);
        if (isBinary)
            return (JavaElement) result;
        if (// if perfect match (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249567 )
        result.exists())
            return (JavaElement) result;
        IMethod[] methods = null;
        try {
            methods = declaringType.getMethods();
        } catch (JavaModelException e) {
            // declaring type doesn't exist
            return null;
        }
        IMethod[] candidates = Member.findMethods(result, methods);
        if (candidates == null || candidates.length == 0)
            return null;
        return (JavaElement) candidates[0];
    }
}
Also used : MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) IType(org.eclipse.jdt.core.IType) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) IMethod(org.eclipse.jdt.core.IMethod)

Example 2 with AnnotationMethodDeclaration

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

the class SourceTypeConverter method convert.

/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;
    /* only source positions available */
    int start = methodInfo.getNameSourceStart();
    int end = methodInfo.getNameSourceEnd();
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
		   on behalf of a 1.4 project we must internalize type variables properly in order to be able to
		   recognize usages of them in the method signature, to apply substitutions and thus to be able to
		   detect overriding in the presence of generics. If we simply drop them, when the method signature
		   refers to the type parameter, we won't know it should be bound to the type parameter and perform
		   incorrect lookup and may mistakenly end up with missing types
		 */
    TypeParameter[] typeParams = null;
    char[][] typeParameterNames = methodInfo.getTypeParameterNames();
    if (typeParameterNames != null) {
        int parameterCount = typeParameterNames.length;
        if (parameterCount > 0) {
            // method's type parameters must be null if no type parameter
            char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
            typeParams = new TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
            }
        }
    }
    int modifiers = methodInfo.getModifiers();
    if (methodInfo.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        method = decl;
        decl.typeParameters = typeParams;
    } else {
        MethodDeclaration decl;
        if (methodInfo.isAnnotationMethod()) {
            AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
            /* conversion of default value */
            SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
            boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
            if ((this.flags & FIELD_INITIALIZATION) != 0) {
                if (hasDefaultValue) {
                    char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
                    if (defaultValueSource != null) {
                        Expression expression = parseMemberValue(defaultValueSource);
                        if (expression != null) {
                            annotationMethodDeclaration.defaultValue = expression;
                        }
                    } else {
                        // could not retrieve the default value
                        hasDefaultValue = false;
                    }
                }
            }
            if (hasDefaultValue)
                modifiers |= ClassFileConstants.AccAnnotationDefault;
            decl = annotationMethodDeclaration;
        } else {
            decl = new MethodDeclaration(compilationResult);
        }
        // convert return type
        decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
        // type parameters
        decl.typeParameters = typeParams;
        method = decl;
    }
    method.selector = methodHandle.getElementName().toCharArray();
    boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
    method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
    method.sourceStart = start;
    method.sourceEnd = end;
    method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
    method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        method.annotations = convertAnnotations(methodHandle);
    }
    /* convert arguments */
    String[] argumentTypeSignatures = methodHandle.getParameterTypes();
    char[][] argumentNames = methodInfo.getArgumentNames();
    int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
    if (argumentCount > 0) {
        ILocalVariable[] parameters = methodHandle.getParameters();
        long position = ((long) start << 32) + end;
        method.arguments = new Argument[argumentCount];
        for (int i = 0; i < argumentCount; i++) {
            TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
            if (isVarargs && i == argumentCount - 1) {
                typeReference.bits |= ASTNode.IsVarArgs;
            }
            method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
            // convert 1.5 specific constructs only if compliance is 1.5 or above
            if (this.has1_5Compliance) {
                /* convert annotations */
                method.arguments[i].annotations = convertAnnotations(parameters[i]);
            }
        }
    }
    /* convert thrown exceptions */
    char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        method.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
        }
    }
    /* convert local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = methodInfo.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;
                }
            }
            method.statements = statements;
        }
    }
    return method;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) IJavaElement(org.eclipse.jdt.core.IJavaElement) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) SourceAnnotationMethodInfo(org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 3 with AnnotationMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration in project spoon by INRIA.

the class PositionBuilder method buildPositionCtElement.

SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
    CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
    CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
    CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
    int[] lineSeparatorPositions = cr.lineSeparatorPositions;
    char[] contents = cr.compilationUnit.getContents();
    int sourceStart = node.sourceStart;
    int sourceEnd = node.sourceEnd;
    if ((node instanceof Annotation)) {
        Annotation ann = (Annotation) node;
        int declEnd = ann.declarationSourceEnd;
        if (declEnd > 0) {
            sourceEnd = declEnd;
        }
    } else if ((node instanceof Expression)) {
        Expression expression = (Expression) node;
        int statementEnd = expression.statementEnd;
        if (statementEnd > 0) {
            sourceEnd = statementEnd;
        }
    }
    if (node instanceof AbstractVariableDeclaration) {
        AbstractVariableDeclaration variableDeclaration = (AbstractVariableDeclaration) node;
        int modifiersSourceStart = variableDeclaration.modifiersSourceStart;
        int declarationSourceStart = variableDeclaration.declarationSourceStart;
        int declarationSourceEnd = variableDeclaration.declarationSourceEnd;
        int declarationEnd = variableDeclaration.declarationEnd;
        Annotation[] annotations = variableDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == sourceStart) {
                modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
            }
        }
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        int modifiersSourceEnd;
        if (variableDeclaration.type != null) {
            modifiersSourceEnd = variableDeclaration.type.sourceStart() - 2;
        } else {
            // variable that has no type such as TypeParameter
            modifiersSourceEnd = declarationSourceStart - 1;
        }
        // when no modifier
        if (modifiersSourceStart > modifiersSourceEnd) {
            modifiersSourceEnd = modifiersSourceStart - 1;
        }
        return cf.createDeclarationSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, lineSeparatorPositions);
    } else if (node instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) node;
        int declarationSourceStart = typeDeclaration.declarationSourceStart;
        int declarationSourceEnd = typeDeclaration.declarationSourceEnd;
        int modifiersSourceStart = typeDeclaration.modifiersSourceStart;
        int bodyStart = typeDeclaration.bodyStart;
        int bodyEnd = typeDeclaration.bodyEnd;
        Annotation[] annotations = typeDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == declarationSourceStart) {
                modifiersSourceStart = findNextNonWhitespace(contents, annotations[annotations.length - 1].declarationSourceEnd + 1);
            }
        }
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        // look for start of first keyword before the type keyword e.g. "class". `sourceStart` points at first char of type name
        int modifiersSourceEnd = findPrevNonWhitespace(contents, findPrevWhitespace(contents, findPrevNonWhitespace(contents, sourceStart - 1)));
        if (modifiersSourceEnd < modifiersSourceStart) {
            // there is no modifier
            modifiersSourceEnd = modifiersSourceStart - 1;
        }
        return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart - 1, bodyEnd, lineSeparatorPositions);
    } else if (node instanceof AbstractMethodDeclaration) {
        AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node;
        int bodyStart = methodDeclaration.bodyStart;
        int bodyEnd = methodDeclaration.bodyEnd;
        int declarationSourceStart = methodDeclaration.declarationSourceStart;
        int declarationSourceEnd = methodDeclaration.declarationSourceEnd;
        int modifiersSourceStart = methodDeclaration.modifiersSourceStart;
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        if (node instanceof AnnotationMethodDeclaration && bodyStart == bodyEnd) {
            // The ";" at the end of annotation method declaration is not part of body
            // let it behave same like in abstract MethodDeclaration
            bodyEnd--;
        }
        Javadoc javadoc = methodDeclaration.javadoc;
        if (javadoc != null && javadoc.sourceEnd() > declarationSourceStart) {
            modifiersSourceStart = javadoc.sourceEnd() + 1;
        }
        Annotation[] annotations = methodDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == declarationSourceStart) {
                modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
            }
        }
        int modifiersSourceEnd = sourceStart - 1;
        if (methodDeclaration instanceof MethodDeclaration && ((MethodDeclaration) methodDeclaration).returnType != null) {
            modifiersSourceEnd = ((MethodDeclaration) methodDeclaration).returnType.sourceStart() - 2;
        }
        TypeParameter[] typeParameters = methodDeclaration.typeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
        }
        if (getModifiers(methodDeclaration.modifiers, false, true).isEmpty()) {
            modifiersSourceStart = modifiersSourceEnd + 1;
        }
        sourceEnd = sourceStart + methodDeclaration.selector.length - 1;
        if (e instanceof CtStatementList) {
            return cf.createSourcePosition(cu, bodyStart - 1, bodyEnd + 1, lineSeparatorPositions);
        } else {
            if (bodyStart == 0) {
                return SourcePosition.NOPOSITION;
            } else {
                if (bodyStart < bodyEnd) {
                    // include brackets if they are there
                    if (contents[bodyStart - 1] == '{') {
                        bodyStart--;
                        if (contents[bodyEnd + 1] == '}') {
                            bodyEnd++;
                        } else {
                            throw new SpoonException("Missing body end in\n" + new String(contents, sourceStart, sourceEnd - sourceStart));
                        }
                    }
                }
                return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart, bodyEnd, lineSeparatorPositions);
            }
        }
    }
    return cf.createSourcePosition(cu, sourceStart, sourceEnd, lineSeparatorPositions);
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) SpoonException(spoon.SpoonException) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) Javadoc(org.eclipse.jdt.internal.compiler.ast.Javadoc) CoreFactory(spoon.reflect.factory.CoreFactory) AbstractVariableDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtStatementList(spoon.reflect.code.CtStatementList) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Aggregations

AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)3 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)3 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)2 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)2 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)2 TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1 IMethod (org.eclipse.jdt.core.IMethod)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)1 Javadoc (org.eclipse.jdt.internal.compiler.ast.Javadoc)1 QualifiedAllocationExpression (org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression)1