Search in sources :

Example 6 with IAnnotation

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

the class JavaElementLinks method parseURI.

public static IJavaElement parseURI(String ssp, JavaProject project) {
    //        String ssp= uri.getSchemeSpecificPart();
    String[] segments = ssp.split(String.valueOf(LINK_SEPARATOR));
    // replace '[' manually, since URI confuses it for an IPv6 address as per RFC 2732:
    IJavaElement element = JavaCore.create(segments[1].replace(LINK_BRACKET_REPLACEMENT, '['));
    if (segments.length > 2) {
        String refTypeName = segments[2];
        if (refTypeName.indexOf('.') == -1) {
            try {
                ITypeParameter resolvedTypeVariable = resolveTypeVariable(element, refTypeName);
                if (resolvedTypeVariable != null)
                    return resolvedTypeVariable;
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }
        }
        if (element instanceof IAnnotation) {
            element = element.getParent();
        }
        if (element instanceof ILocalVariable) {
            element = ((ILocalVariable) element).getDeclaringMember();
        } else if (element instanceof ITypeParameter) {
            element = ((ITypeParameter) element).getDeclaringMember();
        }
        if (element instanceof IMember && !(element instanceof IType)) {
            element = ((IMember) element).getDeclaringType();
        }
        if (element instanceof IPackageFragment) {
            try {
                IPackageFragment root = (IPackageFragment) element;
                element = resolvePackageInfoType(root, refTypeName);
                if (element == null) {
                    // find it as package
                    IJavaProject javaProject = root.getJavaProject();
                    return JavaModelUtil.findTypeContainer(javaProject, refTypeName);
                }
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }
        }
        if (element instanceof IType) {
            try {
                IType type = (IType) element;
                if (refTypeName.length() > 0) {
                    type = resolveType(type, refTypeName);
                    if (type == null) {
                        IPackageFragment pack = JavaModelUtil.getPackageFragmentRoot(element).getPackageFragment(refTypeName);
                        if (pack.exists())
                            return pack;
                    }
                }
                if (type != null) {
                    element = type;
                    if (segments.length > 3) {
                        String refMemberName = segments[3];
                        if (segments.length > 4) {
                            String[] paramSignatures = new String[segments[4].length() == 0 ? 0 : segments.length - 4];
                            for (int i = 0; i < paramSignatures.length; i++) {
                                paramSignatures[i] = Signature.createTypeSignature(segments[i + 4], false);
                            }
                            IMethod method = type.getMethod(refMemberName, paramSignatures);
                            IMethod[] methods = type.findMethods(method);
                            if (methods != null) {
                                return methods[0];
                            } else {
                                //TODO: methods whose signature contains type parameters can not be found
                                // easily, since the Javadoc references are erasures
                                //Shortcut: only check name and parameter count:
                                methods = type.getMethods();
                                for (int i = 0; i < methods.length; i++) {
                                    method = methods[i];
                                    if (method.getElementName().equals(refMemberName) && method.getNumberOfParameters() == paramSignatures.length)
                                        return method;
                                }
                            //                                    // reference can also point to method from supertype:
                            //                                    ITypeHierarchy hierarchy= SuperTypeHierarchyCache.getTypeHierarchy(type);
                            //                                    method= JavaModelUtil.findMethodInHierarchy(hierarchy, type, refMemberName, paramSignatures, false);
                            //                                    if (method != null)
                            //                                        return method;
                            }
                        } else {
                            IField field = type.getField(refMemberName);
                            if (field.exists()) {
                                return field;
                            } else {
                                IMethod[] methods = type.getMethods();
                                for (int i = 0; i < methods.length; i++) {
                                    IMethod method = methods[i];
                                    if (method.getElementName().equals(refMemberName))
                                        return method;
                                }
                            }
                        }
                    }
                } else {
                // FIXME: either remove or show dialog
                //						JavaPlugin.logErrorMessage("JavaElementLinks could not resolve " + uri); //$NON-NLS-1$
                }
                return type;
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }
    return element;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IField(org.eclipse.jdt.core.IField) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) IJavaProject(org.eclipse.jdt.core.IJavaProject) IMethod(org.eclipse.jdt.core.IMethod)

Example 7 with IAnnotation

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

the class SourceTypeConverter method convertAnnotations.

private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
    IAnnotation[] annotations = element.getAnnotations();
    int length = annotations.length;
    Annotation[] astAnnotations = new Annotation[length];
    if (length > 0) {
        char[] cuSource = getSource();
        int recordedAnnotations = 0;
        for (int i = 0; i < length; i++) {
            ISourceRange positions = annotations[i].getSourceRange();
            int start = positions.getOffset();
            int end = start + positions.getLength();
            char[] annotationSource = CharOperation.subarray(cuSource, start, end);
            if (annotationSource != null) {
                Expression expression = parseMemberValue(annotationSource);
                /*
	    			 * expression can be null or not an annotation if the source has changed between
	    			 * the moment where the annotation source positions have been retrieved and the moment were
	    			 * this parsing occurred.
	    			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
	    			 */
                if (expression instanceof Annotation) {
                    astAnnotations[recordedAnnotations++] = (Annotation) expression;
                }
            }
        }
        if (length != recordedAnnotations) {
            // resize to remove null annotations
            System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
        }
    }
    return astAnnotations;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) IAnnotation(org.eclipse.jdt.core.IAnnotation) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 8 with IAnnotation

use of org.eclipse.jdt.core.IAnnotation in project liferay-ide by liferay.

the class PortletActionMethodRequestor method formatMethodName.

@Override
protected String formatMethodName(Object selectedNode, IMethod method) {
    String retval = null;
    if (_hasProcessActionAnnotation(method)) {
        IAnnotation annotation = method.getAnnotation("ProcessAction");
        IMemberValuePair pair = _findNamePair(annotation);
        if (pair != null) {
            retval = pair.getValue().toString();
        }
    } else {
        retval = method.getElementName();
    }
    return retval;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) IMemberValuePair(org.eclipse.jdt.core.IMemberValuePair)

Example 9 with IAnnotation

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

the class BaselineErrorHandler method findPackageInfoJavaVersionLocation.

ISourceRange findPackageInfoJavaVersionLocation(String packageName, ICompilationUnit compUnit) throws JavaModelException {
    ISourceRange range = null;
    IPackageDeclaration[] pkgDecls = compUnit.getPackageDeclarations();
    if (pkgDecls != null) {
        for (IPackageDeclaration pkgDecl : pkgDecls) {
            if (packageName.equals(pkgDecl.getElementName())) {
                IAnnotation[] annots = pkgDecl.getAnnotations();
                for (IAnnotation annot : annots) {
                    String name = annot.getElementName();
                    if (ANNOTATION_VERSION_NO_PKG.equals(name) || ANNOTATION_VERSION_OSGI.equals(name) || ANNOTATION_VERSION_BND.equals(name)) {
                        ASTParser parser = ASTParser.newParser(AST.JLS8);
                        parser.setKind(ASTParser.K_COMPILATION_UNIT);
                        parser.setSource(compUnit);
                        parser.setResolveBindings(true);
                        CompilationUnit ast = (CompilationUnit) parser.createAST(null);
                        if (ast != null) {
                            MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Predicate<String>() {

                                @Override
                                public boolean test(String t) {
                                    return ANNOTATION_VERSION_BND.equals(t) || ANNOTATION_VERSION_OSGI.equals(t);
                                }
                            }, "value");
                            ast.accept(mvpRetriever);
                            range = mvpRetriever.getMemberValuePairSourceRange();
                        }
                    }
                }
            }
        }
    }
    return range;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) MemberValuePairLocationRetriever(org.bndtools.builder.utils.MemberValuePairLocationRetriever) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 10 with IAnnotation

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

