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.sonar.ide.eclipse.internal.jdt.JavaElementsAdapterFactory.java

License:Open Source License

private ISonarResource getSonarResource(Object adaptableObject) {
    if (adaptableObject instanceof IJavaElement) {
        IJavaElement javaElement = (IJavaElement) adaptableObject;
        return (ISonarResource) getAdapter(javaElement.getResource(), ISonarResource.class);
    } else if (adaptableObject instanceof IProject) {
        IProject project = (IProject) adaptableObject;
        if (!isConfigured(project)) {
            return null;
        }//from w  w  w  .ja v a  2  s . co m
        return SonarUiPlugin.getSonarProject(project);
    } else if (adaptableObject instanceof IFolder) {
        IFolder folder = (IFolder) adaptableObject;
        IProject project = folder.getProject();
        if (!isConfigured(project)) {
            return null;
        }
        String projectKey = getProjectKey(folder.getProject());
        String packageName = getPackageName(JavaCore.create(folder));
        if (packageName != null) {
            return SonarCorePlugin.createSonarResource(folder,
                    SonarKeyUtils.packageKey(projectKey, packageName), packageName);
        }
    } else if (adaptableObject instanceof IFile) {
        IFile file = (IFile) adaptableObject;
        IProject project = file.getProject();
        if (!isConfigured(project)) {
            return null;
        }
        String projectKey = getProjectKey(file.getProject());
        IJavaElement javaElement = JavaCore.create(file);
        if (javaElement instanceof ICompilationUnit) {
            String packageName = getPackageName(javaElement.getParent());
            String className = StringUtils.substringBeforeLast(javaElement.getElementName(), "."); //$NON-NLS-1$
            return SonarCorePlugin.createSonarFile(file,
                    SonarKeyUtils.classKey(projectKey, packageName, className), className);
        }
    }
    return null;
}

From source file:org.sonar.ide.eclipse.internal.jdt.JavaResourceResolver.java

License:Open Source License

@Override
public String resolve(IResource resource, IProgressMonitor monitor) {
    IJavaElement javaElement = JavaCore.create(resource);
    if (javaElement instanceof ICompilationUnit) {
        String packageName = getPackageName(javaElement.getParent());
        String className = StringUtils.substringBeforeLast(javaElement.getElementName(), "."); //$NON-NLS-1$
        return packageName + "." + className; //$NON-NLS-1$
    } else if (javaElement instanceof IPackageFragment) {
        return getPackageName(javaElement);
    }/* w w w . j a v  a  2 s. c o  m*/
    return null;
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaResourceResolver.java

License:Open Source License

@Override
public String getSonarPartialKey(IResource resource) {
    IJavaElement javaElement = JavaCore.create(resource);
    if (javaElement instanceof ICompilationUnit) {
        String packageName = getPackageName(javaElement.getParent());
        String className = StringUtils.substringBeforeLast(javaElement.getElementName(), "."); //$NON-NLS-1$
        return packageName + "." + className; //$NON-NLS-1$
    } else if (javaElement instanceof IPackageFragmentRoot) {
        return "[default]"; //$NON-NLS-1$
    } else if (javaElement instanceof IPackageFragment) {
        return getPackageName(javaElement);
    }/*ww  w .j av a 2  s. c  o  m*/
    return null;
}

From source file:org.springframework.ide.eclipse.aop.core.util.AopReferenceModelUtils.java

License:Open Source License

public static String getJavaElementLinkName(IJavaElement je) {
    if (je == null) {
        return "";
    }/*from   w w w.  j  a v  a2 s .  c o  m*/
    // use element name instead, qualified with parent
    if (je instanceof IMethod) {
        // return je.getParent().getElementName() + '.' +
        return readableName((IMethod) je);
    } else if (je instanceof IType) {
        return je.getElementName();
    } else if (je instanceof IField) {
        return je.getElementName() + " - " + ((IType) je.getParent()).getFullyQualifiedName();
    } else if (je.getParent() != null) {
        return je.getParent().getElementName() + '.' + je.getElementName();
    }
    return je.getElementName();
}

From source file:org.springframework.ide.eclipse.aop.core.util.AopReferenceModelUtils.java

License:Open Source License

public static String getPackageLinkName(IJavaElement je) {
    if (je instanceof IMethod) {
        return ((IMethod) je).getDeclaringType().getPackageFragment().getElementName();
    } else if (je instanceof IType) {
        return ((IType) je).getPackageFragment().getElementName();
    }//from   www.jav a 2s.  c  o m
    return je.getElementName();
}

From source file:org.springframework.ide.eclipse.beans.core.internal.model.BeansConfigFactory.java

License:Open Source License

/**
 * @since 3.3.0//w w  w  .  j a  va  2  s. com
 */
public static String getConfigName(IFile file, IProject project) {
    String configName;

    if (!"xml".equals(file.getFileExtension())) {
        IJavaProject javaProject = JdtUtils.getJavaProject(project.getProject());
        if (javaProject != null) {
            IJavaElement element = JavaCore.create(file, javaProject);
            if (element != null && element.getPrimaryElement() instanceof ICompilationUnit) {
                String typeName = element.getElementName();
                String fileExtension = file.getFileExtension();
                if (fileExtension != null && fileExtension.length() > 0) {
                    typeName = typeName.substring(0, typeName.length() - (fileExtension.length() + 1));
                }

                IJavaElement parent = element.getParent();
                String packageName = "";
                if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                    IPackageFragment packageFragment = (IPackageFragment) parent;
                    if (!packageFragment.isDefaultPackage()) {
                        packageName = packageFragment.getElementName() + ".";
                    }

                    return JAVA_CONFIG_TYPE + packageName + typeName;
                }
            }
        }
    }

    if (file.getProject().equals(project.getProject()) && !(file instanceof ExternalFile)) {
        configName = file.getProjectRelativePath().toString();
    } else {
        configName = file.getFullPath().toString();
    }
    return configName;
}

