Example usage for org.eclipse.jdt.core IJavaElement isStructureKnown

List of usage examples for org.eclipse.jdt.core IJavaElement isStructureKnown

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement isStructureKnown.

Prototype

    boolean isStructureKnown() throws JavaModelException;

Source Link

Document

Returns whether the structure of this element is known.

Usage

From source file:apet.utils.SourceUtils.java

License:Open Source License

private static void fileEvaluations(IJavaElement javaFile) throws ApetException {
    try {/* w  w w .j  a  va 2  s .  c o  m*/
        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:com.wuetherich.osgi.ds.annotations.internal.builder.DsAnnotationBuildVisitor.java

License:Open Source License

/**
 * <p>/*w w  w  .  j  a  v a2  s.co m*/
 * </p>
 * 
 * @param resource
 * @throws CoreException
 */
private void handle(IResource resource) throws CoreException {

    //
    if (!resource.getName().endsWith(".java")) {
        return;
    }

    //
    IJavaElement element = JavaCore.create(resource);
    IJavaProject javaProject = JavaCore.create(resource.getProject());
    if (!javaProject.isOnClasspath(element) || !element.isStructureKnown()) {
        return;
    }

    if (JavaCore.create(resource) == null) {
        return;
    }

    try {
        resource.deleteMarkers(Constants.DS_ANNOTATION_PROBLEM_MARKER, true, IResource.DEPTH_ZERO);
    } catch (CoreException e1) {
        e1.printStackTrace();
    }

    //
    IJavaElement compilationUnit = JavaCore.create(resource);

    if (compilationUnit != null && compilationUnit.isStructureKnown()) {
        parse((ICompilationUnit) compilationUnit, resource);
    }
}

From source file:costabs.utils.SourceUtils.java

License:Open Source License

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

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

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

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

    }
}

From source file:de.loskutov.bco.views.BytecodeOutlineView.java

License:Open Source License

/**
 * @param childEl//from www  .jav  a 2 s.c  o m
 * @return return null if type is not known or bytecode is not written or cannot be
 * found
 */
