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

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

Introduction

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

Prototype

@Override
    public String getElementName() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

/**
 * Performs type search in a binary package.
 *///  www  . ja  v a 2 s .c o m
protected void seekTypesInBinaryPackage(String name, IPackageFragment pkg, boolean partialMatch,
        int acceptFlags, IJavaElementRequestor requestor) {
    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        if (!partialMatch) {
            // exact match
            if (requestor.isCanceled())
                return;
            ClassFile classFile = new ClassFile((PackageFragment) pkg, manager, name);
            if (classFile.existsUsingJarTypeCache()) {
                IType type = classFile.getType();
                if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                    requestor.acceptType(type);
                }
            }
        } else {
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            int length = classFiles.length;
            String unqualifiedName = name;
            int index = name.lastIndexOf('$');
            if (index != -1) {
                //the type name of the inner type
                unqualifiedName = Util.localTypeName(name, index, name.length());
                // unqualifiedName is empty if the name ends with a '$' sign.
                // See http://dev.eclipse.org/bugs/show_bug.cgi?id=14642
            }
            int matchLength = name.length();
            for (int i = 0; i < length; i++) {
                if (requestor.isCanceled())
                    return;
                IJavaElement classFile = classFiles[i];
                // MatchName will never have the extension ".class" and the elementName always will.
                String elementName = classFile.getElementName();
                if (elementName.regionMatches(true /*ignore case*/, 0, name, 0, matchLength)) {
                    IType type = ((ClassFile) classFile).getType();
                    String typeName = type.getElementName();
                    if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type
                        if (nameMatches(unqualifiedName, type, true/*partial match*/)
                                && acceptType(type, acceptFlags, false/*not a source type*/))
                            requestor.acceptType(type);
                    }
                }
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInBinaryPackage += System.currentTimeMillis() - start;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IBinaryType getBinaryInfo(ClassFile classFile, IResource resource) throws CoreException {
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (classFile.isOpen())
        return (IBinaryType) binaryType.getElementInfo(); // reuse the info from the java model cache

    // create a temporary info
    IBinaryType info;/*from  w ww .  j  av  a2s.c om*/
    try {
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
        if (root.isArchive()) {
            // class file in a jar
            String classFileName = classFile.getElementName();
            String classFilePath = Util.concatWith(pkg.names, classFileName, '/');
            ZipFile zipFile = null;
            try {
                zipFile = ((JarPackageFragmentRoot) root).getJar();
                info = ClassFileReader.read(zipFile, classFilePath);
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
            }
        } else {
            // class file in a directory
            info = Util.newClassFileReader(resource);
        }
        if (info == null)
            throw binaryType.newNotPresentException();
        return info;
    } catch (ClassFormatException e) {
        //e.printStackTrace();
        return null;
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}

From source file:org.apache.tapestrytools.ui.internal.tcc.editor.PackagesPart.java

License:Open Source License

private void collectCustomComponents(IProject project) {
    List<ComponentInstance> componentList = new ArrayList<ComponentInstance>();
    try {/*from w ww  .  j a v  a  2  s. c o  m*/
        IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IPackageFragmentRoot root = roots[i];
            String srcPath = root.getElementName();
            for (ComponentPackage pi : model.getCustomPackageList()) {
                if (pi.isArchive() == root.isArchive() && srcPath.equals(pi.getFragmentRoot())) {
                    if (!root.isArchive()) {
                        //Load custom components from source directory
                        String wholePath = "/" + srcPath + "/" + pi.getPath().replace(".", "/");
                        IFolder folder = project.getFolder(wholePath);
                        if (folder.exists()) {
                            IResource[] fileList = folder.members();
                            for (IResource file : fileList) {
                                String fullPath = file.getFullPath().toString();
                                String classFile = fullPath.replace(".tml", ".java").substring(1);
                                classFile = classFile.substring(classFile.indexOf("/"));
                                if (fullPath.endsWith(".tml") && file.getType() == IResource.FILE
                                        && project.getFile(classFile).exists()) {
                                    IFile componentFile = (IFile) file;
                                    Element rootElement = getRootElementOfXML(componentFile.getContents());
                                    if (rootElement.getNodeName().trim().equals("t:container")) {
                                        String componentName = classFile.substring(classFile.lastIndexOf("/"));
                                        componentName = componentName.substring(0, componentName.indexOf("."));
                                        if (componentName.startsWith("/"))
                                            componentName = componentName.substring(1);
                                        ComponentInstance ci = new ComponentInstance();
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                            }
                        }
                    } else {
                        // Load custom components from jar files
                        for (IJavaElement pack : root.getChildren()) {
                            if (pack != null && pack instanceof PackageFragment
                                    && pack.getElementName().equals(pi.getPath())) {
                                for (Object packo : ((PackageFragment) pack)
                                        .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                                    ClassFile packi = (ClassFile) packo;
                                    String itemName = packi.getElementName();
                                    if (itemName.indexOf('$') < 0 && itemName.endsWith(".class")) {
                                        ComponentInstance ci = new ComponentInstance();
                                        String componentName = itemName.substring(0, itemName.length() - 6);
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        model.setComponentList(componentList);
        markDirty();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.codehaus.groovy.eclipse.core.builder.GroovyNameLookup.java

License:Apache License

/**
 * Copied from parent class//from ww w  .  j  av a2 s. c  om
 * Changes marked with // GROOVY begin and // GROOVY end
 */
@Override
protected void seekTypesInBinaryPackage(String name, IPackageFragment pkg, boolean partialMatch,
        int acceptFlags, IJavaElementRequestor requestor) {
    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        // GROOVY begin
        // ensure ends with .class
        if (!name.endsWith(".class")) {
            name += ".class";
        }
        // GROOVY end
        if (!partialMatch) {
            // exact match
            if (requestor.isCanceled())
                return;
            ClassFile classFile = (ClassFile) pkg.getClassFile(name);
            if (classFile.existsUsingJarTypeCache()) {
                IType type = classFile.getType();
                if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                    requestor.acceptType(type);
                }
            }

            // GROOVY begin
            // class file may still exist as an inner type
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            for (IJavaElement elt : classFiles) {
                classFile = (ClassFile) elt;
                if (classFile.getElementName().endsWith("$" + name)) {
                    IType type = classFile.getType();
                    if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                        requestor.acceptType(type);
                    }
                }
            }
            // GROOVY end

        } else {
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            int length = classFiles.length;
            String unqualifiedName = name;
            int index = name.lastIndexOf('$');
            if (index != -1) {
                //the type name of the inner type
                unqualifiedName = Util.localTypeName(name, index, name.length());
                // unqualifiedName is empty if the name ends with a '$' sign.
                // See http://dev.eclipse.org/bugs/show_bug.cgi?id=14642
            }
            int matchLength = name.length();
            for (int i = 0; i < length; i++) {
                if (requestor.isCanceled())
                    return;
                IJavaElement classFile = classFiles[i];
                // MatchName will never have the extension ".class" and the elementName always will.
                String elementName = classFile.getElementName();
                if (elementName.regionMatches(true /*ignore case*/, 0, name, 0, matchLength)) {
                    IType type = ((ClassFile) classFile).getType();
                    String typeName = type.getElementName();
                    if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type
                        if (nameMatches(unqualifiedName, type, true/*partial match*/)
                                && acceptType(type, acceptFlags, false/*not a source type*/))
                            requestor.acceptType(type);
                    }
                }
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInBinaryPackage += System.currentTimeMillis() - start;
    }
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IBinaryType getBinaryInfo(ClassFile classFile, IResource resource) throws CoreException {
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (classFile.isOpen())
        return (IBinaryType) binaryType.getElementInfo(); // reuse the info from the java model cache

    // create a temporary info
    IBinaryType info;// ww w.ja v  a2 s  .c  o  m
    try {
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
        if (root.isArchive()) {
            // class file in a jar
            String classFileName = classFile.getElementName();
            String classFilePath = Util.concatWith(pkg.names, classFileName, '/');
            ZipFile zipFile = null;
            try {
                zipFile = ((JarPackageFragmentRoot) root).getJar();
                info = ClassFileReader.read(zipFile, classFilePath);
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
            }
        } else {
            // class file in a directory
            info = Util.newClassFileReader(resource);
        }
        if (info == null)
            throw binaryType.newNotPresentException();
        return info;
    } catch (ClassFormatException e) {
        //e.printStackTrace();
        return null;
    } catch (java.io.IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}

From source file:org.eclipse.wst.xml.core.internal.contentmodel.tapestry.travelpackage.TapestryClassLoader.java

License:Open Source License

public List<String> loadParametersFromParentClass(IPackageFragmentRoot root, String classFileName) {
    List<String> list = new ArrayList<String>();
    if (classFileName.indexOf('/') < 0)
        return list;
    String packageName = classFileName.substring(0, classFileName.lastIndexOf('/')).replace('/', '.');
    String className = classFileName.substring(classFileName.lastIndexOf('/') + 1) + ".class";
    try {/*from   w  w w  . j ava2s .  com*/
        PackageFragment packInstance = (PackageFragment) root.getPackageFragment(packageName);
        for (Object packo : packInstance.getChildrenOfType(IJavaElement.CLASS_FILE)) {
            ClassFile packi = (ClassFile) packo;
            if (packi.getElementName().equals(className)) {
                ClassFileReader reader = null;
                try {
                    reader = new ClassFileReader(packi.getBytes(), null);
                } catch (ClassFormatException e) {
                    e.printStackTrace();
                }

                if (reader.getFields() != null)
                    for (IBinaryField field : reader.getFields()) {
                        boolean parameter = false;
                        if (field.getAnnotations() == null)
                            continue;
                        for (IBinaryAnnotation anno : field.getAnnotations()) {
                            if (String.valueOf(anno.getTypeName()).endsWith("/Parameter;")) {
                                parameter = true;
                                break;
                            }
                        }
                        if (parameter) {
                            list.add(String.valueOf(field.getName()));
                        }
                    }
                String parentClassName = String.valueOf(reader.getSuperclassName());
                if (parentClassName != null && !parentClassName.isEmpty()
                        && !parentClassName.equals("java/lang/Object")) {
                    list.addAll(loadParametersFromParentClass(root, parentClassName));
                }
                return list;
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return list;
}

From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java

License:Open Source License

/**
 * Get custom component attribute templates
 * //from   w  w w  .j a  v  a 2 s .c om
 * @param project
 * @param contextTypeId
 * @param nodeName
 * @param tapestryClassLoader
 * @return
 */
public List<Template> getCustomComponentsAttributes(IProject project, String contextTypeId, String nodeName,
        TapestryClassLoader tapestryClassLoader) {
    components.clear();
    String prefix = nodeName.substring(0, nodeName.indexOf(':'));
    try {
        final IFile res = project.getFile(TapestryContants.CUSTOM_COMPONENTS_FILE);
        if (res == null || prefix == null)
            return components;
        List<ComponentPackage> packageList = loadCustomComponentsPackageListWithPrefix(res, prefix);
        IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots();
        for (ComponentPackage cp : packageList) {
            for (IPackageFragmentRoot root : roots) {
                if (cp.isArchive() == root.isArchive() && cp.getFragmentRoot().equals(root.getElementName())) {
                    IPackageFragment packInstance = root.getPackageFragment(cp.getPath());
                    if (!root.isArchive()) {
                        //If current custom component is in source directory
                        if (packInstance != null) {
                            IJavaElement[] elements = packInstance.getChildren();
                            for (IJavaElement ele : elements) {
                                if (ele.getElementType() == IJavaElement.COMPILATION_UNIT
                                        && ele.getElementName().endsWith(".java")) {
                                    String name = ele.getElementName().substring(0,
                                            ele.getElementName().indexOf('.'));
                                    if ((prefix + ":" + name).toLowerCase().equals(nodeName)) {
                                        goThroughClass((ICompilationUnit) ele, contextTypeId);
                                        return components;
                                    }
                                }
                            }
                        }
                    } else if (packInstance instanceof PackageFragment) {
                        //Current custom component is in jar files
                        for (Object packo : ((PackageFragment) packInstance)
                                .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                            ClassFile packi = (ClassFile) packo;
                            String className = packi.getElementName().substring(0,
                                    packi.getElementName().indexOf('.'));
                            if (className.indexOf('$') < 0
                                    && (prefix + ":" + className.toLowerCase()).equals(nodeName)) {
                                TapestryCoreComponents component = null;
                                try {
                                    component = tapestryClassLoader.loadComponentAttributesFromClassFile(root,
                                            prefix, packi);
                                } catch (ClassFormatException e) {
                                    e.printStackTrace();
                                }
                                if (component != null) {
                                    for (String paramName : component.getPamameters()) {
                                        Template template = new Template(paramName,
                                                "add attribute " + paramName, contextTypeId,
                                                buildAttributeInsertCode(paramName), true);
                                        components.add(template);
                                    }
                                    return components;
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java

License:Open Source License

private List<String> getCustomComponentsNameList(IPackageFragmentRoot[] roots, ComponentPackage cp) {
    List<String> componentNameList = new ArrayList<String>();
    try {/*from w  w  w. j a v  a2s.c  o  m*/
        for (IPackageFragmentRoot root : roots) {
            if (root instanceof JarPackageFragmentRoot == cp.isArchive()
                    && root.getElementName().equals(cp.getFragmentRoot())) {
                if (!root.isArchive()) {
                    // Load custom components from source directory
                    IPackageFragment packInstance = root.getPackageFragment(cp.getPath());
                    if (packInstance != null) {
                        IJavaElement[] elements = packInstance.getChildren();
                        for (IJavaElement ele : elements) {
                            if (ele.getElementType() == IJavaElement.COMPILATION_UNIT
                                    && ele.getElementName().endsWith(".java")) {
                                String name = ele.getElementName().substring(0,
                                        ele.getElementName().indexOf('.'));
                                componentNameList.add(name);
                            }
                        }
                    }
                } else {
                    // Load custom components from jar files
                    for (IJavaElement pack : root.getChildren()) {
                        if (pack != null && pack instanceof PackageFragment
                                && pack.getElementName().equals(cp.getPath())) {
                            for (Object packo : ((PackageFragment) pack)
                                    .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                                ClassFile packi = (ClassFile) packo;
                                String itemName = packi.getElementName();
                                if (itemName.indexOf('$') < 0 && itemName.endsWith(".class"))
                                    componentNameList.add(itemName.substring(0, itemName.length() - 6));
                            }
                            break;
                        }
                    }
                }
                return componentNameList;
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return componentNameList;
}

From source file:org.eclipse.wst.xml.ui.internal.contentassist.XMLTemplateCompletionProcessor.java

License:Open Source License

/**
 * Get templates entrance method/*from  www.j  av a2 s .  c  o  m*/
 * 
 * @param node
 * @param offset
 * @param contextTypeId
 * @param preChar
 * @param preChar2
 * @return
 */
protected Template[] getTemplates(IDOMNode node, int offset, String contextTypeId, char preChar,
        char preChar2) {
    IProject project = getCurrentProject();
    String mapKey = project.getName();
    TapestryCoreComponents[] coreList = this.templateCacheMap.get(mapKey);
    if (coreList == null || coreList.length <= 0) {
        //Get tapestry components from classpath
        List<TapestryCoreComponents> list = new ArrayList<TapestryCoreComponents>();
        PackageFragment tapestryCorePackage = getTapestryCoreLibrary();
        try {
            if (tapestryCorePackage != null)
                for (Object packo : tapestryCorePackage.getChildrenOfType(IJavaElement.CLASS_FILE)) {
                    ClassFile packi = (ClassFile) packo;
                    if (packi.getElementName().indexOf('$') < 0) {
                        TapestryCoreComponents component = tapestryClassLoader
                                .loadComponentAttributesFromClassFile(
                                        tapestryClassLoader.getTapestryCoreJar(project), "t", packi);
                        if (component != null)
                            list.add(component);
                    }
                }
        } catch (JavaModelException e) {
            e.printStackTrace();
        } catch (ClassFormatException e) {
            e.printStackTrace();
        }
        if (list != null && list.size() > 0) {
            coreList = list.toArray(new TapestryCoreComponents[0]);
            this.templateCacheMap.put(mapKey, coreList);
        }
    }

    if (coreList == null)
        return new Template[0];

    if (contextTypeId.equals(TapestryElementCollection.componentsContextTypeId)) {
        boolean customComponent = false;
        int type = 1;
        if (preChar == '<')
            type = 2;
        else if (currentTapestryComponent.getPrefix() != null) {
            customComponent = tapestryRootComponentsProposalComputer
                    .getComponentsPrefixList(this.getCurrentProject())
                    .contains(currentTapestryComponent.getPrefix());
            if (customComponent)
                type = 3;
        }
        List<Template> components = new ArrayList<Template>();
        if (currentTapestryComponent.getPrefix() != null && !customComponent)
            return components.toArray(new Template[0]);

        if (type != 3 || currentTapestryComponent.getNodeName().equals("t:")) {
            List<Template> buildInList = CoreComponentsUtil.buildTemplateListFromComponents(coreList,
                    contextTypeId, type);
            if (buildInList != null && buildInList.size() > 0)
                components.addAll(buildInList);
            List<Template> rootComponents = tapestryRootComponentsProposalComputer
                    .getRootComponentsTemplates(this.getCurrentProject(), contextTypeId, type);
            if (rootComponents != null && rootComponents.size() > 0)
                components.addAll(rootComponents);
        }

        if (currentTapestryComponent.getPrefix() == null || (currentTapestryComponent.getPrefix() + ":")
                .equals(currentTapestryComponent.getNodeName())) {
            List<Template> customComponents = tapestryRootComponentsProposalComputer
                    .getCustomComponentsTemplates(this.getCurrentProject(), contextTypeId, type,
                            currentTapestryComponent.getPrefix());
            if (customComponents != null && customComponents.size() > 0)
                components.addAll(customComponents);
        }

        return components == null ? null : components.toArray(new Template[0]);
    } else if (contextTypeId.equals(TapestryElementCollection.attributesContextTypeId)) {
        String tapestryComponentName = getTapestryComponentName(node);
        //In condition <t:ActionLink
        if (tapestryComponentName == null)
            tapestryComponentName = currentTapestryComponent.getNodeName().toLowerCase();
        //In condition <t:html.Message
        if (tapestryComponentName.indexOf('.') > -1 && currentTapestryComponent.getPrefix() != null
                && currentTapestryComponent.getPrefix().equals("t"))
            tapestryComponentName = tapestryComponentName.substring(2).replace('.', ':');

        List<Template> tapestryTemplates = CoreComponentsUtil.getAttributeList(coreList, contextTypeId,
                tapestryComponentName);
        if (tapestryTemplates == null || tapestryTemplates.size() == 0)
            tapestryTemplates = tapestryRootComponentsProposalComputer.getRootComponentsAttributes(project,
                    contextTypeId, tapestryComponentName);
        if (tapestryTemplates == null || tapestryTemplates.size() == 0)
            tapestryTemplates = tapestryRootComponentsProposalComputer.getCustomComponentsAttributes(project,
                    contextTypeId, tapestryComponentName, tapestryClassLoader);

        return tapestryTemplates == null ? null : tapestryTemplates.toArray(new Template[0]);
    } else if (contextTypeId.equals(TapestryElementCollection.attributesValueContextTypeId)) {
        List<Template> tapestryTemplates = null;
        if (isComponentTypeContentAssist(node, offset)) {
            tapestryTemplates = CoreComponentsUtil.getTapestryComponentNameList(coreList, contextTypeId);
            List<Template> rootComponents = tapestryRootComponentsProposalComputer
                    .getRootComponentsNameTemplates(this.getCurrentProject(), contextTypeId);
            if (rootComponents != null && rootComponents.size() > 0)
                tapestryTemplates.addAll(rootComponents);
            List<Template> customComponents = tapestryRootComponentsProposalComputer
                    .getCustomComponentsNameTemplates(this.getCurrentProject(), contextTypeId);
            if (customComponents != null && customComponents.size() > 0)
                tapestryTemplates.addAll(customComponents);
        } else if (isComponentContentassist(node, offset)) {
            tapestryTemplates = TapestryComponentCompletionProposalComputer.getInstance()
                    .computeCompletionProposals("", node, offset);
        } else {
            tapestryTemplates = collection.getAttributeValueList(contextTypeId, currentTapestryComponent);
        }
        return tapestryTemplates == null ? null : tapestryTemplates.toArray(new Template[0]);
    } else {
        Template templates[] = null;
        TemplateStore store = getTemplateStore();
        if (store != null) {
            templates = store.getTemplates(contextTypeId);
        }

        return templates;
    }
}