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:apet.utils.SourceUtils.java

License:Open Source License

private static void fileEvaluations(IJavaElement javaFile) throws ApetException {
    try {//from  w  ww .  j  av  a2  s . com
        if (javaFile.getElementType() != IJavaElement.COMPILATION_UNIT) {
            throw new ApetException("The file must be a Java file");
        }
        ICompilationUnit javaFileComp = (ICompilationUnit) javaFile;

        if (!javaFile.isStructureKnown()) {
            throw new ApetException("The file cannot have errors to analyze it");
        }
        if (!javaFile.getElementName().endsWith(".java")) {
            throw new ApetException("The file is not a Java File");
        }

        if (!javaFileComp.isConsistent()) {
            throw new ApetException("The file is not consistent, cannot proccess it");
        }

    } catch (JavaModelException e) {
        throw new ApetException("Cannot evaluate the file, it may be incorrect");

    }
}

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render/*from  w  w w .j  ava  2 s  . c om*/
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
    int type = element.getElementType();
    IPackageFragmentRoot root = null;

    if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT
            && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
        root = JavaModelUtil.getPackageFragmentRoot(element);
    if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) {
        appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
        fBuffer.append(JavaElementLabels.CONCAT_STRING);
    }

    switch (type) {
    case IJavaElement.METHOD:
        appendMethodLabel((IMethod) element, flags);
        break;
    case IJavaElement.FIELD:
        appendFieldLabel((IField) element, flags);
        break;
    case IJavaElement.LOCAL_VARIABLE:
        appendLocalVariableLabel((ILocalVariable) element, flags);
        break;
    case IJavaElement.TYPE_PARAMETER:
        appendTypeParameterLabel((ITypeParameter) element, flags);
        break;
    case IJavaElement.INITIALIZER:
        appendInitializerLabel((IInitializer) element, flags);
        break;
    case IJavaElement.TYPE:
        appendTypeLabel((IType) element, flags);
        break;
    case IJavaElement.CLASS_FILE:
        appendClassFileLabel((IClassFile) element, flags);
        break;
    case IJavaElement.COMPILATION_UNIT:
        appendCompilationUnitLabel((ICompilationUnit) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        appendPackageFragmentLabel((IPackageFragment) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
        break;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.PACKAGE_DECLARATION:
        appendDeclarationLabel(element, flags);
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.JAVA_MODEL:
        fBuffer.append(element.getElementName());
        break;
    default:
        fBuffer.append(element.getElementName());
    }

    if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
        int offset = fBuffer.length();
        fBuffer.append(JavaElementLabels.CONCAT_STRING);
        appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);

        //         if (getFlag(flags, JavaElementLabels.COLORIZE)) {
        //            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        //         }

    }
}

From source file:bndtools.internal.pkgselection.SearchUtils.java

License:Open Source License

/**
 * @param match/*  ww w  .jav  a2 s. c  o m*/
 * @return the enclosing {@link ICompilationUnit} of the given match, or null iff none
 */
public static ICompilationUnit getCompilationUnit(SearchMatch match) {
    IJavaElement enclosingElement = getEnclosingJavaElement(match);
    if (enclosingElement != null) {
        if (enclosingElement instanceof ICompilationUnit)
            return (ICompilationUnit) enclosingElement;
        ICompilationUnit cu = (ICompilationUnit) enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (cu != null)
            return cu;
    }

    IJavaElement jElement = JavaCore.create(match.getResource());
    if (jElement != null && jElement.exists() && jElement.getElementType() == IJavaElement.COMPILATION_UNIT)
        return (ICompilationUnit) jElement;
    return null;
}

From source file:br.com.objectos.way.ide.IFileWrapperPojo.java

License:Apache License

@Override
public Optional<ICompilationUnit> getICompilationUnit() {
    IJavaElement element = JavaCore.create(file);
    ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
    return Optional.of(compilationUnit);
}

From source file:byke.DependencyAnalysis.java

License:Open Source License

private List<ICompilationUnit> compilationUnits() throws JavaModelException {
    if (!(_subject instanceof IPackageFragment))
        return asList(((ICompilationUnit) _subject.getAncestor(IJavaElement.COMPILATION_UNIT)));

    List<ICompilationUnit> ret = new ArrayList<ICompilationUnit>();
    for (IPackageFragment packge : withSubpackages((IPackageFragment) _subject))
        ret.addAll(asList(packge.getCompilationUnits()));

    return ret;/*from w  w w .  j  a va 2 s  .com*/
}

From source file:ca.uvic.chisel.diver.mylyn.logger.logging.PageSelectionListener.java

License:Open Source License

/**
 * @param je//from  w ww .j ava2s.  c  om
 * @return
 */
private String getElementType(IJavaElement je) {
    switch (je.getElementType()) {
    case IJavaElement.ANNOTATION:
        return "annotation";
    case IJavaElement.CLASS_FILE:
        return "classfile";
    case IJavaElement.COMPILATION_UNIT:
        return "compilationunit";
    case IJavaElement.FIELD:
        return "field";
    case IJavaElement.IMPORT_CONTAINER:
        return "importcontainer";
    case IJavaElement.IMPORT_DECLARATION:
        return "importdeclaration";
    case IJavaElement.INITIALIZER:
        return "initializer";
    case IJavaElement.JAVA_MODEL:
        return "javamodel";
    case IJavaElement.JAVA_PROJECT:
        return "javaproject";
    case IJavaElement.LOCAL_VARIABLE:
        return "localvariable";
    case IJavaElement.METHOD:
        return "method";
    case IJavaElement.PACKAGE_DECLARATION:
        return "packagedeclaration";
    case IJavaElement.PACKAGE_FRAGMENT:
        return "packagefragment";
    case IJavaElement.TYPE:
        return "type";
    case IJavaElement.TYPE_PARAMETER:
        return "typeparameter";
    }
    return "null";
}

