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:es.bsc.servicess.ide.wizards.ServiceSsCommonWizardPage.java

License:Apache License

protected void initPage(IJavaElement elem) {

    IJavaProject project = null;/*  w  ww . j  a v a 2  s  .  c  om*/
    IPackageFragment pack = null;
    IType typeName = null;
    if (elem != null) {
        // evaluate the enclosing type
        project = elem.getJavaProject();
        pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        System.out.println("Element name:" + elem.getElementName() + "type: " + elem.getElementType());
        if (elem.getElementType() == JavaElement.COMPILATION_UNIT) {
            typeName = ((ICompilationUnit) elem).findPrimaryType();
        } else if (elem.getElementType() == JavaElement.TYPE) {
            typeName = (IType) elem;
        }
    }

    setPackageFragment(pack, true);

    setClassName(typeName, true);

}

From source file:es.bsc.servicess.ide.wizards.ServiceSsNewElementWizardPage.java

License:Apache License

protected void initPage(IJavaElement elem) {

    IJavaProject project = null;//w  ww.j a  v a2s . co m
    IPackageFragment pack = null;
    IType typeName = null;
    if (elem != null) {
        // evaluate the enclosing type
        project = elem.getJavaProject();
        pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        System.out.println("Element name:" + elem.getElementName());
        if (elem.getElementType() == JavaElement.COMPILATION_UNIT) {
            typeName = ((ICompilationUnit) elem).findPrimaryType();
        } else if (elem.getElementType() == JavaElement.TYPE) {
            typeName = (IType) elem;
        }
    }

    setPackageFragment(pack, true);

    setClassName(typeName, true);

}

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Creates a new package in the source folder corresponding to an item.
 * /*  www . j a va  2s.  com*/
 * @param item
 *            the item
 * @param packageName
 *            the package name
 * @param monitor
 *            the monitor
 * 
 * @return the i package fragment
 * 
 * @throws CoreException
 *             the core exception
 */
public static IPackageFragment createPackage(Item item, String packageName, IProgressMonitor monitor)
        throws CoreException {

    IResource resource = getResource(item);
    IJavaElement element = JavaCore.create(resource);

    /*
     * The resource associated with the item is already a package
     */
    if (element instanceof IPackageFragment) {
        if (packageName == null || element.getElementName().equals(packageName)) {
            return (IPackageFragment) element;
        }
    }

    if (packageName == null) {
        throw new CoreException(new Status(Status.ERROR, WSJavaPlugin.PLUGIN_ID, 0,
                "Package name is null in createPackage !!!.", null));
    }

    /*
     * The resource is a source folder
     */
    if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;
        return root.createPackageFragment(packageName, true, monitor);
    }

    /*
     * The resource is a project or a non source folder, get the associated
     * source folder
     */
    IPackageFragmentRoot sources = getPackageFragmentRoot(item);
    if (sources == null) {
        throw new CoreException(
                new Status(Status.ERROR, WSJavaPlugin.PLUGIN_ID, 0, "Cannot find the sources folder.", null));
    }

    IProject project = getProject(item);
    if (!project.exists()) {
        project.create(monitor);
    }

    if (!sources.exists()) {
        getDefaultSourceFolder(item).create(true, true, monitor);
    }

    return sources.createPackageFragment(packageName, false, monitor);
}

From source file:fr.atelechev.buildergen.plugin.TopLevelClassWriter.java

License:Open Source License

private boolean elementIsTypeWithTargetName(IJavaElement element) {
    final int javaExtensionIndex = getSelectedFile().getElementName().indexOf(".java");
    final String analyzedUnitName = getSelectedFile().getElementName().substring(0, javaExtensionIndex);
    return element.getElementType() == IJavaElement.TYPE && element.getElementName().equals(analyzedUnitName);
}

From source file:fr.imag.adele.cadse.cadseg.eclipse.RenameJavaClassMappingOperartion.java

License:Apache License