private DecompiledClass decompileBytecode(IJavaElement childEl) {
    // check here for inner classes too
    IJavaElement type = JdtUtils.getEnclosingType(childEl);
    if (type == null) {
        type = javaInput;
    }
    if (type == null) {
        return null;
    }
    InputStream is = JdtUtils.createInputStream(type);
    if (is == null) {
        return null;
    }
    DecompiledClass decompiledClass = null;
    int available = 0;
    try {
        ClassLoader cl = null;
        if (modes.get(BCOConstants.F_SHOW_ANALYZER)) {
            cl = JdtUtils.getClassLoader(type);
        }

        String fieldName = null;
        String methodName = null;
        /*
         * find out, which name we should use for selected element
         */
        if (modes.get(BCOConstants.F_SHOW_ONLY_SELECTED_ELEMENT) && childEl != null) {
            if (childEl.getElementType() == IJavaElement.FIELD) {
                fieldName = childEl.getElementName();
            } else {
                methodName = JdtUtils.getMethodSignature(childEl);
            }
        }
        available = is.available();
        decompiledClass = DecompilerHelper.getDecompiledClass(is,
                new DecompilerOptions(fieldName, methodName, modes, cl));
    } catch (Exception e) {
        try {
            // check if compilation unit is ok - then this is the user problem
            if (type.isStructureKnown()) {
                BytecodeOutlinePlugin.error("Cannot decompile: " + type, e);
            } else {
                BytecodeOutlinePlugin.log(e, IStatus.ERROR);
            }
        } catch (JavaModelException e1) {
            // this is compilation problem - don't show the message
            BytecodeOutlinePlugin.log(e1, IStatus.WARNING);
        }
    } catch (UnsupportedClassVersionError e) {
        BytecodeOutlinePlugin.error("Cannot decompile: " + type + ". Error was caused by attempt to "
                + "load a class compiled with the Java version which is not "
                + "supported by the current JVM. ", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            BytecodeOutlinePlugin.log(e, IStatus.WARNING);
        }
    }
    // remember class file size to show it later in UI
    if (decompiledClass != null) {
        decompiledClass.setClassSize(available);
    }
    return decompiledClass;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java

License:Open Source License

private static void outputSymbol(IJavaElement elt, String what, String nm, String key, IvyXmlWriter xw) {
    if (what == null || nm == null)
        return;//from   w  ww .  j  av  a2 s . c om

    xw.begin("ITEM");
    xw.field("TYPE", what);
    xw.field("NAME", nm);
    xw.field("HANDLE", elt.getHandleIdentifier());

    xw.field("WORKING", (elt.getPrimaryElement() != elt));
    ICompilationUnit cu = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null) {
        xw.field("CUWORKING", cu.isWorkingCopy());
    }
    try {
        xw.field("KNOWN", elt.isStructureKnown());
    } catch (JavaModelException e) {
    }

    if (elt instanceof ISourceReference) {
        try {
            ISourceRange rng = ((ISourceReference) elt).getSourceRange();
            if (rng != null) {
                xw.field("STARTOFFSET", rng.getOffset());
                xw.field("ENDOFFSET", rng.getOffset() + rng.getLength());
                xw.field("LENGTH", rng.getLength());
            }
        } catch (JavaModelException e) {
            BedrockPlugin.logE("Problem getting source range: " + e);
        }
    }

    if (elt instanceof ILocalVariable) {
        ILocalVariable lcl = (ILocalVariable) elt;
        xw.field("SIGNATURE", lcl.getTypeSignature());
    }

    if (elt instanceof IMember) {
        try {
            IMember mem = ((IMember) elt);
            int fgs = mem.getFlags();
            if (mem.getParent() instanceof IType && !(elt instanceof IType)) {
                IType par = (IType) mem.getParent();
                if (par.isInterface()) {
                    if (elt instanceof IMethod)
                        fgs |= Flags.AccAbstract;
                    fgs |= Flags.AccPublic;
                }
                xw.field("QNAME", par.getFullyQualifiedName() + "." + nm);
            }
            xw.field("FLAGS", fgs);
        } catch (JavaModelException e) {
        }
    }

    if (elt instanceof IPackageFragment || elt instanceof IType) {
        Display d = BedrockApplication.getDisplay();
        if (d != null) {
            JavadocUrl ju = new JavadocUrl(elt);
            d.syncExec(ju);
            URL u = ju.getResult();
            if (u != null) {
                xw.field("JAVADOC", u.toString());
            }
        }
    }

    xw.field("SOURCE", "USERSOURCE");
    if (key != null)
        xw.field("KEY", key);

    boolean havepath = false;
    for (IJavaElement pe = elt.getParent(); pe != null; pe = pe.getParent()) {
        if (pe.getElementType() == IJavaElement.COMPILATION_UNIT) {
            IProject ip = elt.getJavaProject().getProject();
            File f = BedrockUtil.getFileForPath(elt.getPath(), ip);
            xw.field("PATH", f.getAbsolutePath());
            havepath = true;
            break;
        }
    }
    IJavaProject ijp = elt.getJavaProject();
    if (ijp != null)
        xw.field("PROJECT", ijp.getProject().getName());
    IPath p = elt.getPath();
    if (p != null) {
        xw.field("XPATH", p);
        if (!havepath) {
            IProject ip = elt.getJavaProject().getProject();
            File f = getFileForPath(elt.getPath(), ip);
            xw.field("PATH", f.getAbsolutePath());
        }
    }

    if (elt instanceof IMethod) {
        IMethod m = (IMethod) elt;
        try {
            xw.field("RESOLVED", m.isResolved());
            ISourceRange rng = m.getNameRange();
            if (rng != null) {
                xw.field("NAMEOFFSET", rng.getOffset());
                xw.field("NAMELENGTH", rng.getLength());
            }
            xw.field("RETURNTYPE", m.getReturnType());
            xw.field("NUMPARAM", m.getNumberOfParameters());
            String[] pnms = m.getParameterNames();
            String[] ptys = m.getParameterTypes();
            for (int i = 0; i < ptys.length; ++i) {
                xw.begin("PARAMETER");
                if (i < pnms.length)
                    xw.field("NAME", pnms[i]);
                xw.field("TYPE", ptys[i]);
                xw.end();
            }
            for (String ex : m.getExceptionTypes()) {
                xw.begin("EXCEPTION");
                xw.field("TYPE", ex);
                xw.end();
            }
        } catch (JavaModelException e) {
        }
    }

    // TODO: output parameters as separate elements with type and name

    if (elt instanceof IAnnotatable) {
        IAnnotatable ann = (IAnnotatable) elt;
        try {
            IAnnotation[] ans = ann.getAnnotations();
            for (IAnnotation an : ans) {
                xw.begin("ANNOTATION");
                xw.field("NAME", an.getElementName());
                xw.field("COUNT", an.getOccurrenceCount());
                try {
                    for (IMemberValuePair mvp : an.getMemberValuePairs()) {
                        xw.begin("VALUE");
                        xw.field("NAME", mvp.getMemberName());
                        if (mvp.getValue() != null)
                            xw.field("VALUE", mvp.getValue().toString());
                        xw.field("KIND", mvp.getValueKind());
                        xw.end("VALUE");
                    }
                } catch (JavaModelException e) {
                }
                xw.end("ANNOTATION");
            }
        } catch (JavaModelException e) {
        }
    }

    xw.end("ITEM");
}

