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.evosuite.eclipse.quickfixes.MarkerWriter.java

License:Open Source License

public IJavaElement getMethod(IJavaElement e) {
    IJavaElement method = e;
    while (method != null && method.getElementType() != IJavaElement.METHOD) {
        method = method.getParent();
    }/*  w  ww. j  a v  a  2  s.  c  om*/
    return method;

}

From source file:org.evosuite.eclipse.quickfixes.MarkerWriter.java

License:Open Source License

private boolean shouldWriteMarkers(IJavaElement currentElement) {
    IJavaElement parent = currentElement;
    while (parent != null) {

        if (parent instanceof IAnnotatable) {
            IAnnotatable p = (IAnnotatable) parent;
            try {
                for (IAnnotation a : p.getAnnotations()) {
                    if (a.getElementName().equalsIgnoreCase("EvoIgnore")) {
                        return false;
                    }//  w  w w.  j av a2 s  . c o m
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
        parent = parent.getParent();
    }
    return true;
}

From source file:org.evosuite.eclipse.quickfixes.TestGenerationTrigger.java

License:Open Source License

/**
 * Add a new test generation job to the job queue
 * // w w w. jav a 2  s .  co  m
 * @param target
 */
@Override
protected void addTestJob(final IResource target) {
    //TODO: Must do a merging instead of over writing entire test suite
    IJavaElement element = JavaCore.create(target);
    if (element == null) {
        return;
    }
    IJavaElement packageElement = element.getParent();

    String packageName = packageElement.getElementName();

    final String targetClass = (!packageName.isEmpty() ? packageName + "." : "")
            + target.getName().replace(".java", "").replace(File.separator, ".");
    System.out.println("Building new job for " + targetClass);
    job = new TestGenerationJob(shell, target, targetClass);
    job.setPriority(Job.DECORATE);
    job.schedule();
}

From source file:org.grails.ide.eclipse.editor.gsp.search.FindTagReferences.java

License:Open Source License

/**
 * only check for tags if the java element states that 
 * is a field in a taglib/*from www  . j  a  v a2  s .c o m*/
 * @param elt
 * @return the tag name if the specification corresponds to a tag, or else null
 */
boolean shouldSearchForTagRefs(IJavaElement elt) {
    // strangely, if the referenced tag is from a class file, then the type is ILocalVariable
    if (elt.getElementType() == IJavaElement.LOCAL_VARIABLE) {
        elt = elt.getParent();
    }
    if (elt.getElementType() == IJavaElement.FIELD) {
        ICompilationUnit unit = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (unit instanceof GroovyCompilationUnit) {
            if (GrailsWorkspaceCore.isTagLibClass((GroovyCompilationUnit) unit)) {
                tagField = elt;
                tagName = tagField.getElementName();
            }
        } else {
            // could be a built in tag
            IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
            if (type != null && type.isReadOnly() && type.getElementName().endsWith("TagLib")) {
                IPackageFragment frag = (IPackageFragment) elt.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (frag.getElementName().equals("org.codehaus.groovy.grails.plugins.web.taglib")) {
                    tagField = elt;
                    tagName = tagField.getElementName();
                }
            }
        }
    }
    return tagField != null;
}

From source file:org.grails.ide.eclipse.editor.gsp.translation.GSPTranslation.java

License:Open Source License

/**
 * Originally from ReconcileStepForJava.  Creates an ICompilationUnit from the contents of the JSP document.
 * //from   www.  j  a v a2 s.  com
 * @return an ICompilationUnit from the contents of the JSP document
 */
private ICompilationUnit createCompilationUnit() throws JavaModelException {

    IPackageFragment packageFragment = null;
    IJavaElement je = getJavaProject();

    if (je == null || !je.exists())
        return null;

    switch (je.getElementType()) {
    case IJavaElement.PACKAGE_FRAGMENT:
        je = je.getParent();
        // fall through

    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) je;
        packageFragment = packageFragmentRoot.getPackageFragment(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH);
        break;

    case IJavaElement.JAVA_PROJECT:
        IJavaProject jProject = (IJavaProject) je;

        if (!jProject.exists()) {
            if (DEBUG) {
                System.out.println(
                        "** Abort create working copy: cannot create working copy: JSP is not in a Java project"); //$NON-NLS-1$
            }
            return null;
        }

        packageFragmentRoot = null;
        IPackageFragmentRoot[] packageFragmentRoots = jProject.getPackageFragmentRoots();
        int i = 0;
        while (i < packageFragmentRoots.length) {
            if (!packageFragmentRoots[i].isArchive() && !packageFragmentRoots[i].isExternal()) {
                packageFragmentRoot = packageFragmentRoots[i];
                break;
            }
            i++;
        }
        if (packageFragmentRoot == null) {
            if (DEBUG) {
                System.out.println(
                        "** Abort create working copy: cannot create working copy: JSP is not in a Java project with source package fragment root"); //$NON-NLS-1$
            }
            return null;
        }
        packageFragment = packageFragmentRoot.getPackageFragment(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH);
        break;

    default:
        return null;
    }

    // GRAILS CHANGE
    // create compilation unit with .groovy instead of .java file extension
    ICompilationUnit cu = packageFragment.getCompilationUnit(getClassname() + ".groovy") //$NON-NLS-1$
            .getWorkingCopy(getWorkingCopyOwner(), getProgressMonitor());
    //        ICompilationUnit cu = packageFragment.getCompilationUnit(getClassname() + ".java").getWorkingCopy(getWorkingCopyOwner(), getProgressMonitor()); //$NON-NLS-1$
    setContents(cu);

    // GRAILS CHANGE
    // need extra call to makeConsistent
    // https://issuetracker.springsource.com/browse/STS-3091#comment-79054
    cu.makeConsistent(getProgressMonitor());

    if (DEBUG) {
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //$NON-NLS-1$
        System.out.println("(+) JSPTranslation [" + this + "] finished creating CompilationUnit: " + cu); //$NON-NLS-1$ //$NON-NLS-2$
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //$NON-NLS-1$
    }

    return cu;
}

From source file:org.grails.ide.eclipse.search.action.ControllerActionSearch.java

License:Open Source License

public ControllerActionSearch(QuerySpecification specification) throws JavaModelException {
    if (specification instanceof ElementQuerySpecification) {
        ElementQuerySpecification spec = (ElementQuerySpecification) specification;
        if (SearchUtil.wantsReferences(spec)) {
            IJavaSearchScope scope = spec.getScope();
            IJavaElement el = spec.getElement();
            if (el.getElementType() == IJavaElement.METHOD || el.getElementType() == IJavaElement.FIELD) {
                IMember targetAction = (IMember) el;
                IType targetType = (IType) el.getParent();
                String targetActionName = targetAction.getElementName();
                IJavaProject project = targetAction.getJavaProject();
                if (project != null) {
                    GrailsProject grailsProject = GrailsWorkspaceCore.get().create(project);
                    if (grailsProject != null) {
                        init(grailsProject, targetType.getElementName(), targetActionName, scope);
                    }/*  w  w w  .j  a  v a 2 s  .c o m*/
                }
            }
        }
    }
}

From source file:org.jaml.eclipse.editors.JamlContentAssistProcessor.java

License:Open Source License

private IJavaElement getHighestRoot(IJavaElement element) {
    return element.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT
            && element.getElementType() != IJavaElement.JAVA_PROJECT && element.getParent() != null
                    ? getHighestRoot(element.getParent())
                    : element;/*  ww w  .  j  av a2 s. c o m*/
}

From source file:org.jaml.eclipse.utils.JDTUtils.java

License:Open Source License

public static String getIJavaElementPath(IJavaElement element) throws JavaModelException {
    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.PACKAGE_FRAGMENT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.COMPILATION_UNIT:
        return element.getCorrespondingResource().getFullPath().toString().replace(element.getElementName(),
                "");
    case IJavaElement.TYPE:
        return getIJavaElementPath(element.getParent());
    }//from   ww w .ja v  a 2  s  .  co  m
    return JavaExt.STRING_EMPTY;
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * Returns the resource path relative to its containing
 * IPackageFragmentRoot. If the resource is not located within a Java source
 * directory, the project name is stripped from the path.
 * // w w  w.  j a v  a  2s . com
 * @param resource the resource.
 * 
 * @return the relative path.
 */
public static IPath getJavaPathForResource(final IResource resource) {
    if (resource == null || resource.getType() == IResource.PROJECT || resource.getType() == IResource.ROOT) {
        return null;
    }
    IJavaProject project = JavaCore.create(resource.getProject());
    if (project == null) {
        // just remove the project segment.
        return resource.getFullPath().removeFirstSegments(1);
    }
    IResource container = resource;
    if (container.getType() == IResource.FILE) {
        container = container.getParent();
    }
    IJavaElement element = null;
    for (; element == null && container != null; container = container.getParent()) {
        element = JavaCore.create(container, project);
    }
    if (element == null) {
        return resource.getFullPath().removeFirstSegments(1);
    } else if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        return resource.getFullPath().makeRelativeTo(element.getParent().getPath());
    }
    return resource.getFullPath().makeRelativeTo(element.getPath());
}

From source file:org.jboss.tools.arquillian.core.internal.util.ArquillianSearchEngine.java

License:Open Source License

public static boolean isAccessibleClass(IType type) throws JavaModelException {
    if (type == null) {
        return false;
    }//from w  w w  .jav  a 2 s .c  o m
    int flags = type.getFlags();
    if (Flags.isInterface(flags)) {
        return false;
    }
    IJavaElement parent = type.getParent();
    while (true) {
        if (parent instanceof ICompilationUnit || parent instanceof IClassFile) {
            return true;
        }
        if (!(parent instanceof IType) || !Flags.isStatic(flags) || !Flags.isPublic(flags)) {
            return false;
        }
        flags = ((IType) parent).getFlags();
        parent = parent.getParent();
    }
}