From source file:cfgrecognition.loader.SootClassLoader.java

License:Open Source License

/**
 * The method traverses all of the project types in depth-first order
 * including inner and anonymous types and loads them in Soot.
 * /*  w ww  . ja va  2s . c  om*/
 * 
 * @param monitor
 *            The progress monitor.
 * @throws Exception
 *             Propagated from JDT APIs.
 */
public void process() throws Exception {
    IJavaProject project = Activator.getIJavaProject();
    IPackageFragmentRoot[] packageFragmentRoots = project.getPackageFragmentRoots();
    //      subMonitor.beginTask("Loading " + project.getElementName() + " ...", 2);
    //
    //      SubProgressMonitor monitor = new SubProgressMonitor(subMonitor, 1);
    //      monitor.beginTask("Loading packages ... ",
    //            packageFragmentRoots.length + 1);

    for (IPackageFragmentRoot pkgFragRoot : packageFragmentRoots) {
        if (pkgFragRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
            IJavaElement[] pkgFrags = (IJavaElement[]) pkgFragRoot.getChildren();
            for (IJavaElement pkgFrag : pkgFrags) {
                //               if (monitor.isCanceled())
                //                  return;
                //
                //               monitor.subTask("Loading classes in "
                //                     + pkgFrag.getElementName());

                if (pkgFrag.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                    IPackageFragment pkgFragment = (IPackageFragment) pkgFrag;
                    IJavaElement[] children = pkgFragment.getChildren();
                    for (IJavaElement anElement : children) {
                        //                     if (monitor.isCanceled())
                        //                        return;

                        // Make sure its a java file
                        if (anElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                            this.dfsDomTree(anElement);
                            //                        this.dfsDomTree(anElement, monitor);
                        }
                    }
                }
            }
        }
        //         monitor.worked(1);
    }

    // Load the necessary classes after all of the classes have been loaded.
    Scene.v().loadNecessaryClasses();
    //      monitor.done();

    // Create an instance of Interpreter before we process anything further
    // Interpreter.instance();

    //      monitor = new SubProgressMonitor(subMonitor, 1);
    //      monitor.beginTask("Configuring entry points ... ", this
    //            .getAllSootClasses().size());

    //      for (SootClass c : this.getAllSootClasses()) {
    //         // ExtensionManager manager = ExtensionManager.instance();
    //         // Configure entry methods for extension plugin
    //         for (SootMethod method : c.getMethods()) {
    //            if (monitor.isCanceled())
    //               return;
    //            // TODO: later down the road, this specifies the entry points
    //            // (characteristics for different algorithms)
    //            // manager.checkEntryMethod(method, monitor);
    //            monitor.worked(1);
    //         }
    //      }
    //      monitor.done();
}

From source file:cfgrecognition.loader.SootClassLoader.java

License:Open Source License

protected void dfsDomTree(IJavaElement element) throws Exception {
    //      if (monitor.isCanceled()) {
    //         return;
    //      }/*from www  .ja  va2 s. co m*/

    if (element.isReadOnly())
        return;

    int elementType = element.getElementType();
    if (elementType == IJavaElement.COMPILATION_UNIT) {
        ICompilationUnit cu = (ICompilationUnit) element;
        IType[] allTypes = cu.getTypes();
        for (IType aType : allTypes) {
            dfsDomTree(aType);
        }
    } else if (elementType == IJavaElement.TYPE) {
        IType aType = (IType) element;

        if (aType.isClass()) {
            // Load a type in Soot
            load(aType.getFullyQualifiedName());
        }

        // Go inside the methods to look for Anonymous Inner Class
        for (IMethod m : aType.getMethods()) {
            IJavaElement[] elems = m.getChildren();
            for (IJavaElement elem : elems) {
                if (elem.getElementType() == IJavaElement.TYPE) {
                    dfsDomTree(elem);
                }
            }
        }

        // For inner classes
        IType[] allTypes = aType.getTypes();
        for (IType childType : allTypes) {
            dfsDomTree(childType);
        }
    }

}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.PostActivityCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity./*from ww w  . ja  va  2 s.c o  m*/
 *
 */
@Override
public void processNewType(IType newType) {
    try {
        String methodContent = "    /** Called when the activity is first created. */\n" + "    @Override\n"
                + "    public void onCreate(Bundle savedInstanceState) {\n"
                + "        super.onCreate(savedInstanceState);\n" + "\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(SdkConstants.CLASS_BUNDLE, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.PostReceiverCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity./*from  w w  w .j av  a2  s  . co m*/
 *
 */
@Override
public void processNewType(IType newType) {
    try {
        String methodContent = "    @Override\n"
                + "    public void onReceive(Context context, Intent intent) {\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(SdkConstants.CLASS_CONTEXT, null /* sibling */,
                    new NullProgressMonitor());
            compilationUnit.createImport(SdkConstants.CLASS_INTENT, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // looks like the class already existed (this happens when the user check to create
        // inherited abstract methods).
    }
}