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:net.sourceforge.metrics.core.sources.AbstractMetricSource.java

License:Open Source License

/**
 * returns the path if this object represent a compilation unit or bigger scope, null otherwise
 * //from   www . ja va 2  s. c o  m
 * @return
 */
public String getPath() {
    IJavaElement element = getJavaElement();
    if (element.getElementType() <= IJavaElement.COMPILATION_UNIT) {
        return element.getPath().toString();
    } /* else { */
    return null;
    /* } */
}

From source file:net.sourceforge.metrics.core.sources.AbstractMetricSource.java

License:Open Source License

/**
 * get the original compilation unit for the given IJavaElement. If the compilation unit turns out to be a WorkingCopy this method returns its original source compilation unit
 * /*from  ww  w .j  a  v  a  2s.co m*/
 * @param input
 * @return ICompilationUnit
 */
public static ICompilationUnit getOriginalCompilationUnit(IJavaElement input) {

    return (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT);
}

From source file:net.sourceforge.metrics.core.sources.Dispatcher.java

License:Open Source License

/**
 * Get the AbstractMetricSource for the given IJavaElement from cache or create a new one and have it calculate the metrics.
 * /*from   w ww. ja  va2s .  c o m*/
 * @param input
 * @return AbstractMetricSource
 */
public static AbstractMetricSource calculateAbstractMetricSource(IJavaElement input) {
    AbstractMetricSource m = getAbstractMetricSource(input);
    if (m == null) {
        IJavaElement calculate = input;
        // calculate from COMPILATION_UNIT down if type or method
        if (input.getElementType() > IJavaElement.COMPILATION_UNIT) {
            calculate = input.getAncestor(IJavaElement.COMPILATION_UNIT);
        }
        m = singleton.createNewSource(calculate);
        m.setJavaElement(calculate);
        m.recurse(null);
        // should be in cache now
        m = Cache.singleton.get(input);
    }
    return m;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

/**
 * @param values/*w  w w.j ava 2  s  .  c  om*/
 * @param id
 * @param pOut
 */
private void printValues(List<AbstractMetricSource> values, final String id, XMLPrintStream pOut,
        MetricDescriptor md, NumberFormat nf) {
    // sort values first
    Collections.sort(values, new Comparator<AbstractMetricSource>() {

        public int compare(AbstractMetricSource left, AbstractMetricSource right) {
            Metric lm = left.getValue(id);
            Metric rm = right.getValue(id);
            int result;
            if (lm == null) { // BUG #826997
                result = (rm == null) ? 0 : -1;
            } else {
                result = -lm.compareTo(rm); // BUG 746394
            }
            if (result != 0) {
                return result;
            }
            return left.getHandle().compareTo(right.getHandle());
        }

    });
    for (AbstractMetricSource next : values) {
        IJavaElement element = next.getJavaElement();
        Metric val = next.getValue(id);
        if (val != null) {
            pOut.indent(3);
            pOut.print("<Value name=\"");
            pOut.print(getName(element));
            pOut.print("\" ");
            IJavaElement source = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (source != null) {
                pOut.print("source =\"");
                pOut.print(getName(source));
                pOut.print("\" ");
            }
            IJavaElement packageF = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
            if (packageF != null) {
                pOut.print("package =\"");
                pOut.print(getName(packageF));
                pOut.print("\" ");
            }
            pOut.print("value =\"");
            pOut.print(nf.format(val.doubleValue()));
            pOut.print("\"");
            if (!md.isValueInRange(val.doubleValue())) {
                pOut.print(" inrange=\"false\"");
            }
            pOut.println("/>");
        }
    }
}

From source file:net.sourceforge.metrics.ui.dependencies.EclipseNode.java

License:Open Source License

private void openInEditor() {
    Display d = Display.getDefault();/*  w  ww  .  java 2 s .  c om*/
    d.asyncExec(new Runnable() {

        public void run() {
            try {
                IJavaElement element = JavaCore.create(getID());
                ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                IEditorPart javaEditor = JavaUI.openInEditor(cu);
                JavaUI.revealInEditor(javaEditor, element);
            } catch (PartInitException e) {
                Log.logError("Node.openInEditor", e);
            } catch (JavaModelException e) {
                Log.logError("Node.openInEditor", e);
            }
        }
    });
}

From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java

License:Open Source License

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
    if (selection != null) {
        int offset = -1;
        int length = -1;
        final StringTokenizer tokenizer = new StringTokenizer(selection);
        if (tokenizer.hasMoreTokens())
            offset = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (tokenizer.hasMoreTokens())
            length = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (offset >= 0 && length >= 0) {
            fSelectionStart = offset;//  ww w.  java 2 s.  co m
            fSelectionLength = length;
        } else
            return RefactoringStatus.createFatalErrorStatus(
                    Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument,
                            new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
    } else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
    String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(),
                handle, false);
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT)
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(),
                    IJavaRefactorings.INTRODUCE_FACTORY);
        else {
            fCUHandle = (ICompilationUnit) element;
            initialize();
        }
    } else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(),
                handle, false);
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.TYPE)
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(),
                    IJavaRefactorings.INTRODUCE_FACTORY);
        else {
            final IType type = (IType) element;
            fFactoryClassName = type.getFullyQualifiedName();
        }
    } else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String name = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
    if (name != null && !"".equals(name)) //$NON-NLS-1$
        fNewMethodName = name;
    else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
    final String protect = arguments.getAttribute(ATTRIBUTE_PROTECT);
    if (protect != null) {
        fProtectConstructor = Boolean.valueOf(protect).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(
                RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_PROTECT));
    return new RefactoringStatus();
}

