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.arquillian.ui.internal.launcher.ArquillianLaunchConfigurationTab.java

License:Open Source License

private void initializeTestAttributes(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
    if (javaElement != null && javaElement.getElementType() < IJavaElement.COMPILATION_UNIT)
        initializeTestContainer(javaElement, config);
    else/*from   w  w  w.java  2  s . c om*/
        initializeTestType(javaElement, config);
}

From source file:org.jboss.tools.arquillian.ui.internal.launcher.ArquillianLaunchShortcut.java

License:Open Source License

private void launch(Object[] elements, String mode) {
    try {/*  ww w  . ja  v a2  s. c  o m*/
        IJavaElement elementToLaunch = null;

        if (elements.length == 1) {
            Object selected = elements[0];
            if (!(selected instanceof IJavaElement) && selected instanceof IAdaptable) {
                selected = ((IAdaptable) selected).getAdapter(IJavaElement.class);
            }
            if (selected instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) selected;
                switch (element.getElementType()) {
                case IJavaElement.JAVA_PROJECT:
                case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                case IJavaElement.PACKAGE_FRAGMENT:
                    IJavaProject javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.TYPE:
                    IType type = (IType) element;
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true)) {
                        elementToLaunch = type;
                    }
                case IJavaElement.METHOD:
                    javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.CLASS_FILE:
                    type = ((IClassFile) element).getType();
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true, false)) {
                        elementToLaunch = type;
                    }
                    break;
                case IJavaElement.COMPILATION_UNIT:
                    elementToLaunch = findTypeToLaunch((ICompilationUnit) element, mode);
                    break;
                }
            }
        }
        if (elementToLaunch == null) {
            showNoTestsFoundDialog();
            return;
        }
        performLaunch(elementToLaunch, mode);
    } catch (InterruptedException e) {
        // OK, silently move on
    } catch (CoreException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    } catch (InvocationTargetException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.wizards.NewArquillianJUnitTestCasePageOne.java

License:Open Source License

/**
 * Initialized the page with the current selection
 * @param selection The selection/*from w ww.  jav a 2 s  .  c o  m*/
 */
public void init(IStructuredSelection selection) {
    IJavaElement element = getInitialJavaElement(selection);

    initContainerPage(element);
    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) {
                        ArquillianUIActivator.log(e);
                    }
                }
            }
        }
        if (classToTest != null) {
            try {
                if (!CoreTestSearchEngine.isTestImplementor(classToTest)) {
                    setClassUnderTest(classToTest.getFullyQualifiedName('.'));
                }
            } catch (JavaModelException e) {
                ArquillianUIActivator.log(e);
            }
        }
    }

    restoreWidgetValues();
    updateStatus(getStatusList());
}

From source file:org.jboss.tools.common.core.resources.JavaFileAdaptable.java

License:Open Source License

public Object getAdapter(Class adapter) {
    if (adapter == ICompilationUnit.class) {
        IJavaElement element = EclipseResourceUtil.findJavaElement(this);
        if (element != null && element.getElementType() == IJavaElement.COMPILATION_UNIT)
            return element;
    }/*from   w w  w .j a  v a2s  . co  m*/
    return super.getAdapter(adapter);
}

From source file:org.jboss.tools.common.java.impl.ValueResolver.java

License:Open Source License

/**
 * For a complex expression returns source string if it is 
 * available while the result of calculation is to be requested 
 * by getConstant().//  w w w .  ja  v a2  s  .  c o  m
 * 
 * For a reference to a constant returns resolved qualified name
 * while the constant value is to be requested by getConstant().
 * 
 * Otherwise, returns pair.getValue(). If the value is an array,
 * then for each element that is a reference to a constant
 * that element is replaced with resolved qualified name 
 * of the reference.
 * 
 * @param pair
 * @return
 */
public Object resolvePair(IMemberValuePair pair) {
    constant = null;
    Object value = pair.getValue();
    int k = pair.getValueKind();
    if (k == IMemberValuePair.K_QUALIFIED_NAME || k == IMemberValuePair.K_SIMPLE_NAME
            || (value instanceof Object[] && k == IMemberValuePair.K_UNKNOWN)) {
        if (element != null && element.getAncestor(IJavaElement.COMPILATION_UNIT) instanceof ICompilationUnit) {
            value = resolve(value);
        }
    } else if (k == IMemberValuePair.K_UNKNOWN && value == null) {
        if (element instanceof ISourceReference) {
            try {
                String source = getExpressionForName(pair.getMemberName());
                if (source != null) {
                    Object c = resolveExpression(source);
                    if (c != null) {
                        value = source;
                        constant = c;
                    }
                }
            } catch (CoreException e) {
                CommonCorePlugin.getDefault().logError(e);
            }
        }
    }
    return value;
}

From source file:org.jboss.tools.common.java.test.FakeAnnotationImpl.java

License:Open Source License

@Override
public IJavaElement getAncestor(int ancestorType) {
    if (ancestorType == IJavaElement.COMPILATION_UNIT) {
        return new CompilationUnit(null, null, null) {
            public IImportDeclaration[] getImports() throws JavaModelException {
                return new IImportDeclaration[0];
            }/*from   w  ww .  j a  va 2 s . c o m*/
        };
    }
    if (ancestorType == IJavaElement.TYPE) {
        class FakeSourceType extends SourceType {
            FakeSourceType() {
                super(null, "FakeType");
            }
        }
        return new FakeSourceType();
    }
    return null;
}

From source file:org.jboss.tools.common.validation.java.JavaDirtyRegionProcessor.java

License:Open Source License

