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:org.python.pydev.editor.codecompletion.revisited.javaintegration.AbstractJavaClassModule.java

License:Open Source License

/**
 * Create a proposal collector that's able to gather the passed completions/related java elements and adds 
 * them to the passed 'ret' parameter/*from w  w  w .  ja v a 2  s. c o  m*/
 * 
 * @param filterCompletionName may be null or a name to which we want to match the java element name (so, only
 * a java element with an exact match of its name to filterCompletionName is added).
 * 
 * @param ret the placeholder for the found java elements and completion proposals
 * @param unit the ICompilationUnit that's used (must be passed in the CompletionProposalCollector constructor).
 * @return the collector that will gather the completions (note that it'll keep the 'ret' placeholder alive unti it's 
 * garbage-collected.
 */
protected CompletionProposalCollector createCollector(final String filterCompletionName,
        final List<Tuple<IJavaElement, CompletionProposal>> ret, ICompilationUnit unit) {
    CompletionProposalCollector collector = new CompletionProposalCollector(unit) {

        /**
         * Override the java proposal creation to always return null, as we'll keep just what we actually need.
         */
        @SuppressWarnings("restriction")
        @Override
        public IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
            IJavaCompletionProposal javaCompletionProposal = super.createJavaCompletionProposal(proposal);
            if (javaCompletionProposal instanceof AbstractJavaCompletionProposal) {
                AbstractJavaCompletionProposal prop = (AbstractJavaCompletionProposal) javaCompletionProposal;
                IJavaElement javaElement = prop.getJavaElement();
                if (javaElement != null) {

                    if (filterCompletionName == null) {
                        ret.add(new Tuple<IJavaElement, CompletionProposal>(javaElement, proposal));
                        return null;
                    }

                    if (javaElement.getElementName().equals(filterCompletionName)) {
                        ret.add(new Tuple<IJavaElement, CompletionProposal>(javaElement, proposal));
                        return null;
                    }

                }
            }
            return null;
        }
    };
    return collector;
}

From source file:org.python.pydev.editor.codecompletion.revisited.javaintegration.JavaProjectModulesManager.java

License:Open Source License

/**
 * This method passes through all the java packages and calls the filter callback passed
 * on each package found./*  ww w .  jav a2  s.c  om*/
 *
 * If true is returned on the callback, the children of each package (classes) will also be visited,
 * otherwise, they'll be skipped.
 */
