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:com.tsc9526.monalisa.plugin.eclipse.generator.SourceUnit.java

License:Open Source License

public IPackageFragmentRoot getPackageFragmentRoot() {
    IJavaElement e = unit.getJavaElement();
    while (e != null) {
        if (e instanceof IPackageFragmentRoot) {
            return (IPackageFragmentRoot) e;
        } else {//from  www  .  ja v a  2 s.  co  m
            e = e.getParent();
        }
    }
    throw new RuntimeException("Pakcage fragment root not found: " + unit.getJavaElement().getElementName());
}

From source file:de.akra.idocit.java.services.ReflectionHelper.java

License:Apache License

/**
 * Check if the method is declared in a Java interface.
 * //  ww  w .  ja v a  2  s.  c o m
 * @param methodBinding
 *            [OBJECT]
 * @return [REPORT] {@code true} if the method is in a Java interface.
 */
private static boolean isInInterface(final IMethodBinding methodBinding) {
    boolean isInterface = false;
    final IJavaElement method = methodBinding.getJavaElement();
    if (method != null) {
        final IJavaElement parent = method.getParent();

        if (parent.getElementType() == IJavaElement.TYPE) {
            final IType type = (IType) parent;
            try {
                isInterface = type.isInterface() && !type.isAnnotation();
            } catch (JavaModelException e) {
                logger.log(Level.SEVERE, "Failed to check if the method is in an interface.", e);
            }
        }
    } else {
        logger.log(Level.INFO, "Method \"" + methodBinding.getName() + "\" has no parent element.");
    }
    return isInterface;
}

From source file:de.akra.idocit.java.ui.JavaEditorSelectionListener.java

License:Apache License

/**
 * Collect assigned ThematicRoles from the method and it's parents (classes,
 * interfaces, enumerations) and find the reference ThematicGrid.
 * /*w w  w. j av a 2s  . co  m*/
 * @param method
 *            [SOURCE]
 * @throws Exception
 */
private RecommendedGridsViewSelection prepareViewSelection(final IMethod method) throws Exception {
    final AbsJavadocParser javadocParser = JavaParser.getJavadocParser();
    final List<Addressee> addressees = ServiceManager.getInstance().getPersistenceService()
            .loadConfiguredAddressees();
    final List<ThematicRole> roles = ServiceManager.getInstance().getPersistenceService().loadThematicRoles();

    final RecommendedGridsViewSelection selection = new RecommendedGridsViewSelection();
    selection.setOperationIdentifier(method.getElementName());

    final Set<ThematicRole> assignedThematicRoles = new TreeSet<ThematicRole>();
    selection.setAssignedThematicRoles(assignedThematicRoles);

    /*
     * Collect documentations from method
     */
    final String methodSource = extractCodeSnippetOf(method);
    final ASTNode node = parseCodeSnippet(methodSource.toCharArray(), ASTParser.K_CLASS_BODY_DECLARATIONS);

    if ((node.getNodeType() == ASTNode.TYPE_DECLARATION || node.getNodeType() == ASTNode.ENUM_DECLARATION)) {
        final TypeDeclaration typeDeclaration = (TypeDeclaration) node;
        @SuppressWarnings("unchecked")
        final List<BodyDeclaration> bodyDeclarations = (List<BodyDeclaration>) typeDeclaration
                .bodyDeclarations();

        // only one method is parsed
        if (bodyDeclarations.size() == 1) {
            final BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
            if (bodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION
                    && bodyDeclaration.getJavadoc() != null) {
                selection.setReferenceThematicGridName(
                        javadocParser.parseIDocItReferenceGrid(bodyDeclaration.getJavadoc()));

                final List<Documentation> parsedDocs = javadocParser
                        .parseIDocItJavadoc(bodyDeclaration.getJavadoc(), addressees, roles, null);
                for (final Documentation doc : parsedDocs) {
                    if (doc.getThematicRole() != null) {
                        assignedThematicRoles.add(doc.getThematicRole());
                    }
                }
            }

            /*
             * Collect documentations from parent interfaces, classes and enumerations
             */
            IJavaElement parent = method.getParent();
            while (parent != null && parent.getElementType() == IJavaElement.TYPE) {
                final IType type = (IType) parent;
                final String typeSource = extractCodeSnippetOf(type);

                final ASTNode parentNode = parseCodeSnippet(typeSource.toCharArray(),
                        ASTParser.K_CLASS_BODY_DECLARATIONS);

                if ((parentNode.getNodeType() == ASTNode.TYPE_DECLARATION
                        || parentNode.getNodeType() == ASTNode.ENUM_DECLARATION)) {
                    final TypeDeclaration typeDecl = (TypeDeclaration) parentNode;

                    @SuppressWarnings("unchecked")
                    final List<BodyDeclaration> bodyDecls = (List<BodyDeclaration>) typeDecl.bodyDeclarations();

                    // only one class, interface or enum is parsed
                    if (bodyDecls.size() == 1) {
                        final BodyDeclaration bodyDecl = bodyDecls.get(0);

                        // process if it is a class, interface or enum and has Javadoc
                        if ((bodyDecl.getNodeType() == ASTNode.TYPE_DECLARATION
                                || bodyDecl.getNodeType() == ASTNode.ENUM_DECLARATION)
                                && bodyDecl.getJavadoc() != null) {
                            final List<Documentation> parsedDocs = javadocParser
                                    .parseIDocItJavadoc(bodyDecl.getJavadoc(), addressees, roles, null);
                            for (final Documentation doc : parsedDocs) {
                                if (doc.getThematicRole() != null) {
                                    assignedThematicRoles.add(doc.getThematicRole());
                                }
                            }
                        }
                    }

                    // parse next parent
                    parent = parent.getParent();
                } else {
                    LOG.fine("Stop parsing because of incorrect source code syntax.");
                    parent = null;
                }
            }
        }
    } else {
        LOG.info("Parsing of code snippet failed. No assigned ThematicRoles can be collected.");
    }
    return selection;
}

