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.ws.jaxrs.core.jdt.JdtUtils.java

License:Open Source License

/**
 * Parse the compilation unit of the given {@link IJavaElement} if it is a
 * {@link IMember} or a {@link ICompilationUnit}, null otherwise.
 * //from  w  w  w  .  j av  a 2s .co m
 * @param javaElement
 * @param progressMonitor
 * @return the compilation unit or null if the given java element has no
 *         compilation unit (ex: package fragment root).
 * @throws JavaModelException
 */
public static CompilationUnit parse(final IJavaElement javaElement, IProgressMonitor progressMonitor)
        throws JavaModelException {
    if (javaElement instanceof IMember) {
        return parse(((IMember) javaElement).getCompilationUnit(), progressMonitor);
    } else if (javaElement instanceof IAnnotation) {
        return parse(((IAnnotation) javaElement).getAncestor(IJavaElement.COMPILATION_UNIT), progressMonitor);

    } else if (javaElement instanceof ICompilationUnit) {
        return parse((ICompilationUnit) javaElement, progressMonitor);
    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.ui.internal.validation.ValidationUtils.java

License:Open Source License

/**
 * @param annotation/*from ww w  .  j a va2s  .  c o  m*/
 * @return
 * @throws JavaModelException
 * @throws CoreException
 */
public static IJavaCompletionProposal[] getJavaCompletionProposals(final Annotation annotation)
        throws JavaModelException, CoreException {
    final IAnnotation javaAnnotation = annotation.getJavaAnnotation();
    final ICompilationUnit compilationUnit = (ICompilationUnit) javaAnnotation
            .getAncestor(IJavaElement.COMPILATION_UNIT);
    final ISourceRange sourceRange = javaAnnotation.getSourceRange();
    final IInvocationContext invocationContext = getInvocationContext(compilationUnit, sourceRange.getOffset(),
            sourceRange.getLength());
    final IProblemLocation[] problemLocations = getProblemLocations(javaAnnotation);
    final IJavaCompletionProposal[] proposals = new JaxrsMarkerResolutionGenerator()
            .getCorrections(invocationContext, problemLocations);
    return proposals;
}

From source file:org.jboss.tools.ws.jaxrs.ui.internal.validation.ValidationUtils.java

License:Open Source License

private static IProblemLocation[] getProblemLocations(final IAnnotation annotation) throws JavaModelException {
    final CompilationUnit ast = JdtUtils.parse(annotation.getAncestor(IJavaElement.COMPILATION_UNIT), null);
    final IProblem[] problems = ast.getProblems();
    for (IProblem problem : problems) {
        if ((problem.getSourceStart() >= annotation.getSourceRange().getOffset())
                && (problem.getSourceStart() <= (annotation.getSourceRange().getLength()
                        + annotation.getSourceRange().getOffset()))) {
            return new IProblemLocation[] { new ProblemLocation(problem) };
        }/*from ww w .ja v a2s  .  c  o m*/
    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.ui.navigation.JaxrsNameBindingHyperlinkDetector.java

License:Open Source License

/**
 * (non-Javadoc)//  www  .ja  va 2s .  com
 * 
 * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer,
 *      org.eclipse.jface.text.IRegion, boolean)
 */
@Override
public IHyperlink[] detectHyperlinks(final ITextViewer textViewer, final IRegion region,
        final boolean canShowMultipleHyperlinks) {
    final ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !(textEditor instanceof JavaEditor)) {
        return null;
    }
    final int offset = region.getOffset();
    final IJavaElement input = EditorUtility.getActiveEditorJavaInput();
    if (input == null) {
        return null;
    }
    final JaxrsMetamodel metamodel = getMetamodel(input);
    if (metamodel == null) {
        return null;
    }
    final IRegion wordRegion = getCurrentWordRegion(textEditor, offset);
    if (wordRegion == null || wordRegion.getLength() == 0) {
        return null;
    }
    final IJavaElement[] selectedJavaElements = getSelectedElements(input, wordRegion);

    final List<IJaxrsJavaElement> targets = findTargets(metamodel, selectedJavaElements,
            (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT));
    if (targets != null && !targets.isEmpty()) {
        final IHyperlink[] result = new IHyperlink[targets.size()];
        for (int i = 0; i < targets.size(); i++) {
            result[i] = new JaxrsNameBindingAnnotationHyperlink(targets.get(i), wordRegion);
        }
        return result;
    }
    return null;

}

From source file:org.jboss.tools.ws.jaxrs.ui.navigation.JaxrsNameBindingHyperlinkDetector.java

License:Open Source License

/**
 * If the given selectedJavaElement match a custom Name Binding annotation
 * defined in the given {@link JaxrsMetamodel}, then return the list of
 * associated elements with this name binding.
 * //from   w w  w. java2 s . c o  m
 * @param metamodel
 *            the JAX-RS Metamodel associated with the given element in the
 *            current text editor
 * @param selectedJavaElements
 *            the selected java elements
 * @param currentCompilationUnit
 *            the {@link ICompilationUnit} opened in the current text editor
 * @return the list of target {@link IJavaElement} or empty list if none
 *         match.
 */
private List<IJaxrsJavaElement> findTargets(final JaxrsMetamodel metamodel,
        final IJavaElement[] selectedJavaElements, final ICompilationUnit currentCompilationUnit) {
    final List<IJaxrsJavaElement> targetElements = new ArrayList<IJaxrsJavaElement>();
    for (IJavaElement selectedJavaElement : selectedJavaElements) {
        final IJaxrsElement associatedJaxrsElement = metamodel.findElement(selectedJavaElement);
        if (associatedJaxrsElement != null
                && associatedJaxrsElement.getElementKind() == EnumElementKind.NAME_BINDING) {
            final JaxrsNameBinding nameBinding = (JaxrsNameBinding) associatedJaxrsElement;
            final Collection<IJaxrsElement> matchingElements = metamodel
                    .findElementsByAnnotation(nameBinding.getJavaClassName());
            for (IJaxrsElement matchingElement : matchingElements) {
                final IJavaElement matchingJavaElement = ((IJaxrsJavaElement) matchingElement).getJavaElement();
                // skip if the matching/target Java element is part of the current compilation unit
                if (!matchingJavaElement.getAncestor(IJavaElement.COMPILATION_UNIT)
                        .equals(currentCompilationUnit)) {
                    targetElements.add((IJaxrsJavaElement) matchingElement);
                }
            }
        }
    }
    return targetElements;
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.JaxrsMarkerResolutionGenerator.java

License:Open Source License

private IJavaCompletionProposal generateMissingAttributesCompletionProposal(final String qualifiedName,
        final IProblemLocation problemLocation, final IAnnotation annotation) throws JavaModelException {
    if (qualifiedName == null || annotation == null) {
        return null;
    }/*from  ww w . j  a v  a  2 s.  c  om*/
    final IJaxrsElement jaxrsElement = findJaxrsElement(annotation.getParent());
    // skip if the problem is not linked to a JAX-RS element.
    if (jaxrsElement == null) {
        return null;
    }
    final ICompilationUnit compilationUnit = (ICompilationUnit) annotation
            .getAncestor(IJavaElement.COMPILATION_UNIT);
    if (qualifiedName.equals(JaxrsClassnames.TARGET)) {
        switch (jaxrsElement.getElementKind()) {
        case HTTP_METHOD:
            return new AddHttpMethodTargetValuesCompletionProposal(compilationUnit,
                    findEffectiveSourceRange(compilationUnit, problemLocation));
        case NAME_BINDING:
            return new AddHttpMethodTargetValuesCompletionProposal(compilationUnit,
                    findEffectiveSourceRange(compilationUnit, problemLocation));
        default:
            return null;
        }
    } else if (qualifiedName.equals(JaxrsClassnames.RETENTION)) {
        switch (jaxrsElement.getElementKind()) {
        case HTTP_METHOD:
            return new AddHttpMethodRetentionValueCompletionProposal(compilationUnit,
                    findEffectiveSourceRange(compilationUnit, problemLocation));
        case NAME_BINDING:
            return new AddNameBindingRetentionValueCompletionProposal(compilationUnit,
                    findEffectiveSourceRange(compilationUnit, problemLocation));
        default:
            return null;
        }
    } else if (qualifiedName.equals(JaxrsClassnames.HTTP_METHOD)) {
        final IJavaElement httpMethodType = annotation.getAncestor(IJavaElement.TYPE);
        return new AddHttpMethodValueCompletionProposal(compilationUnit,
                "\"" + httpMethodType.getElementName() + "\"",
                findEffectiveSourceRange(compilationUnit, problemLocation));
    }
    return null;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getSourceFolderFromSelection() {
    String defaultFolder = DEFAULT_SOURCE_FOLDER;

    if (selection.isEmpty()) {
        return defaultFolder;
    }/*  w w  w. j a  v a  2s . c  o m*/

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            return getDefaultSrcByProject((IJavaProject) selectedJavaElement);

        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return selectedJavaElement.getPath().toPortableString();

        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.COMPILATION_UNIT:
            return selectedJavaElement.getPath().uptoSegment(2).toPortableString();
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FOLDER:
            return getDefaultSrcByProject(JavaCore.create(selectedResource.getProject()));

        case IResource.FILE:
            return selectedResource.getFullPath().uptoSegment(2).toPortableString();
        }
    }

    return defaultFolder;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getPackageFromSelection() {
    String defaultPackage = DEFAULT_PACKAGE;

    if (selection.isEmpty()) {
        return defaultPackage;
    }/*from w ww . jav a  2 s. co m*/

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return selectedJavaElement.getElementName();

        case IJavaElement.COMPILATION_UNIT:
            try {
                return selectedJavaElement.getJavaProject()
                        .findPackageFragment(selectedJavaElement.getPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FILE:
            try {
                return JavaCore.create(selectedResource.getProject())
                        .findPackageFragment(
                                selectedResource.getFullPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    }

    return defaultPackage;
}

From source file:org.jnario.spec.ui.wizards.NewSpecWizardPageOne.java

License:Open Source License

public void init(IStructuredSelection selection) {
    IJavaElement element = getInitialJavaElement(selection);

    initContainerPage(element);/*  w w  w .j  a va  2  s .c o m*/
    initTypePage(element);
    // put default class to test
    if (element != null) {
        IType classToTest = null;
        // evaluate the enclosing type
        IType typeInCompUnit = (IType) element.getAncestor(IJavaElement.TYPE);
        if (typeInCompUnit != null) {
            if (typeInCompUnit.getCompilationUnit() != null) {
                classToTest = typeInCompUnit;
            }
        } else {
            ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null)
                classToTest = cu.findPrimaryType();
            else {
                if (element instanceof IClassFile) {
                    try {
                        IClassFile cf = (IClassFile) element;
                        if (cf.isStructureKnown())
                            classToTest = cf.getType();
                    } catch (JavaModelException e) {
                        JUnitPlugin.log(e);
                    }
                }
            }
        }
        if (classToTest != null) {
            try {
                if (!CoreTestSearchEngine.isTestImplementor(classToTest)) {
                    setClassUnderTest(classToTest.getFullyQualifiedName('.'));
                }
            } catch (JavaModelException e) {
                JUnitPlugin.log(e);
            }
        }
    }

    updateStatus(getStatusList());
}

From source file:org.limy.eclipse.common.jdt.LimyJavaUtils.java

License:Open Source License

/**
 * javaElement SJavat@C? results i[?B/*w ww  .  j  a va  2 s.  com*/
 * @param results i[?
 * @param javaElement ?[gJavavf
 * @param visitor IJavaResourceVisitor
 * @throws CoreException RAO
 */
public static void appendAllJavas(Collection<IJavaElement> results, IJavaElement javaElement)
        throws CoreException {

    if (javaElement == null || javaElement.getResource() == null) {
        // JarG?g???Aresource = null 
        return;
    }

    // Javav?WFNg?A\?[XpX?AJavapbP?[W
    appendForIParent(results, javaElement/*, visitor*/);

    // JavapbP?[WTupbP?[W
    if (javaElement instanceof IPackageFragment) {
        appendForIPackageFragment(results, (IPackageFragment) javaElement/*, visitor*/);
    }

    // JavaNX?A?\bh`?AtB?[h`
    int type = javaElement.getElementType();
    if (type == IJavaElement.IMPORT_DECLARATION || type == IJavaElement.PACKAGE_DECLARATION
            || type == IJavaElement.COMPILATION_UNIT || type == IJavaElement.TYPE || type == IJavaElement.METHOD
            || type == IJavaElement.FIELD) {

        results.add(javaElement);
    }
}