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

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

Introduction

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

Prototype

int COMPILATION_UNIT

To view the source code for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.

Click Source Link

Document

Constant representing a Java compilation unit.

Usage

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

/**
 * @param folder//from   w  ww.jav a2  s .c o  m
 * @return
 */
private List<TagLibClass> findTagLibClassesInFolder(IFolder folder) {
    List<TagLibClass> taglibs = new ArrayList<TagLibClass>();
    if (folder.exists()) {
        try {
            IPackageFragmentRoot root = (IPackageFragmentRoot) JavaCore.create(folder);
            if (root != null && root.exists()) {
                for (IJavaElement elt : root.getChildren()) {
                    if (elt.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                        IPackageFragment pack = (IPackageFragment) elt;
                        ICompilationUnit[] units = pack.getCompilationUnits();
                        for (ICompilationUnit unit : units) {
                            if (unit instanceof GroovyCompilationUnit
                                    && unit.getElementName().endsWith("TagLib.groovy")) {
                                taglibs.add(getTagLibClass((GroovyCompilationUnit) unit));
                            }
                        }
                    } else if (elt.getElementType() == IJavaElement.COMPILATION_UNIT) {
                        ICompilationUnit unit = (ICompilationUnit) elt;
                        if (unit instanceof GroovyCompilationUnit
                                && unit.getElementName().endsWith("TagLib.groovy")) {
                            taglibs.add(new TagLibClass((GroovyCompilationUnit) unit));
                        }
                    }
                }
            } else {
                GrailsCoreActivator.log("Problem when looking for tag libraries:\n"
                        + folder.getLocation().toOSString() + " is not a source folder.",
                        new RuntimeException());
            }
        } catch (JavaModelException e) {
            GrailsCoreActivator.log(
                    "Exception when trying to get all taglib classes for " + javaProject.getElementName(), e);
        }
    }
    return taglibs;
}

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 ww w  .  j a  va2 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.refactoring.rename.ParticipantChangeManager.java

License:Open Source License

public TextChange getTextChangeFor(Object el) {
    if (el instanceof IJavaElement) {
        IJavaElement jel = (IJavaElement) el;
        ICompilationUnit cu = (ICompilationUnit) jel.getAncestor(IJavaElement.COMPILATION_UNIT);
        return getCUChange(cu);
    } else if (el instanceof IFile) {
        return getFileChange((IFile) el);
    }/*from  w  w w.  j a  v  a  2s .  c o  m*/
    return null;
}

From source file:org.grails.ide.eclipse.refactoring.rename.type.ServiceReferenceUpdater.java

License:Open Source License

