Example usage for org.eclipse.jdt.internal.core CompilationUnit getElementName

List of usage examples for org.eclipse.jdt.internal.core CompilationUnit getElementName

Introduction

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

Prototype

@Override
    public String getElementName() 

Source Link

Usage

From source file:com.google.gdt.eclipse.core.reference.logicaljavamodel.UiBinderImportReferenceType.java

License:Open Source License

@Override
public boolean matches(Object javaElement) {

    if (javaElement instanceof IType) {

        return super.matches(javaElement);

    } else if (javaElement instanceof CompilationUnit) {
        // When a java file that is changed is referenced from a ui.xml file,
        // eclipse gives a CompilationUnit instead of an IType, so this
        // logic is needed for this case.

        String enclosingClassName = getFullyQualifiedName();

        // if this LogicalType represents an inner class, we want only the outermost
        // enclosing class, because the given CompilationUnit represents a 
        // java file, and hence an outermost class
        int dollarIndex = enclosingClassName.indexOf('$');
        if (dollarIndex != -1) {
            enclosingClassName = enclosingClassName.substring(0, dollarIndex);
        }/*from  ww  w. j  ava2  s  . com*/

        CompilationUnit cu = ((CompilationUnit) javaElement);

        // CompilationUnit class doesn't just give us the full qualified name
        // of the top-most class... so we must assemble it ourselves...
        IPackageDeclaration[] pkgs;
        try {
            pkgs = cu.getPackageDeclarations();
        } catch (JavaModelException e) {
            return false;
        }

        if (pkgs.length > 0) {
            // cu.getElementName() returns a filename, so lop off the extension to get the class name
            String className = cu.getElementName().substring(0, cu.getElementName().indexOf('.'));
            String cuName = pkgs[0].getElementName() + "." + className;
            if (enclosingClassName.equals(cuName)) {
                return true;
            }
        }
    }

    return false;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static String getTypeNameAtPosition(CategorizedProblem categorizedProblem, CompilationUnit unit)
        throws JavaModelException {
    IJavaElement elt = unit.getElementAt(categorizedProblem.getSourceStart());
    IType type = elt != null ? (IType) elt.getAncestor(IJavaElement.TYPE) : null;
    if (type == null) {
        // just return the name of the CU
        int dotIndex = unit.getElementName().indexOf('.');
        if (dotIndex > 0) {
            return unit.getElementName().substring(0, dotIndex);
        } else {//from   w  w w  .ja va 2s.c o m
            return unit.getElementName();
        }
    }
    return type.getElementName();
}

From source file:org.ect.reo.diagram.providers.ReoDropTargetListener.java

License:Open Source License

private Component createComponent(ISelection selection) {

    Component component = ReoFactory.eINSTANCE.createComponent();

    if (selection instanceof IStructuredSelection) {

        // We only use the first item in the selection.
        Object element = ((IStructuredSelection) selection).getFirstElement();

        // This is in the Java Perspective.
        if (element instanceof CompilationUnit) {
            CompilationUnit unit = (CompilationUnit) element;

            // class name.
            String name = unit.getElementName();
            if (name.endsWith(".java"))
                name = name.substring(0, name.length() - 5);
            component.setName(name);//from   ww w.j  a va2s  . co m

            // package name.
            /*
            char[][] pname = unit.getPackageName();
            StringBuffer pack = new StringBuffer();
            for (int i=0; i<pname.length; i++) {
               pack.append(pname[i]);
               if (i<pname.length-1) pack.append('.');
            }
            component.setPackage(pack.toString());
            */
        }

        // In the Resource navigator is probably an IFile or so...
        // ...

        else {
            // Otherwise we shouldn't return anything.
            return null;
        }
    }

    return component;
}

From source file:org.evosuite.eclipse.popup.actions.ExtendSuiteEditorAction.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);

    String SUT = "";
    IResource target = null;//from w w  w.j av a  2s . c o m

    System.out.println("Current selection of type " + selection.getClass().getName() + ": " + selection);
    if (selection instanceof TreeSelection) {
        TreeSelection treeSelection = (TreeSelection) selection;
        IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

        // Relies on an internal API, bad juju
        if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
            try {
                org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
                String packageName = "";
                if (compilationUnit.getPackageDeclarations().length > 0) {
                    System.out.println(
                            "Package: " + compilationUnit.getPackageDeclarations()[0].getElementName());
                    packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = compilationUnit.getElementName().replace(".java", "");
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
                target = compilationUnit.getResource();
            } catch (JavaModelException e) {

            }
        }
    } else if (activeEditor instanceof JavaEditor) {
        ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeEditor, false);
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor).getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element;

        try {
            element = root.getElementAt(offset);
            if (element.getElementType() == IJavaElement.METHOD) {
                IJavaElement pDeclaration = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                IPackageFragment pFragment = (IPackageFragment) pDeclaration;
                String packageName = "";
                if (pFragment.getCompilationUnits()[0].getPackageDeclarations().length > 0) {
                    System.out.println("Package: "
                            + pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName());
                    packageName = pFragment.getCompilationUnits()[0].getPackageDeclarations()[0]
                            .getElementName();
                }
                String targetSuite = element.getParent().getElementName();
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
            } else if (element.getElementType() == IJavaElement.TYPE) {
                IType type = ((IType) element);
                System.out.println("Selected class: " + type.getFullyQualifiedName());
                SUT = type.getFullyQualifiedName();
            }

            IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
            target = wroot.findMember(root.getPath());
        } catch (JavaModelException e) {

        }
    }
    if (!SUT.isEmpty() && target != null) {
        IProject proj = target.getProject();
        fixJUnitClassPath(JavaCore.create(proj));
        generateTests(target);
    }

    return null;
}