protected void refactoringRename(String id, String... params) {
    try {/*from   w  w w.ja  v a 2s.  c o m*/
        IJavaElement oldElement = getJavaElement(_oldcontext);
        IJavaElement newElement = getJavaElement(_newcontext);

        Map<String, String> arguments = new HashMap<String, String>();
        for (int i = 0; i < params.length;) {
            String k = params[i++];
            String v = params[i++];
            arguments.put(k, v);
        }
        arguments.put("name", newElement.getElementName());
        arguments.put("input", oldElement.getHandleIdentifier());

        RenameJavaElementDescriptor javaElementDescriptor = new RenameJavaElementDescriptor(id,
                id == IJavaRefactorings.RENAME_JAVA_PROJECT ? null
                        : oldElement.getResource().getProject().getName(),
                _desc, _desc, arguments, 0);
        javaElementDescriptor.setJavaElement(oldElement);
        javaElementDescriptor.setNewName(newElement.getElementName());

        int type = oldElement.getElementType();
        switch (type) {
        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.TYPE:
            javaElementDescriptor.setUpdateQualifiedNames(true);
            break;
        default:
        }
        if (oldElement.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
            javaElementDescriptor.setUpdateReferences(true);
        }

        RenameSupport renameSupport = RenameSupport.create(javaElementDescriptor);
        Shell parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        renameSupport.perform(parent, new ProgressMonitorDialog(parent));

    } catch (CoreException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:hydrograph.ui.expression.editor.jar.util.BuildExpressionEditorDataSturcture.java

License:Apache License

@SuppressWarnings("restriction")
public boolean loadUserDefinedClassesInClassRepo(String jarFileName, String packageName) {
    IPackageFragmentRoot iPackageFragmentRoot = getIPackageFragment(jarFileName);
    try {/* ww  w .ja  v a  2 s .co m*/
        if (iPackageFragmentRoot != null) {
            IPackageFragment fragment = iPackageFragmentRoot.getPackageFragment(packageName);
            if (fragment != null
                    && StringUtils.equals(fragment.getClass().getSimpleName(), JAR_PACKAGE_FRAGMENT)) {
                for (IClassFile element : fragment.getClassFiles()) {
                    if (isValidCLassName(element.getElementName()))
                        ClassRepo.INSTANCE.addClass(element, jarFileName, packageName, true);
                }
                return true;
            } else {
                for (IJavaElement element : fragment.getChildren()) {
                    if (element instanceof ICompilationUnit) {
                        for (IJavaElement javaElement : ((ICompilationUnit) element).getChildren()) {
                            if (javaElement instanceof SourceType
                                    && isValidCLassName(javaElement.getElementName())) {
                                ClassRepo.INSTANCE.addClass((SourceType) javaElement, jarFileName, packageName,
                                        true);
                            }
                        }
                    }
                }
                return true;
            }
        } else {
            LOGGER.warn("Package not found in jar " + iPackageFragmentRoot.getElementName(), "ERROR");
        }
    } catch (JavaModelException e) {
        LOGGER.error("Error occurred while loading class from jar", e);
    }
    return false;
}

From source file:hydrograph.ui.propertywindow.widgets.utility.FilterOperationClassUtility.java

License:Apache License

/**
 * Open file editor to view java file.//from ww w.  j a  v  a 2 s. c om
 * 
 * @param fileName
 *            the file name
 * @return true, if successful
 */
@SuppressWarnings("restriction")
public boolean openFileEditor(Text filePath, String pathFile) {
    String fullQualifiedClassName = filePath.getText();
    try {
        logger.debug("Searching " + fullQualifiedClassName + " in project's source folder");
        PackageFragment packageFragment = null;
        String[] packages = StringUtils.split(fullQualifiedClassName, ".");
        String className = fullQualifiedClassName;
        String packageStructure = "";
        IFile javaFile = null;
        if (packages.length > 1) {
            className = packages[packages.length - 1];
            packageStructure = StringUtils.replace(fullQualifiedClassName, "." + className, "");
        }
        IFileEditorInput editorInput = (IFileEditorInput) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getActivePage().getActiveEditor().getEditorInput();
        IProject project = editorInput.getFile().getProject();
        IJavaProject iJavaProject = JavaCore.create(project);
        IPackageFragmentRoot packageFragmentRoot = iJavaProject
                .getPackageFragmentRoot(project.getFolder(Constants.ProjectSupport_SRC));
        if (packageFragmentRoot != null) {
            for (IJavaElement iJavaElement : packageFragmentRoot.getChildren()) {
                if (iJavaElement instanceof PackageFragment
                        && StringUtils.equals(iJavaElement.getElementName(), packageStructure)) {
                    packageFragment = (PackageFragment) iJavaElement;
                    break;
                }
            }
        }
        if (packageFragment != null) {
            for (IJavaElement element : packageFragment.getChildren()) {
                if (element instanceof CompilationUnit && StringUtils.equalsIgnoreCase(element.getElementName(),
                        className + Constants.JAVA_EXTENSION)) {
                    javaFile = (IFile) element.getCorrespondingResource();
                    break;
                }
            }
        }
        if (javaFile != null && javaFile.exists()) {
            IFileStore fileStore = EFS.getLocalFileSystem().getStore(javaFile.getLocationURI());
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            IDE.openEditorOnFileStore(page, fileStore);
            return true;
        }
    } catch (Exception e) {
        logger.error("Fail to open file");
        return false;
    }
    return false;

}

From source file:in.software.analytics.parichayana.core.internal.builder.ParichayanaBuilder.java

License:Open Source License

/**
 * @param unit//w w  w  .  j av  a2 s  .  c  om
 * @return
 */
private String getFullyQualifiedName(ICompilationUnit unit) {
    String name = unit.getElementName();
    IJavaElement parent = unit.getParent();
    if (parent instanceof IPackageFragment) {
        String n = parent.getElementName();
        if (n != null && !n.isEmpty()) {
            name = n + "." + name;
        }
    }
    if (name.endsWith(".java")) {
        name = name.substring(0, name.length() - 5);
    }
    return name;
}

From source file:iot.jcypher.eclipse.JCypherCompletionProposalComputer.java

License:Apache License

private List<ICompletionProposal> buildJCypherProposals(List<ICompletionProposal> proposals) {
    List<ICompletionProposal> ret = new ArrayList<>(proposals.size());
    List<String> classNames = new ArrayList<String>();
    List<String> classNames_ext = new ArrayList<String>();
    for (ICompletionProposal prop : proposals) {
        if (prop instanceof IJavaCompletionProposal) {
            if (prop instanceof JavaMethodCompletionProposal) {
                JavaMethodCompletionProposal jmcp = (JavaMethodCompletionProposal) prop;
                IJavaElement elem = jmcp.getJavaElement();
                IJavaElement classfile = elem.getParent().getParent();
                if (classfile.getElementType() == IJavaElement.CLASS_FILE
                        || classfile.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    IJavaElement pkg = classfile.getParent();
                    if (JCypherPackages.addsToProposal(pkg.getElementName())) {
                        IJavaCompletionProposal delegate = (IJavaCompletionProposal) prop;
                        IJavaElement mthd = ((AbstractJavaCompletionProposal) delegate).getJavaElement();
                        IJavaElement clsfile = elem.getParent().getParent();
                        String className = clsfile.getElementName();
                        if (!classNames_ext.contains(className)) {
                            classNames_ext.add(className);
                            // trim className extension which can either be .class or .java
                            classNames.add(className.substring(0, className.indexOf('.')));
                        }/*ww w.jav a2s.c o  m*/
                        // classNames always contains (class, superClass, superSuperClass, ..)
                        int relevance = ProposalOrderConfig.INSTANCE.getRelevance(classNames.get(0),
                                mthd.getElementName(), JCypherConstants.DEFAULT_RELEVANCE);
                        ret.add(new JCypherCompletionProposal((IJavaCompletionProposal) prop, relevance));
                    }
                }
            }
        }
    }
    if (ret.size() > 0) {
        ProposalOrderConfig.INSTANCE.addSeparators(classNames.get(0), ret, JCypherConstants.DEFAULT_RELEVANCE);
        ret.add(CompletionSeparator.INSTANCE);
    }
    return ret;
}

From source file:jd.ide.eclipse.editors.JDClassFileEditor.java

License:Open Source License

@SuppressWarnings("rawtypes")
protected void setupSourceMapper(IClassFile classFile) {
    try {/*from w  w  w .  j  av  a2 s . c  o m*/
        // Search package fragment root and classPath
        IJavaElement packageFragment = classFile.getParent();
        IJavaElement packageFragmentRoot = packageFragment.getParent();

        if (packageFragmentRoot instanceof PackageFragmentRoot) {
            // Setup a new source mapper.
            PackageFragmentRoot root = (PackageFragmentRoot) packageFragmentRoot;

            // Location of the archive file containing classes.
            IPath basePath = root.getPath();
            File baseFile = basePath.makeAbsolute().toFile();

            if (!baseFile.exists()) {
                IResource resource = root.getCorrespondingResource();
                basePath = resource.getLocation();
                baseFile = basePath.makeAbsolute().toFile();
            }

            // Class path
            String classPath = classFile.getElementName();
            String packageName = packageFragment.getElementName();
            if ((packageName != null) && (packageName.length() > 0))
                classPath = packageName.replace('.', '/') + '/' + classPath;

            // Location of the archive file containing source.
            IPath sourcePath = root.getSourceAttachmentPath();
            if (sourcePath == null)
                sourcePath = basePath;

            // Location of the package fragment root within the zip 
            // (empty specifies the default root).
            IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
            String sourceRootPath;

            if (sourceAttachmentRootPath == null) {
                sourceRootPath = null;
            } else {
                sourceRootPath = sourceAttachmentRootPath.toString();
                if ((sourceRootPath != null) && (sourceRootPath.length() == 0))
                    sourceRootPath = null;
            }

            // Options
            Map options = root.getJavaProject().getOptions(true);

            root.setSourceMapper(new JDSourceMapper(baseFile, sourcePath, sourceRootPath, options));
        }
    } catch (CoreException e) {
        JavaDecompilerPlugin.getDefault().getLog()
                .log(new Status(Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID, 0, e.getMessage(), e));
    }
}