Example usage for org.eclipse.jdt.core IJavaElement METHOD

List of usage examples for org.eclipse.jdt.core IJavaElement METHOD

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement METHOD.

Prototype

int METHOD

To view the source code for org.eclipse.jdt.core IJavaElement METHOD.

Click Source Link

Document

Constant representing a method or constructor.

Usage

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsElementFactory.java

License:Open Source License

/**
 * Attempts to create a new JAX-RS element from the given {@link Annotation}
 * . Multiple elements can be returned, for example if a java method is
 * annotated with a JAX-RS annotation (ex: <code>@Path()</code>), the parent
 * type becomes a JAX-RS Subresource./*w w w  . jav  a  2 s  .co  m*/
 * 
 * @param element
 * @param ast
 * @param metamodel
 * @return the created JAX-RS element or null if the given Java annotation
 *         is not a valid one.
 * @throws CoreException
 */
public static Set<IJaxrsElement> createElements(final IAnnotation javaAnnotation, final CompilationUnit ast,
        final JaxrsMetamodel metamodel, final IProgressMonitor progressMonitor) throws CoreException {
    // unsupported annotation (eg: on package declaration, or underlying java
    // element does not exist or not found) are ignored
    if (javaAnnotation != null) {
        switch (javaAnnotation.getParent().getElementType()) {
        case IJavaElement.TYPE:
            return internalCreateElements((IType) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        case IJavaElement.METHOD:
            return createElements((IMethod) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        case IJavaElement.FIELD:
            return createElements((IField) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        }
    }
    return Collections.emptySet();
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsMetamodel.java

License:Open Source License

/**
 * Process a single Java Element change/*  w ww.  j  a va2 s  . c  om*/
 * 
 * @param delta
 * @param progressMonitor
 * @throws CoreException
 */
public void processJavaElementChange(final JavaElementChangedEvent delta,
        final IProgressMonitor progressMonitor) throws CoreException {
    try {
        Logger.debug("Processing {}", delta);
        readWriteLock.writeLock().lock();
        final IJavaElement element = delta.getElement();
        final CompilationUnit ast = delta.getCompilationUnitAST();
        final int deltaKind = delta.getKind();
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            processProject(progressMonitor);
            break;
        case IJavaElement.ANNOTATION:
            processJavaAnnotationChange((IAnnotation) element, deltaKind, ast, progressMonitor);
            break;
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
        case IJavaElement.FIELD:
            processJavaElementChange(element, deltaKind, ast, progressMonitor);
            break;
        default:
            // ignore
            break;
        }
    } finally {
        this.initializing = false;
        progressMonitor.done();
        readWriteLock.writeLock().unlock();
        setBuildStatus(Status.OK_STATUS);
        Logger.debug("Done processing Java changes: " + getStatus());
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsMetamodel.java

License:Open Source License

/**
 * Search for the JAX-RS java-based elements matching the given
 * {@link IJavaElement} in the metamodel.
 * //  w ww.  ja  va 2  s. c  o  m
 * @param element
 * @param ast
 * @return the matching JAX-RS Elements or an empty set if no JAX-RS
 *         element matched in the metamodel.
 * @throws JavaModelException
 */
private List<IJaxrsElement> searchJaxrsElements(final IJavaElement element) throws JavaModelException {
    if (element == null) {
        return Collections.emptyList();
    }
    try {
        readWriteLock.readLock().lock();
        final List<IJaxrsElement> result = new ArrayList<IJaxrsElement>();
        final Term javaElementTerm = new Term(FIELD_JAVA_ELEMENT, Boolean.TRUE.toString());
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            final Term javaProjectIdentifier = new Term(FIELD_JAVA_PROJECT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, javaProjectIdentifier));
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            final Term packageFragmentRootIdentifier = new Term(FIELD_PACKAGE_FRAGMENT_ROOT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, packageFragmentRootIdentifier));
            break;
        case IJavaElement.COMPILATION_UNIT:
            final Term compilationUnitTerm = new Term(FIELD_COMPILATION_UNIT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, compilationUnitTerm));
            break;
        case IJavaElement.TYPE:
        case IJavaElement.FIELD:
        case IJavaElement.METHOD:
            final IJaxrsElement foundElement = this.elements.get(element.getHandleIdentifier());
            if (foundElement != null) {
                result.add(foundElement);
            }
            break;
        }
        return result;
    } finally {
        readWriteLock.readLock().unlock();
    }

}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsParameterAggregatorProperty.java

License:Open Source License

@Override
public void update(final IJavaElement javaElement, final CompilationUnit ast) throws CoreException {
    if (javaElement == null) {
        remove(FlagsUtils.computeElementFlags(this));
    } else {/*w w  w . j  av  a2 s . c o m*/
        // NOTE: the given javaElement may be an ICompilationUnit (after
        // resource change) !!
        switch (javaElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            final IType primaryType = ((ICompilationUnit) javaElement).findPrimaryType();
            if (primaryType != null) {
                final IMethod method = primaryType.getMethod(getJavaElement().getElementName(),
                        getJavaElement().getParameterTypes());
                update(method, ast);
            }
            break;
        case IJavaElement.METHOD:
            update(from((IMethod) javaElement, ast).buildTransient());
        }
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsResourceMethod.java

License:Open Source License

/**
 * Updates the current {@link JaxrsJavaElement} from the given
 * {@link IJavaElement}/*from ww  w  . j a  va  2s.c  o m*/
 * 
 * @param javaElement
 * @param ast
 * @return
 * @throws CoreException
 */
// TODO: add support for java method thrown exceptions..
@Override
public void update(final IJavaElement javaElement, final CompilationUnit ast) throws CoreException {
    if (javaElement == null) {
        remove(FlagsUtils.computeElementFlags(this));
    } else {
        // NOTE: the given javaElement may be an ICompilationUnit (after
        // resource change) !!
        switch (javaElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            final IType primaryType = ((ICompilationUnit) javaElement).findPrimaryType();
            if (primaryType != null) {
                final IMethod method = primaryType.getMethod(getJavaElement().getElementName(),
                        getJavaElement().getParameterNames());
                update(method, ast);
            }
            break;
        case IJavaElement.METHOD:
            update(from((IMethod) javaElement, ast, getMetamodel().findAllHttpMethodNames()).buildTransient());
        }
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsResourceProperty.java

License:Open Source License

@Override
public void update(final IJavaElement javaElement, final CompilationUnit ast) throws CoreException {
    if (javaElement == null) {
        remove(FlagsUtils.computeElementFlags(this));
    } else {//from  www  . j av  a  2 s .  c  o  m
        // NOTE: the given javaElement may be an ICompilationUnit (after
        // resource change) !!
        switch (javaElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            final IType primaryType = ((ICompilationUnit) javaElement).findPrimaryType();
            if (primaryType != null) {
                final IField field = primaryType.getField(getJavaElement().getElementName());
                update(field, ast);
            }
            break;
        case IJavaElement.METHOD:
            update(from((IMethod) javaElement, ast).buildTransient());
        }
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.search.JavaElementsSearcher.java

License:Open Source License

/**
 * Search for methods annotated with one of the given annotations, in the search scope.
 * //www. j  ava  2s  . c o  m
 * @param annotationNames
 *            the annotations fully qualified names
 * @param searchScope
 *            the search scope
 * @param progressMonitor
 *            the progress monitor
 * @return the matching methods
 * @throws CoreException
 *             in case of underlying exception
 */
private static Set<IMethod> searchForAnnotatedMethods(final List<String> annotationNames,
        final IJavaSearchScope searchScope, final IProgressMonitor progressMonitor) throws CoreException {
    final JavaMemberSearchResultCollector collector = new JavaMemberSearchResultCollector(IJavaElement.METHOD,
            searchScope);
    SearchPattern pattern = null;
    for (String annotationName : annotationNames) {
        // TODO : apply on METHOD instead of TYPE ?
        SearchPattern subPattern = SearchPattern.createPattern(annotationName,
                IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        if (pattern == null) {
            pattern = subPattern;
        } else {
            pattern = SearchPattern.createOrPattern(pattern, subPattern);
        }
    }
    // perform search, results are added/filtered by the custom
    // searchRequestor defined above
    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            searchScope, collector, progressMonitor);
    return collector.getResult();
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.DOMUtils.java

License:Open Source License

/**
 * Returns the ASTNode associated with the given java Element expectedType and found at the given location. This method will
 * perform faster as the initial parentNode is precisely defined (eg : TypeDeclaration or MethodDeclaration instead of
 * CompilationUnit)//from w w w. jav a  2s.  c  o m
 * 
 * @param compilationUnit
 * @param elementType
 * @param position
 * @return
 * @see {@link IJavaElement}
 */
//FIXME: this should be part of the visitor.
static ASTNode getASTNodeByTypeAndLocation(final ASTNode parentNode, final int expectedType,
        final int location) {
    switch (parentNode.getNodeType()) {
    case ASTNode.COMPILATION_UNIT:
        @SuppressWarnings("unchecked")
        final List<ASTNode> types = ((CompilationUnit) parentNode).types();
        for (ASTNode type : types) {
            if (nodeMatches(type, location)) {
                if (expectedType == IJavaElement.TYPE) {
                    return type;
                }
                // could also be ANNOTATION_TYPE_DECLARATION, which doesn't need to trigger recursive call to this
                // method.
                if (type.getNodeType() == CompilationUnit.TYPE_DECLARATION) {
                    return getASTNodeByTypeAndLocation(type, expectedType, location);
                }
            }
        }
        break;
    case ASTNode.TYPE_DECLARATION:
        final FieldDeclaration[] fieldDeclarations = ((TypeDeclaration) parentNode).getFields();
        for (FieldDeclaration fieldDeclaration : fieldDeclarations) {
            if (nodeMatches(fieldDeclaration, location)) {
                if (expectedType == IJavaElement.FIELD) {
                    return fieldDeclaration;
                }
            }
        }
        final MethodDeclaration[] methodDeclarations = ((TypeDeclaration) parentNode).getMethods();
        for (MethodDeclaration methodDeclaration : methodDeclarations) {
            if (nodeMatches(methodDeclaration, location)) {
                if (expectedType == IJavaElement.METHOD) {
                    return methodDeclaration;
                }
                return getASTNodeByTypeAndLocation(methodDeclaration, expectedType, location);

            }
        }
        return null;
    case ASTNode.METHOD_DECLARATION:
        @SuppressWarnings("unchecked")
        final List<ASTNode> parameters = ((MethodDeclaration) parentNode).parameters();
        for (ASTNode parameter : parameters) {
            if (nodeMatches(parameter, location)) {
                if (expectedType == IJavaElement.LOCAL_VARIABLE) {
                    return parameter;
                }
            }
        }
    }

    return null;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationLocator.java

License:Open Source License

public JavaAnnotationLocator(final IJavaElement parentElement, final int location) {
    this.parentMethod = (parentElement.getElementType() == IJavaElement.METHOD) ? (IMethod) parentElement
            : null;/*w  ww.  j  a  v  a2 s.  com*/
    this.location = location;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationsVisitor.java

License:Open Source License

/**
 * {@inheritDoc}//  w  w  w.  java 2s. c o  m
 * 
 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom. MethodDeclaration)
 */
@Override
public final boolean visit(final MethodDeclaration node) {
    if (memberType == IJavaElement.METHOD && node.getName().getFullyQualifiedName().equals(memberName)
            && matchesLocation(node)) {
        visitExtendedModifiers((List<?>) node.getStructuralProperty(MethodDeclaration.MODIFIERS2_PROPERTY));
        return false;
    }
    return true;
}