From source file:org.evosuite.eclipse.popup.actions.GenerateTestsEditorAction.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    // ISelection selection = HandlerUtil.getCurrentSelection(event);
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);

    String SUT = "";
    IResource target = null;/*from   w  w w .  java 2s  .  c  o  m*/
    System.out.println("Current selection of type " + selection.getClass().getName() + ": " + selection);
    if (selection instanceof TreeSelection) {
        TreeSelection treeSelection = (TreeSelection) selection;
        IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

        // Relies on an internal API, bad juju
        if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
            try {
                org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
                String packageName = "";
                if (compilationUnit.getPackageDeclarations().length > 0) {
                    System.out.println(
                            "Package: " + compilationUnit.getPackageDeclarations()[0].getElementName());
                    packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = compilationUnit.getElementName().replace(".java", "");
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
                target = compilationUnit.getResource();
            } catch (JavaModelException e) {

            }
        }
    } else if (activeEditor instanceof JavaEditor) {
        ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeEditor, false);
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor).getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element;

        try {
            element = root.getElementAt(offset);
            if (element == null) {
                ISelection sel2 = HandlerUtil.getCurrentSelection(event);
                System.out.println(
                        "Selected element of type " + sel2.getClass().getName() + ": " + sel2.toString());
            } else if (element.getElementType() == IJavaElement.METHOD) {
                IJavaElement pDeclaration = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                IPackageFragment pFragment = (IPackageFragment) pDeclaration;
                String packageName = "";
                if (pFragment.getCompilationUnits()[0].getPackageDeclarations().length > 0) {
                    System.out.println("Package: "
                            + pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName());
                    packageName = pFragment.getCompilationUnits()[0].getPackageDeclarations()[0]
                            .getElementName();
                }
                String targetSuite = element.getParent().getElementName();
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
            } else if (element.getElementType() == IJavaElement.TYPE) {
                IType type = ((IType) element);
                System.out.println("Selected class: " + type.getFullyQualifiedName());
                SUT = type.getFullyQualifiedName();
            }

            IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
            target = wroot.findMember(EditorUtility.getEditorInputJavaElement(activeEditor, false).getPath());

        } catch (JavaModelException e) {

        }

    }

    if (!SUT.isEmpty() && target != null) {
        IProject proj = target.getProject();
        fixJUnitClassPath(JavaCore.create(proj));
        generateTests(target);
    }

    return null;
}

From source file:sidecarviz.core.MonitorEclipse.java

License:BSD License

/**
 * When the developer selects an item in the package explorer, we monitor it and save the item in a
 * hashmap for later access./*  w w  w.ja va  2 s. c  o  m*/
 * 
 * @param selection
 */
public void gotPackageExplorerSelectionChanged(TreeSelection selection) {
    // if we cast it to a string, the selection is between two brackets [....]
    // the stuff after the selection gives us more information
    Object[] selected = selection.toArray();
    for (Object o : selected) {
        Class<?> selectedItemType = o.getClass();
        if (selectedItemType.equals(PackageFragment.class)) {
            PackageFragment frag = (PackageFragment) o;
            String fragName = frag.getElementName();

            DebugUtils.println("Package: " + fragName);
            // store the element in a hashmap, for later access
            packageExplorerSelectedPackages.put(fragName, frag);

            // TODO: forward to Flash
        } else if (selectedItemType.equals(CompilationUnit.class)) {
            CompilationUnit comp = (CompilationUnit) o;
            String compName = comp.getElementName();
            IPath compPath = comp.getPath();

            DebugUtils.println("Class: " + compName + "   " + compPath);
            // store the element in a hashmap, for later access
            packageExplorerSelectedClasses.put(compName, comp);

            // TODO: forward to Flash
        } else {
            // DebugUtils.println(o.getClass());
            // unhandled
        }
    }
}