private void filterJavaPackages(IFilter filter) {
    IClasspathEntry[] rawClasspath;
    try {
        rawClasspath = this.javaProject.getRawClasspath();
        FastStringBuffer buffer = new FastStringBuffer();
        for (IClasspathEntry entry : rawClasspath) {
            int entryKind = entry.getEntryKind();
            IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(entry);
            if (entryKind != IClasspathEntry.CPE_CONTAINER) {
                //ignore if it's in the system classpath...
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(resolvedClasspathEntry);

                //get the package roots
                for (IPackageFragmentRoot root : roots) {
                    IJavaElement[] children = root.getChildren();

                    //get the actual packages
                    for (IJavaElement child : children) {
                        IPackageFragment childPackage = (IPackageFragment) child;
                        String elementName = childPackage.getElementName();

                        //and if the java package is 'accepted'
                        if (filter.accept(elementName, root, childPackage)) {
                            buffer.clear();
                            buffer.append(elementName);
                            int packageNameLen = buffer.length();
                            if (packageNameLen > 0) {
                                buffer.append('.');
                                packageNameLen++;
                            }

                            //traverse its classes
                            for (IJavaElement class_ : childPackage.getChildren()) {
                                buffer.append(FullRepIterable.getFirstPart(class_.getElementName()));
                                filter.accept(buffer.toString(), root, class_);
                                buffer.setCount(packageNameLen); //leave only the package part for the next append
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.rdkit.knime.wizards.RDKitNodesWizardsPage.java

License:Open Source License

/**
 * Determines based on the user selection in the Eclipse navigation pane what
 * package the user has selected./*from  w  w  w .j  a v  a2s  . c o m*/
 *
 * @return Selected package or empty string, if unknown.
 */
private String getSelectedPackage() {
    if (m_selection == null || m_selection.isEmpty()) {
        return "";
    }

    Object o = m_selection.getFirstElement();
    if (o instanceof IJavaElement) {
        if (o instanceof IPackageFragment) {
            return ((IPackageFragment) o).getElementName();
        } else {
            IJavaElement je = (IJavaElement) o;
            do {
                je = je.getParent();
            } while (je != null && !(je instanceof IPackageFragment));

            return (je == null ? "" : je.getElementName());
        }
    }

    return "";
}

From source file:org.seasar.dbflute.emecha.eclipse.plugin.dfassist.link.OutsideSqlFileHyperlinkDetector.java

License:Apache License

/**
 * @see org.eclipse.jdt.internal.ui.javaeditor.JavaElementHyperlinkDetector#addHyperlinks(org.eclipse.jface.text.IRegion, org.eclipse.jdt.ui.actions.SelectionDispatchAction, org.eclipse.jdt.core.IJavaElement, boolean, org.eclipse.ui.texteditor.ITextEditor)
 */// w  ww  .  java2  s  .  c o m
@Override
protected void addHyperlinks(List<IHyperlink> hyperlinksCollector, IRegion wordRegion,
        SelectionDispatchAction openAction, IJavaElement element, boolean qualify, JavaEditor editor) {

    if (element instanceof IType) {
        String packageName = ((IType) element).getPackageFragment().getElementName();
        if (packageName.endsWith(".exbhv.pmbean") || packageName.endsWith(".bsbhv.pmbean")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IType) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
        } else if (packageName.endsWith(".exentity.customize") || packageName.endsWith(".bsentity.customize")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IType) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
        }
    } else if (element instanceof IField) {
        if (element.getElementName().startsWith("PATH_")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IField) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
        }
    }
}

From source file:org.seasar.dbflute.emecha.eclipse.plugin.dfassist.link.OutsideSqlHyperlinkDetector.java

License:Apache License

/**
 * @see org.eclipse.jdt.internal.ui.javaeditor.JavaElementHyperlinkDetector#addHyperlinks(org.eclipse.jface.text.IRegion, org.eclipse.jdt.ui.actions.SelectionDispatchAction, org.eclipse.jdt.core.IJavaElement, boolean, org.eclipse.ui.texteditor.ITextEditor)
 */// www .  j av a2 s.c om
@Override
protected void addHyperlinks(List<IHyperlink> hyperlinksCollector, IRegion wordRegion,
        SelectionDispatchAction openAction, IJavaElement element, boolean qualify, JavaEditor editor) {

    if (element instanceof IType) {
        String packageName = ((IType) element).getPackageFragment().getElementName();
        if (packageName.endsWith(".exbhv.pmbean") || packageName.endsWith(".bsbhv.pmbean")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IType) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
            CustomizeEntityHyperlink entityLink = new CustomizeEntityHyperlink(wordRegion, openAction,
                    (IType) element, qualify);
            if (entityLink.existEntityType()) {
                hyperlinksCollector.add(entityLink);
            }
        } else if (packageName.endsWith(".exentity.customize") || packageName.endsWith(".bsentity.customize")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IType) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
            ParameterBeanHyperlink pmbLink = new ParameterBeanHyperlink(wordRegion, openAction, (IType) element,
                    qualify);
            if (pmbLink.existPmbType()) {
                hyperlinksCollector.add(pmbLink);
            }
        }
    } else if (element instanceof IField) {
        if (element.getElementName().startsWith("PATH_")) {
            SqlFileHyperlink sqlLink = new SqlFileHyperlink(wordRegion, openAction, (IField) element, qualify);
            if (sqlLink.existSqlFile()) {
                hyperlinksCollector.add(sqlLink);
            }
        }
    }
}

From source file:org.seasar.kijimuna.core.internal.dicon.model.autoregister.ComponentAutoRegister.java

License:Apache License

public void registerAll() {
    try {//from   www. ja v a2s  . co m
        for (int i = 0; i < referenceClasses.size(); i++) {
            String referenceClass = (String) referenceClasses.get(i);
            IType type = getProject().findType(referenceClass);
            if (!type.exists()) {
                return;
            }
            IJavaElement parent = type.getPackageFragment().getParent();
            if (parent.getElementName().endsWith(".jar")) {
                // JAR??
                register = new JarComponentAutoRegister();
                ((JarComponentAutoRegister) register).setReferenceClass(referenceClass);
            } else {
                // JAR????
                register = new FileSystemComponentAutoRegister();
            }

            register.setProject(getProject());

            List classPatterns = getClassPatterns();
            for (int j = 0; j < classPatterns.size(); j++) {
                ClassPattern pattern = (ClassPattern) classPatterns.get(j);
                register.addClassPattern(pattern.getPackageName(), pattern.getShortClassNames());
            }

            List ignorePatterns = getIgnoreClassPatterns();
            for (int j = 0; j < ignorePatterns.size(); j++) {
                ClassPattern pattern = (ClassPattern) ignorePatterns.get(j);
                register.addIgnoreClassPattern(pattern.getPackageName(), pattern.getShortClassNames());
            }

            register.registerAll();
            setComponentMap(register.getComponentMap());
        }
    } catch (Exception ex) {
        KijimunaCore.reportException(ex);
    }
}

From source file:org.seasar.s2daoplugin.S2DaoNamingConventions.java

License:Apache License

/**
 * SQLt@C?ADAO??p^?[S?B//from  w  ww .  ja va 2  s. co  m
 * pz?A?\?????B
 * 
 * @return {{"PackageFragmentName", "DaoTypeName", "MethodName"}, ...}
 */
public static String[][] resolveDao(IFile sql) {
    if (sql == null || !sql.getName().toLowerCase().endsWith(SQL_EXTENSION)
            || !JavaProjectUtil.isInSourceFolder(sql)) {
        return EMPTY_RESOLVE;
    }
    IJavaElement element = JavaCore.create(sql.getParent());
    String packageName = element instanceof IPackageFragment ? element.getElementName() : "";
    List ret = new ArrayList();
    String name = sql.getName();
    if (hasSuffix(name)) {
        ret.addAll(createDaoNames(packageName, removeSuffix(removeExtension(name))));
    }
    ret.addAll(createDaoNames(packageName, removeExtension(name)));
    String[][] result = new String[ret.size()][];
    for (int i = 0; i < ret.size(); i++) {
        result[i] = (String[]) ret.get(i);
    }
    return result;
}

From source file:org.seasar.s2jsfplugin.model.autoregister.ComponentAutoRegister.java

License:Apache License

public void registerAll() {
    try {/*from www  . j  a v  a 2 s .  c o  m*/
        for (int i = 0; i < referenceClasses.size(); i++) {
            String referenceClass = (String) referenceClasses.get(i);
            IType type = getProject().findType(referenceClass);
            if (!type.exists()) {
                return;
            }
            IJavaElement parent = type.getPackageFragment().getParent();
            if (parent.getElementName().endsWith(".jar")) {
                // JAR??
                register = new JarAutoRegister();
                ((JarAutoRegister) register).setReferenceClass(referenceClass);
            } else {
                // JAR??
                register = new FileSystemAutoRegister();
            }

            register.setProject(getProject());
            register.setScope(getScope());

            List classPatterns = getClassPatterns();
            for (int j = 0; j < classPatterns.size(); j++) {
                ClassPattern pattern = (ClassPattern) classPatterns.get(j);
                register.addClassPattern(pattern.getPackageName(), pattern.getShortClassNames());
            }

            List ignorePatterns = getIgnoreClassPatterns();
            for (int j = 0; j < ignorePatterns.size(); j++) {
                ClassPattern pattern = (ClassPattern) ignorePatterns.get(j);
                register.addIgnoreClassPattern(pattern.getPackageName(), pattern.getShortClassNames());
            }

            register.registerAll();
        }
    } catch (Exception ex) {
        Util.logException(ex);
    }
}

From source file:org.seasar.s2jsfplugin.model.autoregister.JarAutoRegister.java

License:Apache License

public void registerAll() {
    try {//from w  w  w .j ava  2s.c  o  m
        IType type = getProject().findType(referenceClass);
        if (!type.exists()) {
            return;
        }
        IJavaElement parent = type.getPackageFragment().getParent();
        if (!parent.getElementName().endsWith(".jar")) {
            return;
        }
        IContainer container = parent.getResource().getParent();
        File basedir = container.getLocation().makeAbsolute().toFile();
        File[] jars = basedir.listFiles();
        for (int i = 0; i < jars.length; ++i) {
            if (!isAppliedJar(jars[i].getName())) {
                continue;
            }
            JarFile jarFile = new JarFile(jars[i]);
            Enumeration enumeration = jarFile.entries();
            while (enumeration.hasMoreElements()) {
                final JarEntry entry = (JarEntry) enumeration.nextElement();
                final String entryName = entry.getName().replace('\\', '/');
                if (entryName.endsWith(".class")) {
                    final String className = entryName.substring(0, entryName.length() - ".class".length())
                            .replace('/', '.');
                    final int pos = className.lastIndexOf('.');
                    final String packageName = (pos == -1) ? null : className.substring(0, pos);
                    final String shortClassName = (pos == -1) ? className : className.substring(pos + 1);

                    processClass(packageName, shortClassName);
                }
            }
        }
    } catch (Exception ex) {
        Util.logException(ex);
    }
}

From source file:org.sf.feeling.decompiler.actions.ExportSourceAction.java

License:Open Source License

private void exportPackageSources(IProgressMonitor monitor, final String decompilerType, final boolean reuseBuf,
        final boolean always, final String projectFile, final IJavaElement[] children, List exceptions)
        throws InvocationTargetException, InterruptedException {
    monitor.beginTask(Messages.getString("ExportSourceAction.Task.Begin"), //$NON-NLS-1$
            1000000);//from   w ww .  ja  va 2  s. c o m

    final File workingDir = new File(
            JavaDecompilerPlugin.getDefault().getPreferenceStore().getString(JavaDecompilerPlugin.TEMP_DIR)
                    + "/export/" //$NON-NLS-1$
                    + System.currentTimeMillis());

    Map classes = new HashMap();
    for (int i = 0; i < children.length; i++) {
        if (monitor.isCanceled())
            return;
        IJavaElement child = children[i];
        try {
            collectClasses(child, classes, monitor);
        } catch (JavaModelException e) {
            IStatus status = new Status(IStatus.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
                    Messages.getString("ExportSourceAction.Status.Error.CollectPackage"), //$NON-NLS-1$
                    e);
            exceptions.add(status);
        }
    }

    monitor.worked(20000);

    IPackageFragment[] pkgs = (IPackageFragment[]) classes.keySet().toArray(new IPackageFragment[0]);
    int step = 880000 / pkgs.length;
    for (int i = 0; i < pkgs.length; i++) {
        if (monitor.isCanceled())
            return;
        IPackageFragment pkg = pkgs[i];
        List clazzs = (List) classes.get(pkg);
        if (clazzs.size() == 0) {
            monitor.worked(step);
            continue;
        }
        int total = 0;
        int classStep = step / clazzs.size();
        for (int j = 0; j < clazzs.size(); j++) {
            if (monitor.isCanceled())
                return;
            IJavaElement clazz = (IJavaElement) clazzs.get(j);
            if (clazz instanceof IClassFile && clazz.getParent() instanceof IPackageFragment) {
                String className = pkg.getElementName();
                if (pkg.getElementName().length() > 0) {
                    className += ("." + clazz.getElementName()); //$NON-NLS-1$
                }
                monitor.subTask(className);
                try {
                    IClassFile cf = (IClassFile) clazz;
                    if (cf.getElementName().indexOf('$') != -1)
                        continue;
                    String result = DecompileUtil.decompile(cf, decompilerType, always, reuseBuf, true);
                    if (result != null) {
                        String packageName = pkg.getElementName().replace('.', '/');
                        if (packageName.length() > 0)
                            packageName += "/"; //$NON-NLS-1$
                        FileUtil.writeToFile(
                                new File(workingDir, packageName + cf.getElementName().replaceAll("\\..+", "") //$NON-NLS-1$ //$NON-NLS-2$
                                        + ".java"), //$NON-NLS-1$
                                result);
                    } else {
                        IStatus status = new Status(IStatus.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
                                Messages.getFormattedString("ExportSourceAction.Status.Error.DecompileFailed", //$NON-NLS-1$
                                        new String[] { className }));
                        throw new CoreException(status);
                    }
                } catch (Exception e) {
                    IStatus status = new Status(IStatus.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
                            Messages.getFormattedString("ExportSourceAction.Status.Error.DecompileFailed", //$NON-NLS-1$
                                    new String[] { className }));
                    exceptions.add(status);
                }

            }
            total += classStep;
            monitor.worked(classStep);
        }
        if (total < step) {
            monitor.worked(step - total);
        }
    }
    try {
        int exportStep = 80000 / pkgs.length;
        monitor.setTaskName(Messages.getString("ExportSourceAction.Task.ExportSource")); //$NON-NLS-1$
        monitor.subTask(""); //$NON-NLS-1$
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(projectFile)));
        zos.setLevel(Deflater.BEST_SPEED);
        FileUtil.recursiveZip(monitor, zos, workingDir, "", //$NON-NLS-1$
                null, exportStep);
        monitor.subTask(""); //$NON-NLS-1$
        zos.close();

        int total = exportStep * pkgs.length;
        if (total < 80000) {
            monitor.worked(80000 - total);
        }

        int deleteStep = 20000 / pkgs.length;
        monitor.setTaskName(Messages.getString("ExportSourceAction.Task.Clean")); //$NON-NLS-1$
        monitor.subTask(""); //$NON-NLS-1$
        FileUtil.deleteDirectory(monitor, workingDir.getParentFile(), deleteStep);
        total = deleteStep * pkgs.length;
        if (total < 20000) {
            monitor.worked(20000 - total);
        }
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, JavaDecompilerPlugin.PLUGIN_ID,
                Messages.getString("ExportSourceAction.Status.Error.ExportFailed"), //$NON-NLS-1$
                e);
        exceptions.add(status);
    }
}