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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * Cite: jdk1.1.8/docs/guide/innerclasses/spec/innerclasses.doc10.html: For the sake
 * of tools, there are some additional requirements on the naming of an inaccessible
 * class N. Its bytecode name must consist of the bytecode name of an enclosing class
 * (the immediately enclosing class, if it is a member), followed either by `$' and a
 * positive decimal numeral chosen by the compiler, or by `$' and the simple name of
 * N, or else by both (in that order). Moreover, the bytecode name of a block-local N
 * must consist of its enclosing package member T, the characters `$1$', and N, if the
 * resulting name would be unique./* w ww.  jav  a  2s  . co m*/
 * <br>
 * Note, that this rule was changed for static blocks after 1.5 jdk.
 * @param javaElement
 * @return simply element name
 */
public static String getElementName(IJavaElement javaElement) {
    if (isAnonymousType(javaElement)) {
        IType anonType = (IType) javaElement;
        List<IJavaElement> allAnonymous = new ArrayList<IJavaElement>();
        /*
         * in order to resolve anon. class name we need to know about all other
         * anonymous classes in declaring class, therefore we need to collect all here
         */
        collectAllAnonymous(allAnonymous, anonType);
        int idx = getAnonimousIndex(anonType, allAnonymous.toArray(new IType[allAnonymous.size()]));
        return Integer.toString(idx);
    }
    String name = javaElement.getElementName();
    if (isLocal(javaElement)) {
        /*
         * Compiler have different naming conventions for inner non-anon. classes in
         * static blocks or any methods, this difference was introduced with 1.5 JDK.
         * The problem is, that we could have projects with classes, generated
         * with both 1.5 and earlier settings. One could not see on particular
         * java element, for which jdk version the existing bytecode was generated.
         * If we could have a *.class file, but we are just searching for one...
         * So there could be still a chance, that this code fails, if java element
         * is not compiled with comiler settings from project, but with different
         */
        if (is50OrHigher(javaElement)) {
            name = "1" + name; // compiler output changed for > 1.5 code
        } else {
            name = "1$" + name; // see method comment, this was the case for older code
        }
    }

    if (name.endsWith(".java")) { //$NON-NLS-1$
        name = name.substring(0, name.lastIndexOf(".java")); //$NON-NLS-1$
    } else if (name.endsWith(".class")) { //$NON-NLS-1$
        name = name.substring(0, name.lastIndexOf(".class")); //$NON-NLS-1$
    }
    return name;
}

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

License:Open Source License

/**
 * @param childEl//from  w ww . j  ava2s .co  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:de.loskutov.eclipse.jdepend.preferences.JDependPreferencePage.java

License:Open Source License

protected void addPackage() {
    Shell shell = getShell();//ww  w . java2 s  .c o m
    ElementListSelectionDialog dialog = null;
    try {
        dialog = createAllPackagesDialog(shell, null, true);
    } catch (JavaModelException jme) {
        String title = JDepend4EclipsePlugin
                .getResourceString("JDependPreferencePage.Add_package_to_package_filters"); //$NON-NLS-1$
        String message = JDepend4EclipsePlugin.getResourceString(
                "JDependPreferencePage.Could_not_open_package_selection_dialog_for_package_filters"); //$NON-NLS-1$
        JDepend4EclipsePlugin.logError(jme, title + message);
        return;
    }

    dialog.setTitle(
            JDepend4EclipsePlugin.getResourceString("JDependPreferencePage.Add_package_to_package_filters")); //$NON-NLS-1$
    dialog.setMessage(JDepend4EclipsePlugin
            .getResourceString("JDependPreferencePage.Select_a_package_to_filter_for_JDepend")); //$NON-NLS-1$
    dialog.setMultipleSelection(true);
    if (dialog.open() == IDialogConstants.CANCEL_ID) {
        return;
    }

    Object[] packages = dialog.getResult();
    if (packages != null) {
        for (int i = 0; i < packages.length; i++) {
            IJavaElement pkg = (IJavaElement) packages[i];

            String filter = pkg.getElementName();
            if (filter.length() < 1) {
                filter = JDepend4EclipsePlugin.getResourceString("JDependPreferencePage.default_package"); //$NON-NLS-1$
            } else {
                filter += ".*"; //$NON-NLS-1$
            }
            fStepFilterContentProvider.addFilter(filter, true);
        }
    }
}

From source file:de.loskutov.eclipse.jdepend.views.TreeObject.java

License:Open Source License

public static String getJavaPackageName(IJavaElement resource) {
    String name = resource == null ? "" : resource.getElementName(); //$NON-NLS-1$
    if (resource == null || name.length() == 0) {
        return name;
    }/*  w  w  w.java 2  s.  c o m*/
    int type = resource.getElementType();
    if (type == IJavaElement.PACKAGE_FRAGMENT) {
        return name;
    }
    if (type == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
        return DEFAULT_PACKAGE_NAME;
    }
    IJavaElement ancestor = resource.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    if (ancestor != null) {
        return ancestor.getElementName();
    }
    return ""; //$NON-NLS-1$
}

From source file:de.plugins.eclipse.depclipse.preferences.JDependPreferencePage.java

License:Open Source License

protected void addPackage() {
    Shell shell = getShell();/*from   w  w  w  .j  a  va2 s  .  c  o  m*/
    ElementListSelectionDialog dialog = null;
    try {
        dialog = createAllPackagesDialog(shell, null, true);
    } catch (JavaModelException jme) {
        String title = "Add package to package filters"; //$NON-NLS-1$
        String message = "Could not open package selection dialog for package filters"; //$NON-NLS-1$
        DepclipsePlugin.logError(jme, title + message);
        return;
    }

    dialog.setTitle("Add package to package filters"); //$NON-NLS-1$
    dialog.setMessage(Messages.JDependPreferencePage_Select_a_package_to_filter_for_JDepend);
    dialog.setMultipleSelection(true);
    if (dialog.open() == IDialogConstants.CANCEL_ID) {
        return;
    }

    Object[] packages = dialog.getResult();
    if (packages != null) {
        for (int i = 0; i < packages.length; i++) {
            IJavaElement pkg = (IJavaElement) packages[i];

            String filter = pkg.getElementName();
            if (filter.length() < 1) {
                filter = "(default package)"; //$NON-NLS-1$
            } else {
                filter += ".*"; //$NON-NLS-1$
            }
            fStepFilterContentProvider.addFilter(filter, true);
        }
    }
}

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

License:Open Source License

private static MarkerParameter createMarkerParameter(IJavaProject project, BugInstance bug) {
    IJavaElement type = null;
    WorkItem resource = null;/*from   w w w. j a  v  a2 s .  c  om*/
    try {
        type = getJavaElement(bug, project);
        if (type != null) {
            resource = new WorkItem(type);
        }
    } catch (JavaModelException e1) {
        FindbugsPlugin.getDefault().logException(e1, "Could not find Java type for FindBugs warning");
    }
    if (resource == null) {
        if (Reporter.DEBUG) {
            reportNoResourceFound(bug);
        }
        return null;
    }

    // default - first class line
    int primaryLine = bug.getPrimarySourceLineAnnotation().getStartLine();

    // FindBugs needs originally generated primary line in order to find the
    // bug again.
    // Sometimes this primary line is <= 0, which causes Eclipse editor to
    // ignore it
    // So we check if we can replace the "wrong" primary line with a
    // "better" start line
    // If not, we just provide two values - one for Eclipse, another for
    // FindBugs itself.

    int startLine = DONT_KNOW_LINE;

    // only update if we don't already tried it
    if (primaryLine != DONT_KNOW_LINE) {
        // XXX "first line of a file" is too simplistic. What if we have inner types?
        if (primaryLine <= 1 && type instanceof IType) {
            IType iType = (IType) type;
            try {
                startLine = getLineStart(iType);
                if (startLine > 0) {
                    if (Reporter.DEBUG) {
                        System.out.println(
                                "4. Fixed start line to: " + startLine + " on " + type.getElementName());
                    }
                }
            } catch (JavaModelException e1) {
                FindbugsPlugin.getDefault().logException(e1,
                        "Could not find source line for Java type " + type + "for FindBugs warning: " + bug);
            }
        }
    }

    if (primaryLine <= 0 && startLine <= 0) {
        // We have to provide line number, otherwise editor wouldn't show it
        startLine = 1;
        if (Reporter.DEBUG) {
            System.out.println("5. Fixed start line to *default* (1) on " + type);
        }
    }

    MarkerParameter parameter;
    if (startLine > 0) {
        parameter = new MarkerParameter(bug, resource, startLine, primaryLine);
    } else {
        parameter = new MarkerParameter(bug, resource, primaryLine, primaryLine);
    }
    //        if (Reporter.DEBUG) {
    //            System.out
    //            .println("Creating marker for " + resource.getPath() + ": line " + parameter.primaryLine + bug.getMessage());
    //        }
    return parameter;
}

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";
    }/*  w  ww .ja v  a2s. com*/
    return sourceFileStr;
}

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

License:Open Source License

private static int findInnerClassSourceLine(IJavaElement type, String name) throws JavaModelException {
    String elemName = type.getElementName();
    if (name.equals(elemName)) {
        if (type instanceof IType) {
            return getLineStart((IType) type);
        }//w w  w .  j a  v a2 s  .  co m
    }
    if (type instanceof IParent) {
        IJavaElement[] children = ((IParent) type).getChildren();
        for (int i = 0; i < children.length; i++) {
            // recursive call
            int line = findInnerClassSourceLine(children[i], name);
            if (line > 0) {
                return line;
            }
        }
    }
    return DONT_KNOW_LINE;
}

From source file:de.tobject.findbugs.view.properties.PropPageTitleProvider.java

License:Open Source License

String getTitle(IJavaElement elem) {
    if (elem == null) {
        return null;
    }//from  ww w  . j a v a 2s  .co  m
    StringBuilder sb = new StringBuilder("Class: ");
    sb.append(elem.getElementName());
    return sb.toString();
}

From source file:dynamicrefactoring.util.selection.TextSelectionInfo.java

License:Open Source License

/**
 * @see SelectionInfo#isClassSelection()
 *//*from   w ww .ja  v a 2s  .  c  o  m*/
@Override
public boolean isClassSelection() {
    // Se debe haber seleccionado un fragmento que Eclipse considere parte
    // de una seleccin de clase.
    if (getSelectionType().equals(TYPE_NAME)) {
        // Eclipse considera tambin las clusulas de importacin como 
        // seleccin de clase. Hay que filtrar esos casos.
        IJavaElement selectedElement = getSelectedJavaElement();
        if (selectedElement != null) {
            String selectedElementName = selectedElement.getElementName();
            ICompilationUnit unit = getCompilationUnit();
            if (unit != null && unit.findPrimaryType() != null) {
                String typeName = unit.findPrimaryType().getElementName();
                return typeName.equals(selectedElementName);
            }
        }
    }
    return false;
}