the class ComponentMarker method findAndMarkComponentAnnotations.

private static void findAndMarkComponentAnnotations(ICompilationUnit c) throws CoreException, JavaModelException {
    Document document = null;
    boolean found = false;
    String key = null;
    for (IType t : c.getTypes()) {
        for (IAnnotation annot : t.getAnnotations()) {
            if ("Component".equals(annot.getElementName())) {
                if (document == null)
                    document = new Document(c.getBuffer().getContents());
                found = true;
                key = getNameFromComponent(annot);
                int lineNumber;
                try {
                    lineNumber = document.getLineOfOffset(t.getSourceRange().getOffset()) + 1;
                    String message = key == null ? "OSGi Component" : key;
                    IMarker marker = c.getResource().createMarker(BndtoolsConstants.MARKER_COMPONENT);
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
                    marker.setAttribute(IMarker.MESSAGE, message);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
                    marker.setAttribute(IMarker.LOCATION, "line " + lineNumber);
                } catch (BadLocationException e) {
                    logger.logError("Component Marker error", e);
                    lineNumber = -1;
                }
            }
        }
    }
    if (!found) {
        c.getResource().deleteMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE);
    }
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) IMarker(org.eclipse.core.resources.IMarker) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

Aggregations

IAnnotation (org.eclipse.jdt.core.IAnnotation)10 IType (org.eclipse.jdt.core.IType)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IMethod (org.eclipse.jdt.core.IMethod)2 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 MemberValuePairLocationRetriever (org.bndtools.builder.utils.MemberValuePairLocationRetriever)1 IMarker (org.eclipse.core.resources.IMarker)1 IAnnotatable (org.eclipse.jdt.core.IAnnotatable)1 IField (org.eclipse.jdt.core.IField)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1 IMember (org.eclipse.jdt.core.IMember)1 IMemberValuePair (org.eclipse.jdt.core.IMemberValuePair)1 IPackageDeclaration (org.eclipse.jdt.core.IPackageDeclaration)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1