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

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

Introduction

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

Prototype

String getHandleIdentifier();

Source Link

Document

Returns a string representation of this element handle.

Usage

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <T extends IJavaElement> T findWorkingCopy(ICompilationUnit compilationUnit, T element)
        throws JavaModelException {
    if (element instanceof IAnnotation) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IAnnotatable) {
            for (IAnnotation a : ((IAnnotatable) parent).getAnnotations()) {
                if (a.getElementName().equals(element.getElementName()))
                    return (T) a;
            }/*w  w  w .  java 2s . c o m*/
        }
    } else if (element instanceof ILocalVariable && ((ILocalVariable) element).isParameter()) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IMethod) {
            for (ILocalVariable parameter : ((IMethod) parent).getParameters()) {
                if (parameter.getElementName().equals(element.getElementName())
                        && parameter.getTypeSignature().equals(((ILocalVariable) element).getTypeSignature()))
                    return (T) parameter;
            }
        }
    } else {
        IJavaElement[] elements = compilationUnit.findElements(element);
        if (elements != null) {
            for (IJavaElement e : elements) {
                if (e.getHandleIdentifier().equals(element.getHandleIdentifier()))
                    return (T) e;
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.windup.ui.internal.services.MarkerService.java

License:Open Source License

private void populateHints(ConfigurationElement configuration) throws CoreException {
    for (Issue issue : configuration.getWindupResult().getIssues()) {
        String absolutePath = issue.getFileAbsolutePath();
        IFile resource = getResource(absolutePath);
        String type = issue instanceof Classification ? WINDUP_CLASSIFICATION_MARKER_ID : WINDUP_HINT_MARKER_ID;
        IMarker marker = resource.createMarker(type);
        IJavaElement element = JavaCore.create(resource);
        if (element != null) {
            marker.setAttribute(ELEMENT_ID, element.getHandleIdentifier());
        }/*from   ww  w .ja  va2 s.c om*/
        marker.setAttribute(URI_ID, EcoreUtil.getURI(issue).toString());
        marker.setAttribute(IMarker.SEVERITY, MarkerUtil.convertSeverity(issue.getSeverity()));
        marker.setAttribute(SEVERITY, issue.getSeverity());
        marker.setAttribute(RULE_ID, issue.getRuleId());
        marker.setAttribute(EFFORT, issue.getEffort());

        if (issue instanceof Hint) {
            Hint hint = (Hint) issue;
            marker.setAttribute(IMarker.MESSAGE, hint.getHint());
            marker.setAttribute(IMarker.LINE_NUMBER, hint.getLineNumber());

            marker.setAttribute(TITLE, hint.getTitle());
            marker.setAttribute(HINT, hint.getHint());
            marker.setAttribute(LINE, hint.getLineNumber());
            marker.setAttribute(COLUMN, hint.getColumn());
            marker.setAttribute(LENGTH, hint.getLength());
            marker.setAttribute(SOURCE_SNIPPET, hint.getSourceSnippet());

            populateLinePosition(marker, hint.getLineNumber(), new File(hint.getFileAbsolutePath()));
        }

        else {
            Classification classification = (Classification) issue;
            marker.setAttribute(IMarker.MESSAGE, classification.getClassification());
            marker.setAttribute(CLASSIFICATION, classification.getClassification());
            marker.setAttribute(DESCRIPTION, classification.getDescription());

            marker.setAttribute(IMarker.LINE_NUMBER, 1);
            marker.setAttribute(IMarker.CHAR_START, 0);
            marker.setAttribute(IMarker.CHAR_END, 0);
        }

        marker.setAttribute(IMarker.USER_EDITABLE, true);
    }
}

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.
 * //from w w  w.  ja v a 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.JaxrsMetamodel.java

License:Open Source License

/**
 * Searches and returns all JAX-RS Java-based Elements matching the given
 * {@link IJavaElement}, which can be {@link Annotation} {@link IProject},
 * {@link IPackageFragmentRoot}, {@link ICompilationUnit} or an
 * {@link IMember}//from w  w w  . j a va  2  s  . c  o  m
 * 
 * @param javaElement
 *            the java element
 * 
 * @return the JAX-RS Elements matching the given Java Element or empty list
 *         if none matches.
 */
public <T extends IJaxrsElement> Set<T> findElements(final IJavaElement javaElement) {
    if (javaElement == null) {
        return Collections.emptySet();
    }
    try {
        readWriteLock.readLock().lock();
        final String identifier = javaElement.getHandleIdentifier();
        switch (javaElement.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            return searchJaxrsElements(new Term(FIELD_JAVA_PROJECT_IDENTIFIER, identifier));
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return searchJaxrsElements(new Term(FIELD_PACKAGE_FRAGMENT_ROOT_IDENTIFIER, identifier));
        case IJavaElement.COMPILATION_UNIT:
            return searchJaxrsElements(new Term(FIELD_COMPILATION_UNIT_IDENTIFIER, identifier));
        default:
            return searchJaxrsElements(LuceneDocumentFactory.getIdentifierTerm(javaElement));
        }
    } finally {
        readWriteLock.readLock().unlock();
    }
}

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

License:Open Source License

/**
 * Searches and retrieves all the {@link JaxrsEndpoint}s in the metamodel
 * that use the given {@link IJavaElement}
 * /*w w w.  j a  v  a2s.  c  o m*/
 * @param resourceMethod
 *            the resource method used by the searched Endpoints
 * @return a collection containing zero or more matches, or
 *         empty list if the input was {@code null}.
 */
public Set<JaxrsEndpoint> findEndpoints(final IJavaElement element) {
    if (element == null) {
        return Collections.emptySet();
    }
    try {
        readWriteLock.readLock().lock();
        final Term projectTerm = new Term(FIELD_JAVA_PROJECT_IDENTIFIER,
                getJavaProject().getHandleIdentifier());
        final Term categoryTerm = new Term(FIELD_TYPE, EnumElementCategory.ENDPOINT.toString());
        final Term javaElementTerm = new Term(FIELD_JAVA_ELEMENT, element.getHandleIdentifier());
        return searchJaxrsEndpoints(projectTerm, categoryTerm, javaElementTerm);
    } finally {
        readWriteLock.readLock().unlock();
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.indexation.LuceneDocumentFactory.java

License:Open Source License

/**
 * Returns the given {@link IJavaElement#getHandleIdentifier()} value or
 * <code>null</null> if the given javaElement was null;
 * //w  w  w.j av  a 2 s  . c o  m
 * @param javaElement
 * @return the given element's handleIdentifier or null;
 */
private static String getHandleIdentifier(final IJavaElement javaElement) {
    if (javaElement == null) {
        return null;
    }
    return javaElement.getHandleIdentifier();
}

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

License:Open Source License

/**
 * Returns the identifier value to use in index {@link Field} and query
 * {@link Term}s for the given {@link IJavaElement}.
 * /*from  w w w.j  ava2 s .c om*/
 * @param element
 *            the element to identify
 * @return the identifier value
 */
private static String getIdentifierValue(final IJavaElement element) {
    return IndexedObjectType.JAX_RS_ELEMENT.getPrefix() + element.getHandleIdentifier();
}

From source file:org.springframework.ide.eclipse.core.model.java.JavaModelSourceLocation.java

License:Open Source License

public JavaModelSourceLocation(IJavaElement type) throws JavaModelException {
    this.handleIdentifier = type.getHandleIdentifier();
    this.lineNumber = JdtUtils.getLineNumber(JdtUtils.getByHandle(handleIdentifier));
}

From source file:org.springframework.tooling.jdt.ls.commons.java.JavaData.java

License:Open Source License

private void fillJavaElementData(IJavaElement element, JavaElementData data) {
    data.setName(element.getElementName());
    data.setHandleIdentifier(element.getHandleIdentifier());
    if (labelProvider != null) {
        data.setLabel(labelProvider.apply(element));
    }/*from www.jav a 2 s .  co  m*/
}

From source file:org.springsource.ide.eclipse.gradle.core.test.util.JUnitLaunchConfigUtil.java

License:Open Source License

public static ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element)
        throws CoreException {
    final String testName;
    final String mainTypeQualifiedName;
    final String containerHandleId;

    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT: {
        String name = JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_FULLY_QUALIFIED);
        containerHandleId = element.getHandleIdentifier();
        mainTypeQualifiedName = EMPTY_STRING;
        testName = name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
    }/*from   w ww  .  j  av  a2s  . c  om*/
        break;
    case IJavaElement.TYPE: {
        containerHandleId = EMPTY_STRING;
        mainTypeQualifiedName = ((IType) element).getFullyQualifiedName('.'); // don't replace, fix for binary inner types
        testName = element.getElementName();
    }
        break;
    case IJavaElement.METHOD: {
        IMethod method = (IMethod) element;
        containerHandleId = EMPTY_STRING;
        mainTypeQualifiedName = method.getDeclaringType().getFullyQualifiedName('.');
        testName = method.getDeclaringType().getElementName() + '.' + method.getElementName();
    }
        break;
    default:
        throw new IllegalArgumentException(
                "Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
    }

    String testKindId = TestKindRegistry.getContainerTestKindId(element);

    ILaunchConfigurationType configType = getLaunchManager()
            .getLaunchConfigurationType(JUnitLaunchConfigurationConstants.ID_JUNIT_APPLICATION);
    ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
            getLaunchManager().generateLaunchConfigurationName(testName));

    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainTypeQualifiedName);
    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
            element.getJavaProject().getElementName());
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, containerHandleId);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId);
    JUnitMigrationDelegate.mapResources(wc);
    AssertionVMArg.setArgDefault(wc);
    if (element instanceof IMethod) {
        wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, element.getElementName()); // only set for methods
    }
    return wc;
}