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

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

Introduction

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

Prototype

int PACKAGE_FRAGMENT

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

Click Source Link

Document

Constant representing a package fragment.

Usage

From source file:org.grails.ide.eclipse.test.util.AbstractGrailsJUnitIntegrationsTest.java

License:Open Source License

/**
 * COPIED from JUnitLaunchShortcut... create a JUnit lauch config just like the one the JUnit UI would.
 *///from   ww  w. ja v  a 2  s  .c o  m
public static ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element)
        throws CoreException {
    final String testName;
    final String mainTypeQualifiedName;
    final String containerHandleId;

    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT: {
        String name = element.getElementName();
        containerHandleId = element.getHandleIdentifier();
        mainTypeQualifiedName = "";
        testName = name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
    }
        break;
    case IJavaElement.TYPE: {
        containerHandleId = "";
        mainTypeQualifiedName = ((IType) element).getFullyQualifiedName('.'); // don't replace, fix for binary inner types
        testName = element.getElementName();
    }
        break;
    case IJavaElement.METHOD: {
        IMethod method = (IMethod) element;
        containerHandleId = "";
        mainTypeQualifiedName = method.getDeclaringType().getFullyQualifiedName('.');
        testName = method.getDeclaringType().getElementName() + '.' + method.getElementName();
    }
        break;
    default:
        throw new IllegalArgumentException(
                "Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
    }

    String testKindId = TestKindRegistry.getContainerTestKindId(element);

    ILaunchConfigurationType configType = getLaunchManager()
            .getLaunchConfigurationType(JUnitLaunchConfigurationConstants.ID_JUNIT_APPLICATION);
    ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
            getLaunchManager().generateLaunchConfigurationName(testName));

    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainTypeQualifiedName);
    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
            element.getJavaProject().getElementName());
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, containerHandleId);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId);
    JUnitMigrationDelegate.mapResources(wc);
    //AssertionVMArg.setArgDefault(wc);
    if (element instanceof IMethod) {
        wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, element.getElementName()); // only set for methods
    }
    return wc;
}

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 {//  w ww . j  a  v  a 2 s.  c om
        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.incha.core.jswingripples.MethodOverrideTester.java

License:Open Source License

/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.//  ww w . j  av  a  2s .  c  o  m
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(final IMember member, final IPackageFragment pack)
        throws JavaModelException {
    final int type = member.getElementType();
    if (type == IJavaElement.INITIALIZER
            || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
        return false;
    }

    final int otherflags = member.getFlags();

    final IType declaringType = member.getDeclaringType();
    if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags)
            || (declaringType != null && declaringType.isInterface())) {
        return true;
    } else if (Flags.isPrivate(otherflags)) {
        return false;
    }

    final IPackageFragment otherpack = (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    return (pack != null && pack.equals(otherpack));
}

From source file:org.jactr.eclipse.core.parser.ProjectSensitiveParserImportDelegate.java

License:Open Source License

/**
 * we've found the valid class name from the previous call to isValidClassName
 * if the super implementation can't find it, we check for any extensions that
 * provide one../*from w  ww  . j  a  v  a2  s  . com*/
 * 
 * @param moduleClassName
 * @return
 */
@Override
protected IASTParticipant getASTParticipant(String moduleClassName) {
    IASTParticipant participant = super.getASTParticipant(moduleClassName);
    if (participant == null) {
        boolean participantIsDefined = false;

        /*
         * find one ourselves..
         */
        for (ASTParticipantDescriptor descriptor : org.jactr.eclipse.core.bundles.registry.ASTParticipantRegistry
                .getRegistry().getDescriptors(_project))
            if (descriptor.getContributingClassName().equals(moduleClassName)) {
                participantIsDefined = true;

                // the code block below is handled by jactr.io.activator.Activator
                // /*
                // * if we can instantiate this, let's do so.. im not sure if this
                // * should actually be installed since we are project specific.. if
                // we
                // * had different versions active, bad things could happen
                // */
                // try
                // {
                // participant = (IASTParticipant) descriptor.instantiate();
                // if (participant != null) return participant;
                // }
                // catch (Exception e)
                // {
                // CorePlugin.debug("Could not instantiate "
                // + descriptor.getContributingClassName() + " for "
                // + moduleClassName, e);
                // }

                /*
                 * construct and return. We know we can't use participantClass since
                 * this is a workspace extension, we have to use BasicASTParticipat
                 * and content location
                 */
                String contentLocation = descriptor.getContentLocation();
                IPath location = new Path(contentLocation);
                try {
                    IProject contributorProject = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(descriptor.getContributor());

                    // CorePlugin.debug(String.format("Checking %s(%s)",
                    // contributorProject.getName(), contributorProject.exists()));

                    IJavaProject project = JavaCore.create(contributorProject);
                    IResource resource = null;

                    IJavaElement containingPackage = project.findElement(location.removeLastSegments(1));

                    if (containingPackage != null
                            && containingPackage.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("Found package that contains " + location + " at "
                                    + containingPackage.getPath());

                        // CorePlugin.debug("Found package that contains " + location
                        // + " at " + containingPackage.getPath());

                        IResource tmp = containingPackage.getUnderlyingResource();
                        if (tmp instanceof IContainer) {
                            IContainer container = (IContainer) tmp;
                            resource = container.findMember(location.lastSegment());
                        }
                    }
                    // else
                    // CorePlugin.debug(location + " Containing package = "
                    // + containingPackage);

                    if (resource == null)
                        throw new RuntimeException(String.format("Could not find %s (%s) within %s' classpath",
                                contentLocation, location, _project.getName()));

                    URL contentURL = resource.getLocationURI().toURL();

                    // CorePlugin.debug(String.format(
                    // "Creating participant for content @ %s",
                    // contentURL.toExternalForm()));

                    participant = new IDEBasicASTParticipant(contentURL);
                } catch (Exception e) {
                    String message = "Could not create basic ast participant referencing " + contentLocation
                            + " in " + _project.getName();
                    if (LOGGER.isWarnEnabled())
                        LOGGER.warn(message, e);
                    CorePlugin.warn(message, e);
                    return null;
                }
            }

        if (participant == null && participantIsDefined) {
            /*
             * warn and log
             */
            String message = "Could not find a valid IASTParticipant in any dependencies for "
                    + moduleClassName;
            if (LOGGER.isWarnEnabled())
                LOGGER.warn(message);
            CorePlugin.warn(message);
        }
    }

    return participant;
}

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

