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.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    if (targetEditor != null) {
        try {// w ww .j a  va2 s. c o  m
            boolean isAuto = SourceLookupActivator.getDefault().isAutoAddSourceContainer();
            if (!isAuto) {
                return;
            }
            IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
                    IPath attachmentPath = fragment.getSourceAttachmentPath();
                    if (attachmentPath != null && !attachmentPath.isEmpty()
                            && attachmentPath.toFile().exists()) {
                        break;
                    }
                    if (fragment.isArchive()) {
                        IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                        File file = iFile == null || iFile.getLocation() == null ? fragment.getPath().toFile()
                                : iFile.getLocation().toFile();
                        ZipFile jar = new ZipFile(file);
                        final ArtifactKey artifact = JBossSourceContainer.getArtifact(file, jar);
                        if (artifact != null) {
                            IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                            if (sourcePath == null || !sourcePath.toFile().exists()) {
                                Job job = JBossSourceContainer.downloadArtifact(file, artifact);
                                job.addJobChangeListener(new IJobChangeListener() {

                                    @Override
                                    public void sleeping(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void scheduled(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void running(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void done(IJobChangeEvent event) {
                                        IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                                        if (sourcePath != null && sourcePath.toFile().exists()) {
                                            attachSource(fragment, sourcePath);
                                        }
                                    }

                                    @Override
                                    public void awake(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void aboutToRun(IJobChangeEvent event) {
                                    }
                                });
                                job.schedule();
                            } else {
                                attachSource(fragment, sourcePath);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            SourceLookupUIActivator.log(e);
        }
    }
}

From source file:org.jboss.tools.cdi.ui.marker.AddAnnotationMarkerResolution.java

License:Open Source License

public AddAnnotationMarkerResolution(IJavaElement element, String qualifiedName) {
    super(CDIMarkerResolutionUtils.getJavaMember(element).getCompilationUnit());
    this.element = element;
    this.qualifiedName = qualifiedName;
    String shortName = CDIMarkerResolutionUtils.getShortName(qualifiedName);
    String type = "";
    if (element instanceof IType) {
        try {//from  w ww. jav  a 2s. c o  m
            if (((IType) element).isAnnotation())
                type = CDIUIMessages.CDI_QUICK_FIXES_ANNOTATION;
            else if (((IType) element).isInterface())
                type = CDIUIMessages.CDI_QUICK_FIXES_INTERFACE;
            else if (((IType) element).isClass())
                type = CDIUIMessages.CDI_QUICK_FIXES_CLASS;
            else
                type = CDIUIMessages.CDI_QUICK_FIXES_TYPE;
        } catch (JavaModelException ex) {
            CDIUIPlugin.getDefault().logError(ex);
        }
    } else if (element instanceof IMethod) {
        type = CDIUIMessages.CDI_QUICK_FIXES_METHOD;
    } else if (element instanceof IField) {
        type = CDIUIMessages.CDI_QUICK_FIXES_FIELD;
    } else if (element instanceof ILocalVariable && ((ILocalVariable) element).isParameter()) {
        type = NLS.bind(CDIUIMessages.CDI_QUICK_FIXES_PARAMETER, element.getParent().getElementName());
    }

    label = NLS.bind(CDIUIMessages.ADD_ANNOTATION_MARKER_RESOLUTION_TITLE,
            new String[] { shortName, element.getElementName(), type });
    init();
}

From source file:org.jboss.tools.cdi.ui.marker.CDIProblemMarkerResolutionGenerator.java

License:Open Source License

private IJavaElement findJavaElementByAnnotation(IJavaElement element, String qualifiedName)
        throws JavaModelException {
    IAnnotation annotation = getAnnotation(element, qualifiedName);
    if (annotation != null)
        return element;

    if (element instanceof IMethod) {
        for (ILocalVariable parameter : ((IMethod) element).getParameters()) {
            annotation = getAnnotation(parameter, qualifiedName);
            if (annotation != null)
                return parameter;
        }/* ww  w  . ja va2s. c  om*/
    } else if (element instanceof ILocalVariable) {
        IJavaElement parent = element.getParent();
        if (parent != null) {
            annotation = getAnnotation(parent, qualifiedName);
            if (annotation != null)
                return parent;
        }
    } else if (element instanceof IType) {
        for (IField field : ((IType) element).getFields()) {
            annotation = getAnnotation(field, qualifiedName);
            if (annotation != null)
                return field;
        }

        for (IMethod method : ((IType) element).getMethods()) {
            annotation = getAnnotation(method, qualifiedName);
            if (annotation != null)
                return method;
            for (ILocalVariable parameter : method.getParameters()) {
                annotation = getAnnotation(parameter, qualifiedName);
                if (annotation != null)
                    return parameter;
            }
        }
    }

    if (element instanceof IMember) {
        annotation = getAnnotation(((IMember) element).getDeclaringType(), qualifiedName);
        if (annotation != null)
            return ((IMember) element).getDeclaringType();
    }

    return null;
}

From source file:org.jboss.tools.cdi.ui.marker.DeleteAnnotationMarkerResolution.java

License:Open Source License

public DeleteAnnotationMarkerResolution(IJavaElement element, String qualifiedName) {
    super(CDIMarkerResolutionUtils.getJavaMember(element).getCompilationUnit());
    this.element = element;
    this.qualifiedName = qualifiedName;
    String shortName = CDIMarkerResolutionUtils.getShortName(qualifiedName);
    String type = "";
    if (element instanceof IType) {
        try {/*from   w  ww  . j a va 2s  .  c o  m*/
            if (((IType) element).isAnnotation())
                type = CDIUIMessages.CDI_QUICK_FIXES_ANNOTATION;
            else if (((IType) element).isInterface())
                type = CDIUIMessages.CDI_QUICK_FIXES_INTERFACE;
            else if (((IType) element).isClass())
                type = CDIUIMessages.CDI_QUICK_FIXES_CLASS;
            else
                type = CDIUIMessages.CDI_QUICK_FIXES_TYPE;
        } catch (JavaModelException ex) {
            CDIUIPlugin.getDefault().logError(ex);
        }
    } else if (element instanceof IMethod) {
        type = CDIUIMessages.CDI_QUICK_FIXES_METHOD;
    } else if (element instanceof IField) {
        type = CDIUIMessages.CDI_QUICK_FIXES_FIELD;
    } else if (element instanceof ILocalVariable && ((ILocalVariable) element).isParameter()) {
        type = NLS.bind(CDIUIMessages.CDI_QUICK_FIXES_PARAMETER, element.getParent().getElementName());
    }

    label = NLS.bind(CDIUIMessages.DELETE_ANNOTATION_MARKER_RESOLUTION_TITLE,
            new String[] { shortName, element.getElementName(), type });
    init();
}

From source file:org.jboss.tools.common.java.impl.JavaAnnotation.java

License:Open Source License

public IMember getParentMember() {
    IJavaElement ancestor = annotation.getParent();
    while (ancestor != null) {
        if (ancestor instanceof IMember) {
            return (IMember) ancestor;
        }/*from  www .  ja va  2s.  com*/
        ancestor = ancestor.getParent();
    }
    return null;
}

From source file:org.jboss.tools.common.quickfix.AbstractQuickFixGenerator.java

License:Open Source License

protected IJavaElement findJavaElementByAnnotation(IJavaElement element, String qualifiedName)
        throws JavaModelException {
    IAnnotation annotation = getAnnotation(element, qualifiedName);
    if (annotation != null)
        return element;

    if (element instanceof IMethod) {
        for (ILocalVariable parameter : ((IMethod) element).getParameters()) {
            annotation = getAnnotation(parameter, qualifiedName);
            if (annotation != null)
                return parameter;
        }// ww w . ja  v a 2  s .c o m
    } else if (element instanceof ILocalVariable) {
        IJavaElement parent = element.getParent();
        if (parent != null) {
            annotation = getAnnotation(parent, qualifiedName);
            if (annotation != null)
                return parent;
        }
    } else if (element instanceof IType) {
        for (IField field : ((IType) element).getFields()) {
            annotation = getAnnotation(field, qualifiedName);
            if (annotation != null)
                return field;
        }

        for (IMethod method : ((IType) element).getMethods()) {
            annotation = getAnnotation(method, qualifiedName);
            if (annotation != null)
                return method;
            for (ILocalVariable parameter : method.getParameters()) {
                annotation = getAnnotation(parameter, qualifiedName);
                if (annotation != null)
                    return parameter;
            }
        }
    }

    if (element instanceof IMember) {
        annotation = getAnnotation(((IMember) element).getDeclaringType(), qualifiedName);
        if (annotation != null)
            return ((IMember) element).getDeclaringType();
    }

    return null;
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

public static IMember getJavaMember(IJavaElement element) {
    while (element != null) {
        if (element instanceof IMember)
            return (IMember) element;
        element = element.getParent();
    }//from  w  w  w. j av  a  2  s  .c o m
    return null;
}

From source file:org.jboss.tools.common.ui.marker.AddSuppressWarningsMarkerResolution.java

License:Open Source License

private IAnnotatable getAnnatatableElement(IJavaElement element) {
    IJavaElement elem = element;
    while (elem != null) {
        if (element instanceof IAnnotatable) {
            return (IAnnotatable) element;
        }// w w  w  .  j  a  va  2  s.  c  o  m
        elem = elem.getParent();
    }
    return null;
}

From source file:org.jboss.tools.common.util.EclipseJavaUtil.java

License:Open Source License

private static Set<IAnnotation> findAnnotationsByShortName(Set<IAnnotation> result, IJavaElement element,
        String name, boolean checkParents) throws JavaModelException {
    if (element instanceof IAnnotatable) {
        IAnnotation[] annotations = ((IAnnotatable) element).getAnnotations();
        for (IAnnotation annotation : annotations) {
            String aName = annotation.getElementName();
            int i = aName.lastIndexOf('.');
            if (i > -1) {
                aName = aName.substring(i + 1);
            }//from w  w w  . ja v  a  2s.c  o  m
            if (aName.equals(name)) {
                if (result == null) {
                    result = new HashSet<IAnnotation>();
                }
                result.add(annotation);
                break;
            }
        }
    }
    if (checkParents) {
        IJavaElement parent = element.getParent();
        if (parent instanceof IAnnotatable) {
            return findAnnotationsByShortName(result, parent, name, true);
        }
    }
    return result;
}

From source file:org.jboss.tools.common.validation.java.JavaDirtyRegionProcessor.java

License:Open Source License

private boolean isJavaElementValidationRequired() {
    ICompilationUnit unit = fReporter.getCompilationUnit();
    if (unit == null)
        return false;

    boolean result = false;
    boolean atLeastOneElementIsProcessed = false;

    int position = fStartRegionToProcess;
    try {/*from   ww w. j av a2s . c  o  m*/
        unit = unit.getWorkingCopy(null);
        IJavaElement element = null;
        while (position >= 0 && (element = unit.getElementAt(position--)) == null)
            ;

        if (position < 0)
            position = 0;

        ITypedRegion[] partitions = computePartitioning(position, fEndPartitionsToProcess - position);

        ITypedRegion startPartition = findPartitionByOffset(partitions, position);
        ITypedRegion endPartition = (startPartition != null && fEndRegionToProcess >= startPartition.getOffset()
                && fEndRegionToProcess < startPartition.getOffset() + startPartition.getLength())
                        ? startPartition
                        : findPartitionByOffset(partitions, fEndRegionToProcess);

        if (startPartition != null && startPartition.equals(endPartition)
                && !isProcessingRequiredForPartition(startPartition)) {
            return false;
        }

        while (position <= fEndRegionToProcess) {
            ITypedRegion partition = findPartitionByOffset(partitions, position);
            if (!isProcessingRequiredForPartition(partition)) {
                position = partition.getOffset() + partition.getLength();
                continue;
            }

            element = unit.getElementAt(position);
            if (element == null) {
                position++;
                continue;
            }

            atLeastOneElementIsProcessed = true;
            boolean doSkipThisElement = false;
            if (element instanceof IMember && element.getElementType() == IJavaElement.METHOD) {
                ISourceRange range = ((IMember) element).getSourceRange();
                if (position >= range.getOffset()) {
                    try {
                        String text = fDocument.get(range.getOffset(), position - range.getOffset() + 1);
                        if (text.indexOf('{') != -1 && !text.endsWith("}")) { //$NON-NLS-1$
                            doSkipThisElement = true;
                            position = range.getOffset() + range.getLength();
                        }
                    } catch (BadLocationException e) {
                        // Ignore it and do not skip validation
                    }
                    position++;
                }
            } else {
                IJavaElement parent = element.getParent();
                while (parent != null && parent.getElementType() != IJavaElement.COMPILATION_UNIT) {
                    if (parent.getElementType() == IJavaElement.METHOD) {
                        doSkipThisElement = true;
                        break;
                    }
                    parent = parent.getParent();
                }
                position++;
            }

            if (!doSkipThisElement)
                return true;
        }
    } catch (JavaModelException e) {
        LogHelper.logError(CommonValidationPlugin.getDefault(), e);
    } finally {
        try {
            unit.discardWorkingCopy();
        } catch (JavaModelException e) {
            LogHelper.logError(CommonValidationPlugin.getDefault(), e);
        }
    }

    return atLeastOneElementIsProcessed ? result : true;
}