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

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

Introduction

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

Prototype

int COMPILATION_UNIT

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

Click Source Link

Document

Constant representing a Java compilation unit.

Usage

From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentSymbolHandler.java

License:Open Source License

public static int mapKind(IJavaElement element) {
    //      /**/*from   w ww  . jav  a  2s. co m*/
    //      * A symbol kind.
    //      */
    //      export enum SymbolKind {
    //        File = 1,
    //        Module = 2,
    //        Namespace = 3,
    //        Package = 4,
    //        Class = 5,
    //        Method = 6,
    //        Property = 7,
    //        Field = 8,
    //        Constructor = 9,
    //        Enum = 10,
    //        Interface = 11,
    //        Function = 12,
    //        Variable = 13,
    //        Constant = 14,
    //        String = 15,
    //        Number = 16,
    //        Boolean = 17,
    //        Array = 18,
    //      }
    switch (element.getElementType()) {
    case IJavaElement.ANNOTATION:
        return 7; // TODO: find a better mapping 
    case IJavaElement.CLASS_FILE:
    case IJavaElement.COMPILATION_UNIT:
        return 1;
    case IJavaElement.FIELD:
        return 8;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
        return 2;
    case IJavaElement.INITIALIZER:
        return 9;
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        return 13;
    case IJavaElement.METHOD:
        return 12;
    case IJavaElement.PACKAGE_DECLARATION:
        return 3;
    case IJavaElement.TYPE:
        try {
            return (((IType) element).isInterface() ? 11 : 5);
        } catch (JavaModelException e) {
            return 5; //fallback 
        }
    }
    return 15;
}

From source file:org.jboss.tools.vscode.java.internal.handlers.NavigateToDefinitionHandler.java

License:Open Source License

private Location computeDefinitonNavigation(ITypeRoot unit, int line, int column) {
    try {// ww w .j  a va2  s  .  c o  m
        IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0);

        if (elements == null || elements.length != 1)
            return null;
        IJavaElement element = elements[0];
        ICompilationUnit compilationUnit = (ICompilationUnit) element
                .getAncestor(IJavaElement.COMPILATION_UNIT);
        IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
        if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) {
            return JDTUtils.toLocation(element);
        }
        return null;

    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Problem with codeSelect for" + unit.getElementName(), e);
    }
    return null;
}

From source file:org.jboss.tools.vscode.java.internal.handlers.ReferencesHandler.java

License:Open Source License

@Override
public List<org.jboss.tools.langs.Location> handle(org.jboss.tools.langs.ReferenceParams param) {
    SearchEngine engine = new SearchEngine();

    String uri = param.getTextDocument().getUri();
    if (MODE_SOURCEGRAPH) {
        // SOURCEGRAPH: URI is expected to be in form file:///foo/bar,
        // but we need to construct absolute file URI
        uri = new File(connection.getWorkpaceRoot(), uri.substring(8)).toURI().toString();
    }/*from   w w  w. j av a  2 s  .  co  m*/

    ITypeRoot unit = JDTUtils.resolveTypeRoot(uri);

    if (unit == null) {
        return null;
    }

    try {
        IJavaElement elementToSearch = findElementAtSelection(unit, param.getPosition().getLine().intValue(),
                param.getPosition().getCharacter().intValue());

        if (elementToSearch == null)
            return Collections.emptyList();

        SearchPattern pattern = SearchPattern.createPattern(elementToSearch, IJavaSearchConstants.REFERENCES);
        List<org.jboss.tools.langs.Location> locations = new ArrayList<org.jboss.tools.langs.Location>();
        engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                createSearchScope(), new SearchRequestor() {

                    @Override
                    public void acceptSearchMatch(SearchMatch match) throws CoreException {
                        Object o = match.getElement();
                        if (o instanceof IJavaElement) {
                            IJavaElement element = (IJavaElement) o;
                            ICompilationUnit compilationUnit = (ICompilationUnit) element
                                    .getAncestor(IJavaElement.COMPILATION_UNIT);
                            Location location = null;
                            if (compilationUnit != null) {
                                location = JDTUtils.toLocation(compilationUnit, match.getOffset(),
                                        match.getLength());
                            } else {
                                IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
                                if (cf != null && cf.getSourceRange() != null) {
                                    location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength());
                                }
                            }
                            if (location != null)
                                if (MODE_SOURCEGRAPH) {
                                    // SOURCEGRAPH: transforming location's URI back from file://WORKSPACE/foo/bar to file:///foo/bar
                                    File file = new File(URI.create(location.getUri()).getPath());
                                    File root = new File(connection.getWorkpaceRoot());
                                    location.setUri("file:///" + root.toPath().relativize(file.toPath())
                                            .toString().replace(File.separatorChar, '/'));
                                }
                            locations.add(location);

                        }

                    }
                }, new NullProgressMonitor());

        return locations;
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Find references failure ", e);
    }
    return null;
}

From source file:org.jboss.tools.vscode.java.internal.JDTUtils.java

License:Open Source License

/**
 * Creates a location for a given java element.
 * Element can be a {@link ICompilationUnit} or {@link IClassFile}
 *
 * @param element//from   ww w  .  ja va2 s  . c o  m
 * @return location or null
 * @throws JavaModelException
 */