From source file:org.springframework.ide.eclipse.beans.ui.autowire.internal.model.AutowireGraphContentExtender.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  w w .j  ava  2  s  .c om*/
 */
public void addAdditionalBeans(Map<String, Bean> beans, List<Reference> beansReferences,
        IBeansModelElement root, IBeansModelElement context) {
    AutowireDependencyProvider provider = new AutowireDependencyProvider(root, context);
    Map<IBean, Set<IBeanReference>> autowiredReferences = provider.resolveAutowiredDependencies();

    for (Map.Entry<IBean, Set<IBeanReference>> entry : autowiredReferences.entrySet()) {
        Bean bean = beans.get(entry.getKey().getElementName());
        if (bean != null) {
            Set<String> autowiredProperties = new HashSet<String>();
            Set<IBeanReference> refs = entry.getValue();
            for (IBeanReference ref : refs) {
                Bean targetBean = beans.get(ref.getBeanName());
                if (targetBean != null) {
                    beansReferences.add(new Reference(BeanType.STANDARD, bean, targetBean, null,
                            !bean.isRootBean(), (IResourceModelElement) ref));

                    try {
                        IJavaElement source = ((AutowireBeanReference) ref).getSource();
                        if (source instanceof IField) {
                            String propertyName = source.getElementName();
                            if (!autowiredProperties.contains(propertyName)) {
                                IBeanProperty newProperty = new BeanProperty(entry.getKey(), new PropertyValue(
                                        propertyName, new RuntimeBeanReference(ref.getBeanName())));
                                bean.addBeanProperty(newProperty);
                                autowiredProperties.add(propertyName);
                            }

                        } else if (source instanceof IMethod && ((IMethod) source).isConstructor()) {
                            IBeanConstructorArgument newConstructorArg = new BeanConstructorArgument(
                                    entry.getKey(), ((AutowireBeanReference) ref).getParameterIndex(),
                                    new ValueHolder(new RuntimeBeanReference(ref.getBeanName())));
                            bean.addBeanConstructorArgument(newConstructorArg);
                        } else if (source instanceof IMethod && !((IMethod) source).isConstructor()) {
                            String propertyName = source.getElementName();
                            if (propertyName.startsWith("set")) {
                                propertyName = Introspector.decapitalize(propertyName.substring(3));
                            }
                            if (!autowiredProperties.contains(propertyName)) {
                                IBeanProperty newProperty = new BeanProperty(entry.getKey(), new PropertyValue(
                                        propertyName, new RuntimeBeanReference(ref.getBeanName())));
                                bean.addBeanProperty(newProperty);
                                autowiredProperties.add(propertyName);
                            }
                        }
                    } catch (JavaModelException e) {
                        // Just ignore this
                    }
                }
            }
        }
    }
}

From source file:org.springframework.ide.eclipse.beans.ui.refactoring.util.BeansRefactoringChangeUtils.java

License:Open Source License