private boolean isJavaElementValidationRequired() {
    ICompilationUnit unit = fReporter.getCompilationUnit();
    if (unit == null)
        return false;

    boolean result = false;
    boolean atLeastOneElementIsProcessed = false;

    int position = fStartRegionToProcess;
    try {//from  w w  w . j a va 2 s  .c o m
        unit = unit.getWorkingCopy(null);
        IJavaElement element = null;
        while (position >= 0 && (element = unit.getElementAt(position--)) == null)
            ;

        if (position < 0)
            position = 0;

        ITypedRegion[] partitions = computePartitioning(position, fEndPartitionsToProcess - position);

        ITypedRegion startPartition = findPartitionByOffset(partitions, position);
        ITypedRegion endPartition = (startPartition != null && fEndRegionToProcess >= startPartition.getOffset()
                && fEndRegionToProcess < startPartition.getOffset() + startPartition.getLength())
                        ? startPartition
                        : findPartitionByOffset(partitions, fEndRegionToProcess);

        if (startPartition != null && startPartition.equals(endPartition)
                && !isProcessingRequiredForPartition(startPartition)) {
            return false;
        }

        while (position <= fEndRegionToProcess) {
            ITypedRegion partition = findPartitionByOffset(partitions, position);
            if (!isProcessingRequiredForPartition(partition)) {
                position = partition.getOffset() + partition.getLength();
                continue;
            }

            element = unit.getElementAt(position);
            if (element == null) {
                position++;
                continue;
            }

            atLeastOneElementIsProcessed = true;
            boolean doSkipThisElement = false;
            if (element instanceof IMember && element.getElementType() == IJavaElement.METHOD) {
                ISourceRange range = ((IMember) element).getSourceRange();
                if (position >= range.getOffset()) {
                    try {
                        String text = fDocument.get(range.getOffset(), position - range.getOffset() + 1);
                        if (text.indexOf('{') != -1 && !text.endsWith("}")) { //$NON-NLS-1$
                            doSkipThisElement = true;
                            position = range.getOffset() + range.getLength();
                        }
                    } catch (BadLocationException e) {
                        // Ignore it and do not skip validation
                    }
                    position++;
                }
            } else {
                IJavaElement parent = element.getParent();
                while (parent != null && parent.getElementType() != IJavaElement.COMPILATION_UNIT) {
                    if (parent.getElementType() == IJavaElement.METHOD) {
                        doSkipThisElement = true;
                        break;
                    }
                    parent = parent.getParent();
                }
                position++;
            }

            if (!doSkipThisElement)
                return true;
        }
    } catch (JavaModelException e) {
        LogHelper.logError(CommonValidationPlugin.getDefault(), e);
    } finally {
        try {
            unit.discardWorkingCopy();
        } catch (JavaModelException e) {
            LogHelper.logError(CommonValidationPlugin.getDefault(), e);
        }
    }

    return atLeastOneElementIsProcessed ? result : true;
}

From source file:org.jboss.tools.forge.ui.internal.commands.EntityPostProcessor.java

License:Open Source License

@Override
public void postProcess(Map<String, String> commandDetails) {
    try {//w  w  w  .  ja v a  2  s .c  o  m
        String crn = commandDetails.get("crn");
        IFile file = ForgeCommandPostProcessorHelper.getFile(crn);
        if (file == null)
            return;
        IJavaElement javaElement = JavaCore.create(file);
        if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
            IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
            IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
            if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
                ((ISetSelectionTarget) projectExplorer).selectReveal(new StructuredSelection(file));
            }
            IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer");
            if (packageExplorer == null && projectExplorer == null) {
                packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
            }
            if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
                ((ISetSelectionTarget) packageExplorer).selectReveal(new StructuredSelection(file));
            }
        }
    } catch (PartInitException e) {
        ForgeUIPlugin.log(e);
    }
}

From source file:org.jboss.tools.forge.ui.internal.commands.FieldPostProcessor.java

License:Open Source License

@Override
public void postProcess(Map<String, String> commandDetails) {
    try {/*w  w  w  .j  av  a  2 s . c  o  m*/
        String crn = commandDetails.get("crn");
        String par = commandDetails.get("par");
        IFile file = ForgeCommandPostProcessorHelper.getFile(crn);
        if (file == null)
            return;
        IJavaElement javaElement = JavaCore.create(file);
        if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
            try {
                IType type = ((ICompilationUnit) javaElement).getTypes()[0];
                IField field = getFieldToPostProcess(par, type);
                IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
                if (field != null) {
                    ISourceRange sourceRange = field.getSourceRange();
                    IEditorPart editorPart = IDE.openEditor(workbenchPage, file);
                    if (sourceRange != null && editorPart != null && editorPart instanceof ITextEditor) {
                        ((ITextEditor) editorPart).selectAndReveal(sourceRange.getOffset(),
                                sourceRange.getLength());
                    }
                }
                IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
                if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
                    ((ISetSelectionTarget) projectExplorer).selectReveal(new StructuredSelection(file));
                }
                IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer");
                if (packageExplorer == null && projectExplorer == null) {
                    packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
                }
                if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
                    ((ISetSelectionTarget) packageExplorer).selectReveal(new StructuredSelection(file));
                }
            } catch (JavaModelException e) {
                ForgeUIPlugin.log(e);
            }
        }
    } catch (PartInitException e) {
        ForgeUIPlugin.log(e);
    }
}

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

License:Open Source License

private List<Location> findReferences(IJavaElement element) throws JavaModelException, CoreException {
    SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES);
    final List<Location> result = new ArrayList<>();
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            createSearchScope(), new SearchRequestor() {

                @Override/*  w w  w.j  a  v  a 2 s .c  o m*/
                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);
                        if (compilationUnit == null) {
                            return;
                        }
                        Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(),
                                match.getLength());
                        result.add(location);

                    }

                }
            }, new NullProgressMonitor());

    return result;
}