From source file:org.jboss.ide.eclipse.as.ui.mbeans.wizards.pages.NewSessionBeanWizardPage.java

License:Open Source License

public void createControl(Composite parent) {
    initializeDialogUnits(parent);//w  w  w.j a v a2s.com

    Composite composite = new Composite(parent, SWT.NONE);
    listener = new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            updateTypeNames();
            if (e.getSource() == remoteInterfacePackageText) {
                handleFieldChanged(REMOTE_INTERFACE_PACKAGE_NAME);
            } else if (e.getSource() == beanPackageName) {
                handleFieldChanged(BEAN_PACKAGE_NAME);
                if (!useCustomRemoteInterfacePackage) {
                    handleFieldChanged(REMOTE_INTERFACE_PACKAGE_NAME);
                }
            } else if (e.getSource() == beanNameText) {
                handleFieldChanged(BEAN_NAME);
            }
        }
    };

    int nColumns = 4;

    GridLayout layout = new GridLayout();
    layout.numColumns = nColumns;
    composite.setLayout(layout);

    createContainerControls(composite, nColumns);
    createEnclosingTypeControls(composite, nColumns);
    createBeanTypeControls(composite, nColumns);
    createSeparator(composite, nColumns);

    createBeanNameControls(composite, nColumns);
    createRemoteInterfaceControls(composite, nColumns);

    createSeparator(composite, nColumns);

    createModifierControls(composite, nColumns);

    createSuperClassControls(composite, nColumns);
    createSuperInterfacesControls(composite, nColumns);

    setControl(composite);

    IJavaElement element = getInitialJavaElement(selection);
    if (element != null && element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        beanPackageName.setText(element.getElementName());
        remoteInterfacePackageText.setText(element.getElementName());
    }

    Dialog.applyDialogFont(composite);
    WorkbenchHelp.setHelp(composite, IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE);
}

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  va  2  s. c om*/
 * @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.ui.internal.commands.ArquillianPropertyTester.java

License:Open Source License

private boolean canLaunchAsArquillianJUnitTest(IJavaElement element) {
    try {/* w w w .j  a  v a2s.  c o  m*/
        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;
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.launcher.ArquillianLaunchShortcut.java

License:Open Source License

private void launch(Object[] elements, String mode) {
    try {/*from   w  ww  .j a v a 2 s  . co m*/
        IJavaElement elementToLaunch = null;

        if (elements.length == 1) {
            Object selected = elements[0];
            if (!(selected instanceof IJavaElement) && selected instanceof IAdaptable) {
                selected = ((IAdaptable) selected).getAdapter(IJavaElement.class);
            }
            if (selected instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) selected;
                switch (element.getElementType()) {
                case IJavaElement.JAVA_PROJECT:
                case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                case IJavaElement.PACKAGE_FRAGMENT:
                    IJavaProject javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.TYPE:
                    IType type = (IType) element;
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true)) {
                        elementToLaunch = type;
                    }
                case IJavaElement.METHOD:
                    javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.CLASS_FILE:
                    type = ((IClassFile) element).getType();
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true, false)) {
                        elementToLaunch = type;
                    }
                    break;
                case IJavaElement.COMPILATION_UNIT:
                    elementToLaunch = findTypeToLaunch((ICompilationUnit) element, mode);
                    break;
                }
            }
        }
        if (elementToLaunch == null) {
            showNoTestsFoundDialog();
            return;
        }
        performLaunch(elementToLaunch, mode);
    } catch (InterruptedException e) {
        // OK, silently move on
    } catch (CoreException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    } catch (InvocationTargetException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    }
}