@Override
public void createChanges(final ParticipantChangeManager changes, final RefactoringStatus status,
        IProgressMonitor pm) {/*from  w w  w .j a v a  2 s. co  m*/
    Assert.isNotNull(changes);
    try {
        IJavaSearchScope scope = RefactoringUtils.getSearchScope(project.getJavaProject());
        SearchEngine engine = new SearchEngine();

        final String fieldName = GrailsNameUtils.getPropertyName(renaming.getTarget().getFullyQualifiedName());
        final String fieldNewName = GrailsNameUtils.getPropertyName(renaming.getNewName());

        SearchPattern fieldByNamePat = SearchPattern.createPattern(fieldName, IJavaSearchConstants.FIELD,
                IJavaSearchConstants.ALL_OCCURRENCES,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

        SearchRequestor req = new SearchRequestor() {
            @Override
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                try {
                    if (match instanceof FieldDeclarationMatch || match instanceof FieldReferenceMatch) {
                        Object el = match.getElement();
                        if (el instanceof IJavaElement) {
                            IJavaElement jel = (IJavaElement) el;
                            ICompilationUnit cu = (ICompilationUnit) jel
                                    .getAncestor(IJavaElement.COMPILATION_UNIT);
                            if (cu != null) {
                                TextChange cuChange = changes.getCUChange(cu);
                                final int offset = match.getOffset();
                                final int length = match.getLength();
                                String text = cu.getBuffer().getText(offset, length);
                                if (text.equals(fieldName)) {
                                    //Only perform the edit if the text we are about to replace is what we expect!
                                    cuChange.addEdit(new ReplaceEdit(offset, length, fieldNewName));
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    //Ignore this match and log the exception...
                    //but keep processing other matches!
                    GrailsCoreActivator.log(e);
                }
            }

        };
        engine.search(fieldByNamePat, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                scope, req, pm);
    } catch (CoreException e) {
        GrailsCoreActivator.log(e);
    }
}

From source file:org.grails.ide.eclipse.ui.internal.launch.GrailsTestLaunchShortcut.java

License:Open Source License

@Override
protected String getScriptFor(IResource resource) {
    if (GrailsResourceUtil.isTestFolder(resource)) {
        String script = TEST_APP + " -" + resource.getName();
        System.out.println("grails command = " + script);
        return script;
    } else {//from   ww w  . jav a2 s .com
        String script = TEST_APP;
        String testType = GrailsResourceUtil.typeOfTest(resource);
        if (testType != null) {
            script += " -" + testType;
        }
        if (GrailsResourceUtil.isSourceFile(resource)) {
            IJavaElement javaElement = JavaCore.create(resource);
            if (javaElement != null) {
                int elementType = javaElement.getElementType();
                switch (elementType) {
                case IJavaElement.PACKAGE_FRAGMENT:
                    IPackageFragment pkg = (IPackageFragment) javaElement;
                    script += " " + pkg.getElementName() + ".*";
                    break;
                case IJavaElement.COMPILATION_UNIT:
                    String pathIncludingSourceFolder = resource.getProjectRelativePath().toString();
                    IPackageFragmentRoot sourceRoot = (IPackageFragmentRoot) javaElement
                            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                    String sourceFolderPath = sourceRoot.getResource().getProjectRelativePath().toString();
                    Assert.isTrue(pathIncludingSourceFolder.startsWith(sourceFolderPath + "/"));
                    String pathWithoutSourceFolder = pathIncludingSourceFolder
                            .substring(sourceFolderPath.length() + 1);
                    if (isRecentGrails(resource.getProject())) {
                        pathWithoutSourceFolder = removeSuffix(pathWithoutSourceFolder, ".groovy");
                    } else {
                        pathWithoutSourceFolder = removeSuffix(pathWithoutSourceFolder, "Test.groovy",
                                "Tests.groovy", ".groovy");
                    }
                    if (pathWithoutSourceFolder != null) {
                        String testTarget = pathWithoutSourceFolder.replace('/', '.');
                        script += " " + testTarget;
                    }
                default:
                    break;
                }
            }
        }
        debug("grails command = " + script);
        return script;
    }
}

From source file:org.gw4e.eclipse.launching.GW4ELaunchShortcut.java

License:Open Source License

private void launch(Object[] elements, String mode) {
    try {//from w  ww .  j a va2 s  .  c o  m
        IJavaElement[] ijes = null;
        List<IJavaElement> jes = new ArrayList<IJavaElement>();
        for (int i = 0; i < elements.length; i++) {
            Object selected = elements[i];
            if (selected instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) selected;
                switch (element.getElementType()) {
                case IJavaElement.COMPILATION_UNIT:
                    jes.add(element);
                    break;
                }
            }

        }
        ijes = new IJavaElement[jes.size()];
        jes.toArray(ijes);
        ILaunchConfigurationWorkingCopy wc = buildLaunchConfiguration(ijes);
        if (wc == null)
            return;
        ILaunchConfiguration config = findExistingORCreateLaunchConfiguration(wc, mode);
        DebugUITools.launch(config, mode);
    } catch (Exception e) {
        ResourceManager.logException(e);
        MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), "GW4E Launcher",
                (Image) null, "Unable to launch. See error in Error view.", MessageDialog.ERROR,
                new String[] { "Close" }, 0);
        dialog.open();
    }
}

From source file:org.hibernate.eclipse.console.utils.xpl.SelectionHelper.java

License:Open Source License

static public IType getClassFromElement(IJavaElement element) {
    IType classToTest = null;//from ww  w. j a  v  a  2s.co  m
    if (element != null) {
        // evaluate the enclosing type
        IType typeInCompUnit = (IType) element.getAncestor(IJavaElement.TYPE);
        if (typeInCompUnit != null) {
            if (typeInCompUnit.getCompilationUnit() != null) {
                classToTest = typeInCompUnit;
            }
        } else {
            ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null)
                classToTest = cu.findPrimaryType();
            else {
                if (element instanceof IClassFile) {
                    try {
                        IClassFile cf = (IClassFile) element;
                        if (cf.isStructureKnown())
                            classToTest = cf.getType();
                    } catch (JavaModelException e) {
                        HibernateConsolePlugin.getDefault().log(e);
                    }
                }
            }
        }
    }
    return classToTest;
}

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

License:Open Source License

public static void findJavaTypes(List<IType> list, IJavaElement element) throws JavaModelException {
    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
        // Java project
        IJavaProject proj = (IJavaProject) element;
        findJavaTypes(list, proj.getChildren());
        break;/*w  ww.j  a v  a 2  s .  c o m*/
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        // JAR file
        IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) element;
        findJavaTypes(list, pkgRoot.getChildren());
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        // Java package
        IPackageFragment pkgFragment = (IPackageFragment) element;
        findJavaTypes(list, pkgFragment.getChildren());
        break;
    case IJavaElement.COMPILATION_UNIT:
        // Source file (.java)
        ICompilationUnit cmUnit = (ICompilationUnit) element;
        findJavaTypes(list, cmUnit.getTypes());
        break;
    case IJavaElement.CLASS_FILE:
        // Compiled file (.class)
        IClassFile clFile = (IClassFile) element;
        findJavaTypes(list, clFile.getType());
        break;
    case IJavaElement.TYPE:
        // Java class
        IType type = (IType) element;
        if (!type.getFullyQualifiedName().contains("$")) {
            list.add(type);
        }
        break;
    }
}

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());
    }/* ww  w. j a v a 2 s.co m*/
    return JavaExt.STRING_EMPTY;
}

From source file:org.jboss.tools.arquillian.ui.internal.commands.ArquillianPropertyTester.java

License:Open Source License

private boolean canLaunchAsArquillianJUnitTest(IJavaElement element) {
    try {// w ww .java  2s  .  c om
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return true; // can run, let test runner detect if there are tests
        case IJavaElement.PACKAGE_FRAGMENT:
            return ((IPackageFragment) element).hasChildren();
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.CLASS_FILE:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
            return ArquillianSearchEngine.isArquillianJUnitTest(element, true, true, false);
        default:
            return false;
        }
    } catch (JavaModelException e) {
        return false;
    }
}