Example usage for org.eclipse.jdt.core IType getPrimaryElement

List of usage examples for org.eclipse.jdt.core IType getPrimaryElement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getPrimaryElement.

Prototype

IJavaElement getPrimaryElement();

Source Link

Document

Returns the primary element (whose compilation unit is the primary compilation unit) this working copy element was created from, or this element if it is a descendant of a primary compilation unit or if it is not a descendant of a working copy (e.g.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.HierarchyScope.java

License:Open Source License

private boolean enclosesType(IType type, boolean recurse) {
    if (this.subTypes != null) {
        // searching subtypes
        if (this.subTypes.contains(type)) {
            return true;
        }/*from   w w  w .  ja va2s .c  o m*/
        // be flexible: look at original element (see bug 14106 and below)
        IType original = type.isBinary() ? null : (IType) type.getPrimaryElement();
        if (original != type && this.subTypes.contains(original)) {
            return true;
        }
    } else {
        if (this.hierarchy.contains(type)) {
            return true;
        } else {
            // be flexible: look at original element (see bug 14106 Declarations in Hierarchy does not find declarations in hierarchy)
            IType original;
            if (!type.isBinary() && (original = (IType) type.getPrimaryElement()) != null) {
                if (this.hierarchy.contains(original)) {
                    return true;
                }
            }
        }
    }
    if (recurse) {
        // queried type is enclosed in this scope if one of its members is:
        try {
            IType[] memberTypes = type.getTypes();
            for (int i = 0; i < memberTypes.length; i++) {
                if (enclosesType(memberTypes[i], recurse)) {
                    return true;
                }
            }
        } catch (JavaModelException e) {
            return false;
        }
    }
    return false;
}

From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java

License:Mozilla Public License

private void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }/*from  w ww . j  a va2 s  .  c  o m*/

    monitor.beginTask(UIPlugin.getString("ClassFactory.operationdesc", fTypeName), 10);

    ICompilationUnit createdWorkingCopy = null;
    try {
        IPackageFragmentRoot root = fPackageFragmentyRoot;
        IPackageFragment pack = fPackageFragment;
        if (pack == null) {
            pack = root.getPackageFragment(""); //$NON-NLS-1$
        }

        if (!pack.exists()) {
            String packName = pack.getElementName();
            pack = root.createPackageFragment(packName, true, null);
        }

        monitor.worked(1);

        String clName = fTypeName;

        boolean isInnerClass = false;

        IType createdType;
        ImportsManager imports;
        int indent = 0;

        String lineDelimiter = System.getProperty("line.separator", "\n");

        ICompilationUnit parentCU = pack.createCompilationUnit(clName + ".java", "", false,
                new SubProgressMonitor(monitor, 2));
        // create a working copy with a new owner
        createdWorkingCopy = parentCU.getWorkingCopy(null);

        // use the compiler template a first time to read the imports
        String content = CodeGeneration.getCompilationUnitContent(createdWorkingCopy, null, "", lineDelimiter); //$NON-NLS-1$
        if (content != null)
            createdWorkingCopy.getBuffer().setContents(content);

        imports = new ImportsManager(createdWorkingCopy);
        // add an import that will be removed again. Having this import solves
        // 14661
        imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), fTypeName));

        String typeContent = constructTypeStub(imports, lineDelimiter);

        String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);

        createdWorkingCopy.getBuffer().setContents(cuContent);

        createdType = createdWorkingCopy.getType(clName);

        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }

        // add imports for superclass/interfaces, so types can be resolved
        // correctly

        ICompilationUnit cu = createdType.getCompilationUnit();
        boolean needsSave = !cu.isWorkingCopy();

        imports.create(needsSave, new SubProgressMonitor(monitor, 1));

        JavaModelUtil.reconcile(cu);

        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }

        // set up again
        imports = new ImportsManager(imports.getCompilationUnit(), imports.getAddedTypes());

        createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1));

        // add imports
        imports.create(needsSave, new SubProgressMonitor(monitor, 1));

        removeUnusedImports(cu, imports.getAddedTypes(), needsSave);

        JavaModelUtil.reconcile(cu);

        ISourceRange range = createdType.getSourceRange();

        IBuffer buf = cu.getBuffer();
        String originalContent = buf.getText(range.getOffset(), range.getLength());

        String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS,
                originalContent, indent, null, lineDelimiter, pack.getJavaProject());
        buf.replace(range.getOffset(), range.getLength(), formattedContent);

        //      String fileComment = getFileComment(cu);
        //      if (fileComment != null && fileComment.length() > 0)
        //      {
        //        buf.replace(0, 0, fileComment + lineDelimiter);
        //      }
        cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
        if (createdWorkingCopy != null) {
            fCreatedType = (IType) createdType.getPrimaryElement();
        } else {
            fCreatedType = createdType;
        }
    } finally {
        if (createdWorkingCopy != null) {
            createdWorkingCopy.discardWorkingCopy();
        }
        monitor.done();
    }

}

From source file:de.tobject.findbugs.reporter.MarkerUtil.java

License:Open Source License

private static String getSourceFileHint(IType type, String qualifiedClassName) {
    String sourceFileStr = "";
    IJavaElement primaryElement = type.getPrimaryElement();
    if (primaryElement != null) {
        return primaryElement.getElementName() + ".java";
    }//from www  . ja va2  s  . c  om
    return sourceFileStr;
}

From source file:org.eclipse.jst.jsf.facesconfig.ui.util.JavaClassUtils.java

License:Open Source License

/**
 * open the type in the editor.//  w w  w.  j  a  v a2s. c o m
 * 
 * @param type
 * @return true if the type could opened in an editor
 */
public static boolean openType(IType type) {
    if (type == null || !type.exists()) {
        return false;
    }

    try {
        IEditorPart editorPart = JavaUI.openInEditor(type.getPrimaryElement());
        if (editorPart != null) {
            JavaUI.revealInEditor(editorPart, type.getPrimaryElement());
            return true;
        }
    } catch (PartInitException e) {
        // ignore this error.
    } catch (JavaModelException e) {
        // ignore this error.
    }
    return false;
}