public static Location toLocation(IJavaElement element) throws JavaModelException {
    ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
    IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
    if (unit == null && cf == null) {
        return null;
    }
    if (element instanceof ISourceReference) {
        ISourceRange nameRange = ((ISourceReference) element).getNameRange();
        if (cf == null) {
            return toLocation(unit, nameRange.getOffset(), nameRange.getLength());
        } else {
            return toLocation(cf, nameRange.getOffset(), nameRange.getLength());
        }
    }
    return null;
}

From source file:org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils.java

License:Open Source License

public static ICompilationUnit findUnitByFileName(IJavaElement javaElem, String filePath) throws Exception {
    ICompilationUnit unit = null;//w  ww. ja v  a2 s . c  o  m

    if (!javaElem.getOpenable().isOpen()) {
        javaElem.getOpenable().open(null);
    }

    IJavaElement[] elems = null;

    if (javaElem instanceof IParent) {
        IParent parent = (IParent) javaElem;
        elems = parent.getChildren();
    }

    if (elems == null) {
        return null;
    }

    for (IJavaElement elem : elems) {
        if (elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
            IPackageFragmentRoot root = (IPackageFragmentRoot) elem;

            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                unit = findUnitByFileName(elem, filePath);

                if (unit != null) {
                    return unit;
                }
            }
        } else if ((elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT)
                || (elem.getElementType() == IJavaElement.JAVA_PROJECT)) {
            unit = findUnitByFileName(elem, filePath);

            if (unit != null) {
                return unit;
            }
        } else if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ICompilationUnit compUnit = (ICompilationUnit) elem;

            if (compUnit.getPath().toString().equals(filePath)) {
                compUnit.open(null);

                return compUnit;
            }
        }
    }

    return null;
}

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

License:Open Source License

/**
 * Builder initializer/* w  ww  . j  ava2s.co  m*/
 * 
 * @param javaElement
 *            the underlying {@link IJavaElement} that on which this JAX-RS
 *            Element will be built.
 * @return the Builder
 * @throws JavaModelException
 */
public static Builder from(final IJavaElement javaElement) throws JavaModelException {
    final CompilationUnit ast = JdtUtils.parse(javaElement, new NullProgressMonitor());
    switch (javaElement.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        return new Builder(((ICompilationUnit) javaElement).findPrimaryType(), ast);
    case IJavaElement.TYPE:
        return new Builder((IType) javaElement, ast);
    }
    return null;
}

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

License:Open Source License

/**
 * Builder initializer//  w  w w  .  j  a  v  a  2s  . co m
 * 
 * @param javaElement
 *            the underlying {@link IJavaElement} that on which this JAX-RS
 *            Element will be built.
 * @param ast
 *            the associated AST
 * @return the Builder
 * @throws JavaModelException
 */
public static Builder from(final IJavaElement javaElement, final CompilationUnit ast) {
    switch (javaElement.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        return new Builder(((ICompilationUnit) javaElement).findPrimaryType(), ast);
    case IJavaElement.TYPE:
        return new Builder((IType) javaElement, ast);
    }
    return null;
}

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

License:Open Source License

/**
 * Process a single Java Element change//www.ja v a 2s.co m
 * 
 * @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

/**
 * Process the givne {@link IJavaElement} to see if it can be a JAX-RS element
 * @param javaElement// www .  j  a v  a 2 s  .  c  o m
 * @param deltaKind
 * @param progressMonitor
 * @throws CoreException
 * @throws JavaModelException
 */
private void processJavaElement(final IJavaElement javaElement, final int deltaKind,
        final IProgressMonitor progressMonitor) throws CoreException, JavaModelException {
    final Set<JaxrsJavaElement<?>> matchingElements = findElements(javaElement);
    switch (deltaKind) {
    case ADDED:
        JaxrsElementFactory.createElements(javaElement, JdtUtils.parse(javaElement, progressMonitor), this,
                progressMonitor);
        break;
    case CHANGED:
        final CompilationUnit ast = JdtUtils.parse(javaElement, progressMonitor);
        if (matchingElements.isEmpty()) {
            JaxrsElementFactory.createElements(javaElement, ast, this, progressMonitor);
        } else {
            for (JaxrsJavaElement<?> element : matchingElements) {
                // only working on JAX-RS elements bound to IType, not
                // IFields nor IMethods
                // those elements will internally update their own
                // children elements based on IMethods and IFields
                if (element.getJavaElement().getElementType() == IJavaElement.TYPE
                        && javaElement.getElementType() == IJavaElement.TYPE) {
                    element.update(javaElement, ast);
                } else if (element.getJavaElement().getElementType() == IJavaElement.TYPE
                        && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    final ICompilationUnit compilationUnit = (ICompilationUnit) javaElement;
                    final IType type = JdtUtils.resolveType(compilationUnit,
                            element.getJavaElement().getHandleIdentifier());
                    if (type != null) {
                        element.update(type, ast);
                    } else {
                        element.remove(FlagsUtils.computeElementFlags(element));
                    }
                }
            }
        }
        break;
    case REMOVED:
        for (JaxrsJavaElement<?> element : matchingElements) {
            element.remove(FlagsUtils.computeElementFlags(element));
        }
        break;
    }
}

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   ww  w.jav a2s.  c om
 * @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();
    }

}