From source file:de.gebit.integrity.ui.contentassist.DSLProposalProvider.java

License:Open Source License

private IType resolveJDTTypeForJvmType(JvmType aType) throws JavaModelException {
    IJavaElement tempSourceMethod = (IJavaElement) elementFinder.findElementFor(aType);

    if (tempSourceMethod.getParent() instanceof CompilationUnit) {
        CompilationUnit tempCompilationUnit = (CompilationUnit) tempSourceMethod.getParent();
        return tempCompilationUnit.getTypes()[0];
    } else if (tempSourceMethod.getParent() instanceof ClassFile) {
        ClassFile tempClassFile = (ClassFile) tempSourceMethod.getParent();
        tempClassFile.open(null);/*from  w  ww . j  av a 2s  .co  m*/
        return tempClassFile.getType();
    }

    return null;
}

From source file:de.gebit.integrity.ui.utils.JavadocUtil.java

License:Open Source License

private static MethodDeclaration getMethodDeclaration(JvmOperation aMethod,
        IJavaElementFinder anElementFinder) {
    IJavaElement tempSourceMethod = (IJavaElement) anElementFinder.findElementFor(aMethod);

    // That parent may also be an instance of org.eclipse.jdt.internal.core.ClassFile - in that case there's
    // no Javadoc anyway
    if (tempSourceMethod.getParent().getParent() instanceof ICompilationUnit) {
        ICompilationUnit tempCompilationUnit = (ICompilationUnit) tempSourceMethod.getParent().getParent();

        AbstractTypeDeclaration tempType = parseCompilationUnit(tempCompilationUnit);

        if (tempType instanceof TypeDeclaration) {
            for (MethodDeclaration tempMethod : ((TypeDeclaration) tempType).getMethods()) {
                // We only check the plain method name and omit the full
                // signature check here because fixture method names are
                // required to be unique per class
                if (aMethod.getSimpleName().equals(tempMethod.getName().getFullyQualifiedName())) {
                    return tempMethod;
                }//from w  w w  . j av  a2 s  .  co  m
            }
        }
    }

    return null;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement//from  w  w w. j  a  v a  2  s  .co  m
 * @return null, if javaElement is top level class
 */
private static IType getFirstAncestor(IJavaElement javaElement) {
    IJavaElement parent = javaElement;
    if (javaElement.getElementType() == IJavaElement.TYPE) {
        parent = javaElement.getParent();
    }
    if (parent != null) {
        return (IType) parent.getAncestor(IJavaElement.TYPE);
    }
    return null;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

private static IJavaElement getLastAncestor(IJavaElement javaElement, int elementType) {
    IJavaElement lastFound = null;/*w  w  w  .  j  ava2 s  . co  m*/
    if (elementType == javaElement.getElementType()) {
        lastFound = javaElement;
    }
    IJavaElement parent = javaElement.getParent();
    if (parent == null) {
        return lastFound;
    }
    IJavaElement ancestor = parent.getAncestor(elementType);
    if (ancestor != null) {
        return getLastAncestor(ancestor, elementType);
    }
    return lastFound;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement//from w  w  w.j  a  v a2 s.com
 * @return first non-anonymous ancestor
 */
private static IJavaElement getFirstNonAnonymous(IJavaElement javaElement, IJavaElement topAncestor) {
    if (javaElement.getElementType() == IJavaElement.TYPE && !isAnonymousType(javaElement)) {
        return javaElement;
    }
    IJavaElement parent = javaElement.getParent();
    if (parent == null) {
        return topAncestor;
    }
    IJavaElement ancestor = parent.getAncestor(IJavaElement.TYPE);
    if (ancestor != null) {
        return getFirstNonAnonymous(ancestor, topAncestor);
    }
    return topAncestor;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param elt//from   w  ww  . j  av a2  s .co m
 * @return true, if given element is inner class from initializer block or method body
 */
private static boolean isAnyParentLocal(IJavaElement elt, IJavaElement topParent) {
    if (isLocal(elt)) {
        return true;
    }
    IJavaElement parent = elt.getParent();
    while (parent != null && parent != topParent) {
        if (isLocal(parent)) {
            return true;
        }
        parent = parent.getParent();
    }
    return false;
}

From source file:de.tobject.findbugs.view.BugInfoView.java

License:Open Source License

private boolean matchInput(IEditorInput input) {
    if (file != null && (input instanceof IFileEditorInput)) {
        return file.equals(((IFileEditorInput) input).getFile());
    }// ww  w  .ja  va2s  .  co  m
    if (javaElt != null && input != null) {
        IJavaElement javaElement = JavaUI.getEditorInputJavaElement(input);
        if (javaElt.equals(javaElement)) {
            return true;
        }
        IJavaElement parent = javaElt.getParent();
        while (parent != null && !parent.equals(javaElement)) {
            parent = parent.getParent();
        }
        if (parent != null && parent.equals(javaElement)) {
            return true;
        }
    }
    return false;
}