Example usage for org.eclipse.jdt.internal.core PackageFragment getChildrenOfType

List of usage examples for org.eclipse.jdt.internal.core PackageFragment getChildrenOfType

Introduction

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

Prototype

public ArrayList getChildrenOfType(int type) throws JavaModelException 

Source Link

Document

Returns a collection of (immediate) children of this node of the specified type.

Usage

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 a  v a  2  s.c  o  m
        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.XMLTemplateCompletionProcessor.java

License:Open Source License

/**
 * Get templates entrance method//from  w  w w  .j a va  2  s.c  om
 * 
 * @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;
    }
}