From source file:edu.buffalo.cse.green.editor.DiagramEditor.java

License:Open Source License

/**
 * Finds all relationships that have the given element as their source.
 * //w w w. ja  v  a  2 s  . c  o m
 * @param element - The element to find relationships for.
 */
private void findRelationships(IJavaElement element) {
    long modified;

    // if the element contains errors, quit
    try {
        if (!element.exists() || !element.isStructureKnown())
            return;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return;
    }

    CompilationUnit cu;
    String id = element.getHandleIdentifier();

    // generate AST if necessary - check modification stamp
    Long modifiedStore = _cuMap.getModificationStamp(id);
    IResource resource = element.getResource();

    if (resource == null) {
        if (_cuMap.getCompilationUnit(id) != null) {
            modifiedStore = new Long(0);
        }

        modified = 0;
    } else {
        modified = resource.getModificationStamp();
    }

    // if there isn't an up-to-date AST, create one
    if ((modifiedStore == null) || (modified != modifiedStore)) {
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setResolveBindings(true);

        if (element instanceof ICompilationUnit) {
            parser.setSource((ICompilationUnit) element);
        } else if (element instanceof IClassFile) {
            // only search through the class if it has source code attached
            IClassFile classFile = (IClassFile) element;

            try {
                if (classFile.getSource() == null) {
                    return;
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }

            parser.setSource(classFile);
        } else {
            GreenException.illegalOperation("Illegal element type: " + element.getClass());
        }

        cu = (CompilationUnit) parser.createAST(null);
        _cuMap.put(element, cu);
    } else {
        cu = _cuMap.getCompilationUnit(id);
    }

    // run the recognizers
    for (Class klass : PlugIn.getRelationships()) {
        RelationshipRecognizer recognizer = PlugIn.getRelationshipGroup(klass).getRecognizer();

        // run the recognizer
        recognizer.run(cu, getRootModel().getRelationshipCache());
    }
}

From source file:org.grails.ide.eclipse.test.gsp.GSPHyperlinkTests.java

License:Open Source License

private void assertHyperlink(String contents, int offset, String elementName, boolean isBinary)
        throws Exception {
    String fileName = "grails-app/views/nuthin/foo.gsp";
    IFile file = testProject.getProject().getFile(new Path(fileName));
    if (!file.exists()) {
        file = testProject.createFile(fileName, contents);
        waitForIndexes();//  w w w .  j  av a  2 s .c om
    }
    IEditorPart part = IDE.openEditor(Workbench.getInstance().getActiveWorkbenchWindow().getActivePage(), file);
    try {
        IHyperlink[] links = detector.detectHyperlinks(((StructuredTextEditor) part).getTextViewer(),
                new Region(offset, 0), true);
        assertNotNull("Should have found one hyperlink", links);
        assertEquals("Should have found one hyperlink", 1, links.length);
        JavaElementHyperlink link = (JavaElementHyperlink) links[0];
        IJavaElement element = link.getElement();
        assertEquals(elementName, element.getElementName());
        assertTrue("Element should exist: " + element, element.exists());
        if (isBinary) {
            assertTrue("Should be read only", element.isReadOnly());
        } else {
            assertFalse("Should be not read only", element.isReadOnly());
        }
        assertTrue("Should have structure known", element.isStructureKnown());
    } finally {
        ((StructuredTextEditor) part).close(false);
    }
}

From source file:org.sonarlint.eclipse.jdt.internal.JdtUtils.java

License:Open Source License

private static boolean isStructureKnown(IJavaElement javaElt) {
    try {//  ww w  .  j a v a2  s  .  c o m
        return javaElt.isStructureKnown();
    } catch (JavaModelException e) {
        return false;
    }
}