From source file:nz.ac.massey.cs.jquest.views.ElementChangedListener.java

License:Open Source License

private void processDelta(IJavaElementDelta delta) {
    IJavaElement javaElement = delta.getElement();
    switch (javaElement.getElementType()) {
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        IJavaElementDelta[] affectedChildren = delta.getAffectedChildren();
        for (IJavaElementDelta affectedChild : affectedChildren) {
            processDelta(affectedChild);
        }//from  ww  w  .j  a  v a  2s .co m
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) javaElement;
        if (selectedProject == null)
            return;
        if (!cu.getJavaProject().getProject().getName().equals(selectedProject.getName())) {
            return;
        }
        if (delta.getKind() == IJavaElementDelta.ADDED) {
            projectHasChanged = true;
            singleView.projectUpdated();
        } else if (delta.getKind() == IJavaElementDelta.REMOVED) {
            projectHasChanged = true;
            singleView.projectUpdated();
        } else if (delta.getKind() == IJavaElementDelta.CHANGED) {
            projectHasChanged = true;
            singleView.projectUpdated();
            //            if((delta.getFlags() & IJavaElementDelta.F_FINE_GRAINED) != 0) {
            //            }
        }
    }
}

From source file:nz.ac.massey.cs.jquest.views.ViewContentProvider.java

License:Open Source License

private Object[] getTypeNodes(Object inputElement) throws JavaModelException {
    if (selection == null)
        return new Object[] {};
    else {/*from www .  ja  va 2 s.  c om*/
        selectedProject = selection.getJavaProject().getProject();
        validateOrAddGraph();
    }
    //      if(inputElement == null) return new Object[]{};
    //      if(inputElement instanceof IJavaElement) {
    //         selection = (IJavaElement) inputElement;
    //         selectedProject = selection.getJavaProject().getProject();
    //         validateOrAddGraph();
    //      }
    TypeNode selectedNode = null;
    if (selection.getElementType() == IJavaElement.COMPILATION_UNIT) {
        String classname = ((ICompilationUnit) selection).getTypes()[0].getFullyQualifiedName();
        selectedNode = Utils.getNode(g, classname);
        selectedNodeName = classname;
        baseNode = selectedNode;
    } else if (selection.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        String packageName = ((IPackageFragment) selection).getElementName();
        if (pg == null) {
            if (h == null)
                h = new GraphBuilderHandler();
            pg = h.loadPackageGraph(g, new NullProgressMonitor());
        }
        selectedNode = Utils.getNode(pg, packageName);
        selectedNodeName = packageName;
        baseNode = selectedNode;

    } else {
        return new Object[] {};
    }
    if (selectedNode == null) {
        //         displayMessage();
        return new Object[] {};
    }
    return getNodes(selectedNode);

}

From source file:org.apache.felix.sigil.eclipse.model.util.JavaHelper.java

License:Apache License

private static void findIndirectReferences(Set<String> indirect, String pckg, IParent parent, IJavaProject p)
        throws JavaModelException {
    for (IJavaElement e : parent.getChildren()) {
        boolean skip = false;
        switch (e.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            IPackageFragmentRoot rt = (IPackageFragmentRoot) e;
            IClasspathEntry ce = rt.getRawClasspathEntry();
            IPath path = ce.getPath();/*from   w  w w.j  a v  a2s.c  o  m*/
            skip = "org.eclipse.jdt.launching.JRE_CONTAINER".equals(path.toString());
            break;
        case IJavaElement.CLASS_FILE:
            IClassFile cf = (IClassFile) e;
            if (cf.getElementName().startsWith(pckg)) {
                findIndirectReferences(indirect, findPackage(cf.getType().getSuperclassName()), p, p);
            }
            break;
        case IJavaElement.COMPILATION_UNIT:
            ICompilationUnit cu = (ICompilationUnit) e;
            break;
        }

        if (!skip && e instanceof IParent) {
            IParent newParent = (IParent) e;
            findIndirectReferences(indirect, pckg, newParent, p);
        }
    }
}

From source file:org.apache.felix.sigil.eclipse.ui.util.ResourcesDialogHelper.java

License:Apache License

public static BackgroundLoadingSelectionDialog<String> createClassSelectDialog(Shell shell, String title,
        final ISigilProjectModel project, String selected, final String ifaceOrParentClass) {
    final BackgroundLoadingSelectionDialog<String> dialog = new BackgroundLoadingSelectionDialog<String>(shell,
            "Class Name", true);

    IJobRunnable job = new IJobRunnable() {
        public IStatus run(IProgressMonitor monitor) {
            try {
                for (IJavaElement e : JavaHelper.findTypes(project.getJavaModel(),
                        IJavaElement.PACKAGE_FRAGMENT)) {
                    IPackageFragment root = (IPackageFragment) e;
                    if (project.isInBundleClasspath(root)) {
                        for (IJavaElement e1 : JavaHelper.findTypes(root, IJavaElement.COMPILATION_UNIT,
                                IJavaElement.CLASS_FILE)) {
                            ITypeRoot typeRoot = (ITypeRoot) e1;
                            IType type = (IType) JavaHelper.findType(typeRoot, IJavaElement.TYPE);
                            if (JavaHelper.isAssignableTo(ifaceOrParentClass, type)) {
                                dialog.addElement(type.getFullyQualifiedName());
                            }//from   w w  w  . ja v a 2s . c  o m
                        }
                    }
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getStatus();
            }
        }

    };

    dialog.addBackgroundJob("Scanning for activators in project", job);

    return dialog;
}