Search in sources :

Example 96 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class AbstractLaunchShortcut method launchSelectedObject.

protected void launchSelectedObject(Object selected, String mode) throws CoreException {
    if (selected instanceof IJavaElement) {
        launchJavaElements(Collections.singletonList((IJavaElement) selected), mode);
    } else if (selected instanceof IResource && Project.BNDFILE.equals(((IResource) selected).getName())) {
        IProject project = ((IResource) selected).getProject();
        launchProject(project, mode);
    } else if (selected instanceof IFile && ((IFile) selected).getName().endsWith(LaunchConstants.EXT_BNDRUN)) {
        IFile bndRunFile = (IFile) selected;
        launchBndRun(bndRunFile, mode);
    } else if (selected instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) selected;
        IJavaElement javaElement = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
        if (javaElement != null) {
            launchJavaElements(Collections.singletonList(javaElement), mode);
        } else {
            IResource resource = (IResource) adaptable.getAdapter(IResource.class);
            if (resource != null && resource != selected)
                launchSelectedObject(resource, mode);
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 97 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement 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 98 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class JavadocFinder method addValue.

private void addValue(StringBuffer buf, IJavaElement element, Object value) throws URISyntaxException {
    // Note: To be bug-compatible with Javadoc from Java 5/6/7, we currently don't escape HTML tags in String-valued annotations.
    if (value instanceof ITypeBinding) {
        ITypeBinding typeBinding = (ITypeBinding) value;
        IJavaElement type = typeBinding.getJavaElement();
        if (type == null) {
            buf.append(typeBinding.getName());
        } else {
            String uri = JavaElementLinks.createURI(baseHref, type);
            String name = type.getElementName();
            addLink(buf, uri, name);
        }
        //$NON-NLS-1$
        buf.append(".class");
    } else if (value instanceof IVariableBinding) {
        // only enum constants
        IVariableBinding variableBinding = (IVariableBinding) value;
        IJavaElement variable = variableBinding.getJavaElement();
        String uri = JavaElementLinks.createURI(baseHref, variable);
        String name = variable.getElementName();
        addLink(buf, uri, name);
    } else if (value instanceof IAnnotationBinding) {
        IAnnotationBinding annotationBinding = (IAnnotationBinding) value;
        addAnnotation(buf, element, annotationBinding);
    } else if (value instanceof String) {
        buf.append(ASTNodes.getEscapedStringLiteral((String) value));
    } else if (value instanceof Character) {
        buf.append(ASTNodes.getEscapedCharacterLiteral((Character) value));
    } else if (value instanceof Object[]) {
        Object[] values = (Object[]) value;
        buf.append('{');
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                buf.append(JavaElementLabels.COMMA_STRING);
            }
            addValue(buf, element, values[i]);
        }
        buf.append('}');
    } else {
        // primitive types (except char) or null
        buf.append(String.valueOf(value));
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 99 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class JavadocFinder method addAnnotation.

private void addAnnotation(StringBuffer buf, IJavaElement element, IAnnotationBinding annotation) throws URISyntaxException {
    IJavaElement javaElement = annotation.getAnnotationType().getJavaElement();
    buf.append('@');
    if (javaElement != null) {
        String uri = JavaElementLinks.createURI(baseHref, javaElement);
        addLink(buf, uri, annotation.getName());
    } else {
        buf.append(annotation.getName());
    }
    IMemberValuePairBinding[] mvPairs = annotation.getDeclaredMemberValuePairs();
    if (mvPairs.length > 0) {
        buf.append('(');
        for (int j = 0; j < mvPairs.length; j++) {
            if (j > 0) {
                buf.append(JavaElementLabels.COMMA_STRING);
            }
            IMemberValuePairBinding mvPair = mvPairs[j];
            String memberURI = JavaElementLinks.createURI(baseHref, mvPair.getMethodBinding().getJavaElement());
            addLink(buf, memberURI, mvPair.getName());
            buf.append('=');
            addValue(buf, element, mvPair.getValue());
        }
        buf.append(')');
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMemberValuePairBinding(org.eclipse.jdt.core.dom.IMemberValuePairBinding)

Example 100 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class JavadocFinder method findJavadoc.

public String findJavadoc(IJavaProject project, String fqn, int offset) throws JavaModelException {
    IMember member = null;
    IType type = project.findType(fqn);
    ICodeAssist codeAssist;
    if (type.isBinary()) {
        codeAssist = type.getClassFile();
    } else {
        codeAssist = type.getCompilationUnit();
    }
    IJavaElement[] elements = null;
    if (codeAssist != null) {
        elements = codeAssist.codeSelect(/*region.getOffset(), region.getLength()*/
        offset, 0);
    }
    IJavaElement element = null;
    if (elements != null && elements.length > 0) {
        element = elements[0];
    }
    if (element != null && element instanceof IMember) {
        member = ((IMember) element);
    }
    if (member == null) {
        return null;
    }
    return getJavadoc(member);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICodeAssist(org.eclipse.jdt.core.ICodeAssist) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)208 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)46 IType (org.eclipse.jdt.core.IType)41 ArrayList (java.util.ArrayList)34 IJavaProject (org.eclipse.jdt.core.IJavaProject)32 JavaModelException (org.eclipse.jdt.core.JavaModelException)31 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)30 IResource (org.eclipse.core.resources.IResource)29 IMethod (org.eclipse.jdt.core.IMethod)28 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)28 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)21 IField (org.eclipse.jdt.core.IField)14 IMember (org.eclipse.jdt.core.IMember)14 CoreException (org.eclipse.core.runtime.CoreException)13 StringTokenizer (java.util.StringTokenizer)11 ISourceRange (org.eclipse.jdt.core.ISourceRange)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 IPath (org.eclipse.core.runtime.IPath)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)8