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:com.android.ide.eclipse.adt.internal.launch.junit.AndroidJUnitLaunchConfigurationTab.java

License:Open Source License

private void initializeTestAttributes(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
    if (javaElement != null && javaElement.getElementType() < IJavaElement.COMPILATION_UNIT) {
        initializeTestContainer(javaElement, config);
    } else {/* www  .ja v  a2 s.co  m*/
        initializeTestType(javaElement, config);
    }
}

From source file:com.android.ide.eclipse.adt.internal.launch.junit.AndroidJUnitPropertyTester.java

License:Open Source License

private boolean canLaunchAsJUnitTest(IJavaElement element) {
    try {// w w w .  ja v a  2 s .com
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            return true; // can run, let JDT detect if there are tests
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return false; // not supported by Android test runner
        case IJavaElement.PACKAGE_FRAGMENT:
            return ((IPackageFragment) element).hasChildren();
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.CLASS_FILE:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
            return isJUnitTest(element);
        default:
            return false;
        }
    } catch (JavaModelException e) {
        return false;
    }
}

From source file:com.android.ide.eclipse.editors.manifest.descriptors.PostActivityCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity.//from   w w  w .j a  va2 s. c o m
 * 
 */
public void processNewType(IType newType) {
    try {
        String methodContent = "    /** Called when the activity is first created. */\n" + "    @Override\n"
                + "    public void onCreate(Bundle savedInstanceState) {\n"
                + "        super.onCreate(savedInstanceState);\n" + "\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(AndroidConstants.CLASS_BUNDLE, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.android.ide.eclipse.editors.manifest.descriptors.PostReceiverCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity.// w ww  .j  a v  a2  s.c o  m
 * 
 */
public void processNewType(IType newType) {
    try {
        String methodContent = "    @Override\n"
                + "    public void onReceive(Context context, Intent intent) {\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(AndroidConstants.CLASS_CONTEXT, null /* sibling */,
                    new NullProgressMonitor());
            compilationUnit.createImport(AndroidConstants.CLASS_INTENT, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // looks like the class already existed (this happens when the user check to create
        // inherited abstract methods).
    }
}

From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java

License:Apache License

private ICompilationUnit getGinModule(IPackageFragment packageFragment, IProgressMonitor monitor)
        throws JavaModelException {
    if (packageFragment.getChildren() != null) {
        for (IJavaElement element : packageFragment.getChildren()) {
            if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
                ICompilationUnit cUnit = (ICompilationUnit) element;
                logger.info("Compilation Unit: " + cUnit.getElementName());
                for (IType type : cUnit.getTypes()) {
                    ITypeHierarchy hierarchy = type.newSupertypeHierarchy(monitor);
                    IType[] interfaces = hierarchy.getAllInterfaces();
                    for (IType checkInterface : interfaces) {
                        logger.info(checkInterface.getTypeQualifiedName());
                        if (checkInterface.getTypeQualifiedName().equals("GinModule")) {
                            return cUnit;
                        }//w  ww.  ja  va 2  s. c  o m
                    }
                }
            }
        }
    }

    String packageName = packageFragment.getElementName();
    if (packageName.contains(".")) {
        packageName = packageName.substring(0, packageName.lastIndexOf("."));
        return getGinModule(getPackageFragmentRoot().getPackageFragment(packageName), monitor);
    }

    return null;
}

From source file:com.ashigeru.eclipse.util.jdt.internal.ui.handlers.InsertAssertionHandler.java

License:Apache License

private ICompilationUnit getJavaElement(IEditorPart editor) {
    assert editor != null;
    IEditorInput input = editor.getEditorInput();
    ITypeRoot element = JavaUI.getEditorInputTypeRoot(input);
    if (element == null || element.getElementType() != IJavaElement.COMPILATION_UNIT) {
        return null;
    }/*from w  w  w.j a v  a 2 s.  c  o m*/
    return (ICompilationUnit) element;
}

From source file:com.cb.eclipse.folding.java.calculation.StrategyFactory.java

License:Open Source License

/**
 * Get a CalculationStrategy instance that is capable of handling the region
 * building for <code>elem</code>
 * /*from   ww w .j ava 2 s  .c o m*/
 * @param elem
 *            The java element to find a calculation strategy for.
 * @return The Calculation strategy meant for processing elem.
 */
public static RegionCalculationStrategy instance(JavaProjectionCalculator calculator, IJavaElement elem) {
    int type = elem.getElementType();
    switch (type) {
    case IJavaElement.TYPE:
        return new CommentPrefixStrategy(new TypeStrategy());
    case IJavaElement.FIELD:
    case IJavaElement.METHOD:
        return new CommentPrefixStrategy(new MethodStrategy());
    case IJavaElement.IMPORT_CONTAINER:
        return new ImportContainerStrategy();
    case IJavaElement.COMPILATION_UNIT:
    case IJavaElement.CLASS_FILE:
        return new UserDefinedStrategy(new RootStrategy());
    case IJavaElement.INITIALIZER:
        return new CommentPrefixStrategy(new StaticInitializerStrategy());
    default:
        return NO_OP_STRATEGY;
    }
}

From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java

License:Open Source License

/**
 * Returns the enclosing type names of the given type.
 *///from w  w  w. j  a  v  a  2 s.  c  o  m
private static char[][] enclosingTypeNames(IType type) {
    IJavaElement parent = type.getParent();
    switch (parent.getElementType()) {
    case IJavaElement.CLASS_FILE:
        // For a binary type, the parent is not the enclosing type, but the declaring type is.
        // (see bug 20532  Declaration of member binary type not found)
        IType declaringType = type.getDeclaringType();
        if (declaringType == null)
            return CharOperation.NO_CHAR_CHAR;
        return CharOperation.arrayConcat(enclosingTypeNames(declaringType),
                declaringType.getElementName().toCharArray());
    case IJavaElement.COMPILATION_UNIT:
        return CharOperation.NO_CHAR_CHAR;
    case IJavaElement.FIELD:
    case IJavaElement.INITIALIZER:
    case IJavaElement.METHOD:
        IType declaringClass = ((IMember) parent).getDeclaringType();
        return CharOperation.arrayConcat(enclosingTypeNames(declaringClass),
                new char[][] { declaringClass.getElementName().toCharArray(), IIndexConstants.ONE_STAR });
    case IJavaElement.TYPE:
        return CharOperation.arrayConcat(enclosingTypeNames((IType) parent),
                parent.getElementName().toCharArray());
    default:
        return null;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ICompilationUnit#findElements(org.eclipse.jdt.core.IJavaElement)
 *//* w  ww .  j  ava  2  s.  c o m*/
public IJavaElement[] findElements(IJavaElement element) {
    ArrayList children = new ArrayList();
    while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
        children.add(element);
        element = element.getParent();
    }
    if (element == null)
        return null;
    IJavaElement currentElement = this;
    for (int i = children.size() - 1; i >= 0; i--) {
        SourceRefElement child = (SourceRefElement) children.get(i);
        switch (child.getElementType()) {
        case IJavaElement.PACKAGE_DECLARATION:
            currentElement = ((ICompilationUnit) currentElement).getPackageDeclaration(child.getElementName());
            break;
        case IJavaElement.IMPORT_CONTAINER:
            currentElement = ((ICompilationUnit) currentElement).getImportContainer();
            break;
        case IJavaElement.IMPORT_DECLARATION:
            currentElement = ((IImportContainer) currentElement).getImport(child.getElementName());
            break;
        case IJavaElement.TYPE:
            switch (currentElement.getElementType()) {
            case IJavaElement.COMPILATION_UNIT:
                currentElement = ((ICompilationUnit) currentElement).getType(child.getElementName());
                break;
            case IJavaElement.TYPE:
                currentElement = ((IType) currentElement).getType(child.getElementName());
                break;
            case IJavaElement.FIELD:
            case IJavaElement.INITIALIZER:
            case IJavaElement.METHOD:
                currentElement = ((IMember) currentElement).getType(child.getElementName(),
                        child.occurrenceCount);
                break;
            }
            break;
        case IJavaElement.INITIALIZER:
            currentElement = ((IType) currentElement).getInitializer(child.occurrenceCount);
            break;
        case IJavaElement.FIELD:
            currentElement = ((IType) currentElement).getField(child.getElementName());
            break;
        case IJavaElement.METHOD:
            currentElement = ((IType) currentElement).getMethod(child.getElementName(),
                    ((IMethod) child).getParameterTypes());
            break;
        }

    }
    if (currentElement != null && currentElement.exists()) {
        return new IJavaElement[] { currentElement };
    } else {
        return null;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnitStructureRequestor.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor
 */// ww w. j  a v a2  s .c  om
public void acceptImport(int declarationStart, int declarationEnd, int nameSourceStart, int nameSourceEnd,
        char[][] tokens, boolean onDemand, int modifiers) {
    JavaElement parentHandle = (JavaElement) this.handleStack.peek();
    if (!(parentHandle.getElementType() == IJavaElement.COMPILATION_UNIT)) {
        Assert.isTrue(false); // Should not happen
    }

    ICompilationUnit parentCU = (ICompilationUnit) parentHandle;
    //create the import container and its info
    if (this.importContainer == null) {
        this.importContainer = createImportContainer(parentCU);
        this.importContainerInfo = new ImportContainerInfo();
        Object parentInfo = this.infoStack.peek();
        addToChildren(parentInfo, this.importContainer);
        this.newElements.put(this.importContainer, this.importContainerInfo);
    }

    String elementName = manager.intern(new String(CharOperation.concatWith(tokens, '.')));
    ImportDeclaration handle = createImportDeclaration(this.importContainer, elementName, onDemand);
    resolveDuplicates(handle);

    ImportDeclarationElementInfo info = new ImportDeclarationElementInfo();
    info.setSourceRangeStart(declarationStart);
    info.setSourceRangeEnd(declarationEnd);
    info.setNameSourceStart(nameSourceStart);
    info.setNameSourceEnd(nameSourceEnd);
    info.setFlags(modifiers);

    addToChildren(this.importContainerInfo, handle);
    this.newElements.put(handle, info);
}