Example usage for org.eclipse.jdt.core IJavaElement getParent

List of usage examples for org.eclipse.jdt.core IJavaElement getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getParent.

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

Usage

From source file:org.seasar.kijimuna.ui.editor.contentassist.JavaPackageProposalHolder.java

License:Apache License

private boolean isArchive() {
    IJavaElement parent = element;
    while (parent != null) {
        if (parent instanceof IPackageFragmentRoot) {
            return ((IPackageFragmentRoot) parent).isArchive();
        }// w  w w. j a v  a  2 s.c o m
        parent = parent.getParent();
    }
    return true;
}

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);//ww  w  .  j a  v a  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);
    }
}

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;
        }/*  w  w w  .j a  v  a  2  s.c  o  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);
    }//from  w w  w .  java2s  .  co  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);
    }//from ww  w .j  a  v  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 "";
    }//  w  w  w  . j  a  va 2  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.beans.core.internal.model.BeansConfigFactory.java

License:Open Source License

/**
 * @since 3.3.0/*from w ww  .  j  a va 2s  .  c o  m*/
 */
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.refactoring.util.BeansRefactoringChangeUtils.java

License:Open Source License

private static Set<TextEdit> createConstructorTextEdits(Node node, IJavaElement element, String newName,
        IFile file) {/*  w  w w  .j ava  2 s.co  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.core.java.annotation.JdtBasedAnnotationMetadata.java

License:Open Source License

private Annotation processAnnotation(IAnnotation annotation, IType itype) {
    Annotation modelAnnotation;//from   ww  w .  j av  a2 s. com
    if (itype.isBinary()) {
        modelAnnotation = new Annotation(annotation.getElementName());
    } else {
        IType type = itype;
        IJavaElement parent = annotation.getParent();
        while (parent != null && !(parent instanceof IType)) {
            parent = parent.getParent();
        }
        if (parent != null) {
            type = (IType) parent;
        }
        modelAnnotation = new Annotation(JdtUtils.resolveClassName(annotation.getElementName(), type));
    }
    try {
        for (IMemberValuePair member : annotation.getMemberValuePairs()) {
            StringBuilder builder = new StringBuilder();
            // First check if we have an array
            if (member.getValue() != null && member.getValue().getClass().isArray()) {
                Object[] values = (Object[]) member.getValue();
                for (int i = 0; i < values.length; i++) {
                    processStringValue(member, builder, values[i].toString());
                    if (i < values.length - 1) {
                        builder.append(", ");
                    }
                }
            }
            // If not process the value directly
            else if (member.getValue() != null) {
                processStringValue(member, builder, member.getValue().toString());
            }

            modelAnnotation.addMember(new AnnotationMemberValuePair(
                    ("value".equals(member.getMemberName()) ? null : member.getMemberName()),
                    builder.toString()));

        }
    } catch (JavaModelException e) {
        // 
    }
    return modelAnnotation;
}

From source file:org.springframework.ide.eclipse.quickfix.hyperlinks.AutowireHyperlinkDetector.java

License:Open Source License

private IType getParentType(IJavaElement element) {
    if (element == null) {
        return null;
    }//from w w  w  .  jav  a  2s  .co  m

    if (element instanceof IType) {
        return (IType) element;
    }

    return getParentType(element.getParent());
}