private static Set<TextEdit> createConstructorTextEdits(Node node, IJavaElement element, String newName,
        IFile file) {//from  w  ww  .ja  v  a  2s . c  o  m
    if (node == null) {
        return null;
    }

    Set<TextEdit> result = new HashSet<TextEdit>();

    if (element instanceof ILocalVariable) {
        // c-namespace references to the argument name that belong to the changed constructor argument
        if (node.hasAttributes()) {
            String argumentName = element.getElementName();

            String attributeNameStart = "c:";
            String optionalAttributeNameEnd = "-ref";

            NamedNodeMap attributes = node.getAttributes();
            int attributeCount = attributes.getLength();

            for (int i = 0; i < attributeCount; i++) {
                AttrImpl attribute = (AttrImpl) attributes.item(i);
                String attributeName = attribute.getNodeName();
                if (attributeName != null && attributeName.startsWith(attributeNameStart)) {
                    if (attributeName.equals(attributeNameStart + argumentName) || attributeName
                            .equals(attributeNameStart + argumentName + optionalAttributeNameEnd)) {
                        List<IType> types = BeansEditorUtils.getClassNamesOfBean(file, node);
                        if (element.getParent() != null && element.getParent() instanceof IMethod
                                && types.contains(((IMethod) element.getParent()).getDeclaringType())) {
                            int offset = attribute.getNameRegionStartOffset() + attributeNameStart.length();
                            result.add(new ReplaceEdit(offset, argumentName.length(), newName));
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.springframework.ide.eclipse.beans.ui.refactoring.util.BeansRefactoringChangeUtils.java

License:Open Source License

private static Set<TextEdit> createMethodTextEdits(Node node, IJavaElement element, String newName,
        IFile file) {/*w  ww  .  j  a  v  a2s .  c o  m*/
    if (node == null) {
        return null;
    }

    Set<TextEdit> result = new HashSet<TextEdit>();

    if (element instanceof IMethod) {
        if (element.getElementName().startsWith("set")) {
            String methodName = StringUtils.uncapitalize(element.getElementName().substring(3));

            // change those child notes that are property childs and refer to the changed method name
            NodeList nodes = node.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node child = nodes.item(i);
                if ("property".equals(child.getLocalName()) && BeansEditorUtils.hasAttribute(child, "name")) {
                    String propertyName = BeansEditorUtils.getAttribute(child, "name");
                    if (methodName.equals(propertyName)) {
                        List<IType> types = BeansEditorUtils.getClassNamesOfBean(file, node);
                        if (types.contains(((IMethod) element).getDeclaringType())) {
                            AttrImpl attr = (AttrImpl) child.getAttributes().getNamedItem("name");
                            int offset = attr.getValueRegionStartOffset() + 1;
                            if (offset >= 0) {
                                result.add(new ReplaceEdit(offset, propertyName.length(), newName));
                            }
                        }
                    }
                }
            }

            // p-namespace references to the properties that belong to the changed method
            if (node.hasAttributes()) {
                String attributeNameStart = "p:";
                String optionalAttributeNameEnd = "-ref";

                NamedNodeMap attributes = node.getAttributes();
                int attributeCount = attributes.getLength();

                for (int i = 0; i < attributeCount; i++) {
                    AttrImpl attribute = (AttrImpl) attributes.item(i);
                    String attributeName = attribute.getNodeName();
                    if (attributeName != null && attributeName.startsWith(attributeNameStart)) {
                        if (attributeName.equals(attributeNameStart + methodName) || attributeName
                                .equals(attributeNameStart + methodName + optionalAttributeNameEnd)) {
                            List<IType> types = BeansEditorUtils.getClassNamesOfBean(file, node);
                            if (types.contains(((IMethod) element).getDeclaringType())) {
                                int offset = attribute.getNameRegionStartOffset() + attributeNameStart.length();
                                result.add(new ReplaceEdit(offset, methodName.length(), newName));
                            }
                        }
                    }
                }
            }
        } else {
            TextEdit edit = null;
            edit = createMethodTextEditForAttribute(node, element, newName, file, "init-method");
            if (edit != null) {
                result.add(edit);
            }

            edit = createMethodTextEditForAttribute(node, element, newName, file, "destroy-method");
            if (edit != null) {
                result.add(edit);
            }

            edit = createMethodTextEditForAttribute(node, element, newName, file, "factory-method");
            if (edit != null) {
                result.add(edit);
            }
        }
    }
    return result;
}

From source file:org.springframework.ide.eclipse.beans.ui.refactoring.util.BeansRefactoringChangeUtils.java

License:Open Source License

private static TextEdit createMethodTextEditForAttribute(Node node, IJavaElement element, String newName,
        IFile file, String attrName) {
    String methodName = element.getElementName();
    if (BeansEditorUtils.hasAttribute(node, attrName)) {
        String attrMethodName = BeansEditorUtils.getAttribute(node, attrName);
        if (methodName.equals(attrMethodName)) {
            List<IType> types = BeansEditorUtils.getClassNamesOfBean(file, node);
            if (types.contains(((IMethod) element).getDeclaringType())) {
                AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(attrName);
                int offset = attr.getValueRegionStartOffset() + 1;
                if (offset >= 0) {
                    return new ReplaceEdit(offset, attrMethodName.length(), newName);
                }/*from w ww  .  j  a  v a2 s.  com*/
            }
        }
    }
    return null;
}