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.eclim.plugin.jdt.command.doc.CommentCommand.java

License:Open Source License

/**
 * Comment a type declaration.//from  www.j av  a2s .com
 *
 * @param src The source file.
 * @param javadoc The Javadoc.
 * @param element The IJavaElement.
 * @param isNew true if there was no previous javadoc for this element.
 */
private void commentType(ICompilationUnit src, Javadoc javadoc, IJavaElement element, boolean isNew)
        throws Exception {
    if (element.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
        @SuppressWarnings("unchecked")
        List<TagElement> tags = javadoc.tags();
        IProject project = element.getJavaProject().getProject();
        if (isNew) {
            addTag(javadoc, tags.size(), null, null);
            addTag(javadoc, tags.size(), null, null);
            addTag(javadoc, tags.size(), TagElement.TAG_AUTHOR, getAuthor(project));
            String version = getPreferences().getValue(project, "org.eclim.java.doc.version");
            if (version != null && !StringUtils.EMPTY.equals(version.trim())) {
                version = StringUtils.replace(version, "\\$", "$");
                addTag(javadoc, tags.size(), TagElement.TAG_VERSION, version);
            }
        } else {
            // check if author tag exists.
            int index = -1;
            String author = getAuthor(project);
            for (int ii = 0; ii < tags.size(); ii++) {
                TagElement tag = (TagElement) tags.get(ii);
                if (TagElement.TAG_AUTHOR.equals(tag.getTagName())) {
                    String authorText = tag.fragments().size() > 0
                            ? ((TextElement) tag.fragments().get(0)).getText()
                            : null;
                    // check if author tag is the same.
                    if (authorText != null && author.trim().equals(authorText.trim())) {
                        index = -1;
                        break;
                    }
                    index = ii + 1;
                } else if (tag.getTagName() != null) {
                    if (index == -1) {
                        index = ii;
                    }
                }
            }

            // insert author tag if it doesn't exist.
            if (index > -1) {
                TagElement authorTag = javadoc.getAST().newTagElement();
                TextElement authorText = javadoc.getAST().newTextElement();
                authorText.setText(author);
                authorTag.setTagName(TagElement.TAG_AUTHOR);

                @SuppressWarnings("unchecked")
                List<ASTNode> fragments = authorTag.fragments();
                fragments.add(authorText);
                tags.add(index, authorTag);
            }

            // add the version tag if it doesn't exist.
            boolean versionExists = false;
            for (int ii = 0; ii < tags.size(); ii++) {
                TagElement tag = (TagElement) tags.get(ii);
                if (TagElement.TAG_VERSION.equals(tag.getTagName())) {
                    versionExists = true;
                    break;
                }
            }
            if (!versionExists) {
                String version = getPreferences().getValue(project, "org.eclim.java.doc.version");
                version = StringUtils.replace(version, "\\$", "$");
                addTag(javadoc, tags.size(), TagElement.TAG_VERSION, version);
            }
        }
    } else {
        commentOther(src, javadoc, element, isNew);
    }
}

From source file:org.eclim.plugin.jdt.util.JavaUtils.java

License:Open Source License

/**
 * Gets the primary element (compilation unit or class file) for the supplied
 * element./* w  w  w.j  av  a 2 s  . c om*/
 *
 * @param element The element.
 * @return The primary element.
 */
public static IJavaElement getPrimaryElement(IJavaElement element) {
    IJavaElement parent = element;
    while (parent.getElementType() != IJavaElement.COMPILATION_UNIT
            && parent.getElementType() != IJavaElement.CLASS_FILE) {
        parent = parent.getParent();
    }
    return parent;
}

From source file:org.eclim.plugin.jdt.util.JavaUtils.java

License:Open Source License

/**
 * Get the offset of the supplied element within the source.
 *
 * @param element The element/*from w  ww. j a v  a 2  s  .c  om*/
 * @return The offset or -1 if it could not be determined.
 */
public static int getElementOffset(IJavaElement element) throws Exception {
    IJavaElement parent = getPrimaryElement(element);
    CompilationUnit cu = null;
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        cu = ASTUtils.getCompilationUnit((ICompilationUnit) parent);
        break;
    case IJavaElement.CLASS_FILE:
        try {
            cu = ASTUtils.getCompilationUnit((IClassFile) parent);
        } catch (IllegalStateException ise) {
            // no source attachement
        }
        break;
    }

    if (cu != null) {
        ASTNode[] nodes = ASTNodeSearchUtil.getDeclarationNodes(element, cu);
        if (nodes != null && nodes.length > 0) {
            int offset = nodes[0].getStartPosition();
            if (nodes[0] instanceof BodyDeclaration) {
                Javadoc docs = ((BodyDeclaration) nodes[0]).getJavadoc();
                if (docs != null) {
                    offset += docs.getLength() + 1;
                }
            }
            return offset;
        }
    }
    return -1;
}

From source file:org.eclim.plugin.jdt.util.JavaUtils.java

License:Open Source License

/**
 * Gets the fully qualified name of the supplied java element.
 * <p/>//from  w w w .jav  a 2s.c o  m
 * NOTE: For easy of determining fields and method segments, they are appended
 * with a javadoc style '#' instead of the normal '.'.
 *
 * @param element The IJavaElement.
 *
 * @return The fully qualified name.
 */
public static String getFullyQualifiedName(IJavaElement element) {
    IJavaElement parent = element;
    while (parent.getElementType() != IJavaElement.COMPILATION_UNIT
            && parent.getElementType() != IJavaElement.CLASS_FILE) {
        parent = parent.getParent();
    }

    StringBuffer elementName = new StringBuffer().append(parent.getParent().getElementName()).append('.')
            .append(FileUtils.getFileName(parent.getElementName()));

    switch (element.getElementType()) {
    case IJavaElement.FIELD:
        IField field = (IField) element;
        elementName.append('#').append(field.getElementName());
        break;
    case IJavaElement.METHOD:
        IMethod method = (IMethod) element;
        elementName.append('#').append(method.getElementName()).append('(');
        String[] parameters = method.getParameterTypes();
        for (int ii = 0; ii < parameters.length; ii++) {
            if (ii != 0) {
                elementName.append(", ");
            }
            elementName.append(Signature.toString(parameters[ii]).replace('/', '.'));
        }
        elementName.append(')');
        break;
    }

    return elementName.toString();
}

From source file:org.eclim.plugin.jdt.util.TypeUtils.java

License:Open Source License

/**
 * Gets the type at the supplied offset, which will either be the primary type
 * of the comilation unit, or an inner class.
 *
 * @param src The ICompilationSource.//www. j  a  v a 2 s .c o m
 * @param offset The offet in the source file.
 * @return The IType.
 */
public static IType getType(ICompilationUnit src, int offset) throws Exception {
    IJavaElement element = src.getElementAt(offset);
    IType type = null;

    // offset outside the class source (Above the package declaration most
    // likely)
    if (element == null) {
        type = ((CompilationUnit) src).getTypeRoot().findPrimaryType();

        // inner class
    } else if (element != null && element.getElementType() == IJavaElement.TYPE) {
        type = (IType) element;
    } else {
        element = element.getParent();

        // offset on import statement
        if (element.getElementType() == IJavaElement.IMPORT_CONTAINER) {
            element = element.getParent();
        }

        // offset on the package declaration or continuation of import ^
        if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
            element = ((CompilationUnit) element).getTypeRoot().findPrimaryType();
        }
        type = (IType) element;
    }
    return type;
}

From source file:org.eclipse.ajdt.core.model.AJModelChecker.java

License:Open Source License

private static List<String> invalidAdviceRelationsip(IRelationship rel, AJProjectModelFacade model) {
    List<String> problems = new ArrayList<String>();
    if (rel.getKind() == IRelationship.Kind.ADVICE || rel.getKind() == IRelationship.Kind.ADVICE_AFTER
            || rel.getKind() == IRelationship.Kind.ADVICE_AFTERRETURNING
            || rel.getKind() == IRelationship.Kind.ADVICE_AFTERTHROWING
            || rel.getKind() == IRelationship.Kind.ADVICE_BEFORE
            || rel.getKind() == IRelationship.Kind.ADVICE_AROUND) {

        IJavaElement elt = model.programElementToJavaElement(rel.getSourceHandle());
        if (!elt.exists()) {
            problems.add("Java Element does not exist: " + rel.getSourceHandle()
                    + "\n\tIt is the source relationship of " + toRelString(rel)
                    + "\n\tThis may not actually be a problem if compiling broken code or advising static initializers.");
        }//from   w ww  . jav a  2 s.  c  om
        if (elt.getElementType() == IJavaElement.COMPILATION_UNIT
                || elt.getElementType() == IJavaElement.CLASS_FILE) {
            problems.add(
                    "Java Element is wrong type (advice relationships should not contain any types or compilation units): "
                            + rel.getSourceHandle() + "\n\tIt is the source relationship of "
                            + toRelString(rel));
        }

        for (Iterator<String> targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
            String target = targetIter.next();
            elt = model.programElementToJavaElement(target);
            if (!elt.exists()) {
                problems.add("Java Element does not exist: " + target + "\n\tIt is the source relationship of "
                        + toRelString(rel)
                        + "\n\tThis may not actually be a problem if compiling broken code or advising static initializers.");
            }
            if (elt != AJProjectModelFacade.ERROR_JAVA_ELEMENT
                    && (elt.getElementType() == IJavaElement.COMPILATION_UNIT
                            || elt.getElementType() == IJavaElement.CLASS_FILE)) {
                problems.add(
                        "Java Element is wrong type (advice relationships should not contain any types or compilation units): "
                                + target + "\n\tIt is the source relationship of " + toRelString(rel));
            }
        }

    }
    return problems;
}

From source file:org.eclipse.ajdt.core.model.AJModelChecker.java

License:Open Source License

private static List<String> itdsNotOnType(IRelationship rel, AJProjectModelFacade model) {
    List<String> problems = new ArrayList<String>();
    if (rel.getKind() == IRelationship.Kind.DECLARE_INTER_TYPE) {

        IJavaElement elt = model.programElementToJavaElement(rel.getSourceHandle());
        if (!elt.exists()) {
            problems.add("Java Element does not exist: " + rel.getSourceHandle()
                    + "\n\tIt is the source relationship of " + toRelString(rel)
                    + "\n\tThis may not actually be a problem if compiling broken code.");
        }// w  ww .  j  ava2 s .co m
        if (elt != AJProjectModelFacade.ERROR_JAVA_ELEMENT
                && (elt.getElementType() == IJavaElement.FIELD || elt.getElementType() == IJavaElement.METHOD
                        || elt.getElementType() == IJavaElement.LOCAL_VARIABLE
                        || elt.getElementType() == IJavaElement.INITIALIZER
                        || elt.getElementType() == IJavaElement.COMPILATION_UNIT
                        || elt.getElementType() == IJavaElement.CLASS_FILE)
                && !(elt instanceof IntertypeElement || elt instanceof DeclareElement)) {
            problems.add(
                    "Java Element is wrong type (ITD relationships should only contain types and intertype elements): "
                            + rel.getSourceHandle() + "\n\tIt is the source relationship of "
                            + toRelString(rel));
        }

        for (Iterator<String> targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
            String target = targetIter.next();
            elt = model.programElementToJavaElement(target);
            if (!elt.exists()) {
                problems.add("Java Element does not exist: " + target + "\n\tIt is the source relationship of "
                        + toRelString(rel)
                        + "\n\tThis may not actually be a problem if compiling broken code.");
            }
            if (elt != AJProjectModelFacade.ERROR_JAVA_ELEMENT
                    && (elt.getElementType() == IJavaElement.FIELD
                            || elt.getElementType() == IJavaElement.METHOD
                            || elt.getElementType() == IJavaElement.LOCAL_VARIABLE
                            || elt.getElementType() == IJavaElement.INITIALIZER
                            || elt.getElementType() == IJavaElement.COMPILATION_UNIT
                            || elt.getElementType() == IJavaElement.CLASS_FILE)
                    && !(elt instanceof IntertypeElement || elt instanceof DeclareElement)) {
                problems.add(
                        "Java Element is wrong type (ITD relationships should only contain types and intertype elements): "
                                + target + "\n\tIt is the source relationship of " + toRelString(rel));
            }
        }

    }
    return problems;
}

From source file:org.eclipse.ajdt.core.model.AJProjectModelFacade.java

License:Open Source License

/**
 * Find the java element corresponding to the handle.  We know that it exists in 
 * as a class file in a binary folder, but it may actually be
 * a source file in a different project/*  w w  w.  j  a va 2s.c  o  m*/
 */
private IJavaElement findElementInBinaryFolder(HandleInfo handleInfo, IClassFile classFile)
        throws JavaModelException {
    IJavaElement candidate = classFile;
    // we have a class file that is not in a jar.
    // can we find this as a source file in some project?
    IPath path = classFile.getPath();
    IJavaProject otherProject = JavaCore.create(project).getJavaModel().getJavaProject(path.segment(0));
    ITypeRoot typeRoot = classFile;
    if (otherProject.exists()) {
        IType type = otherProject.findType(handleInfo.sourceTypeQualName());
        typeRoot = type.getTypeRoot();
        if (typeRoot instanceof ICompilationUnit) {
            AJCompilationUnit newUnit = CompilationUnitTools
                    .convertToAJCompilationUnit((ICompilationUnit) typeRoot);
            typeRoot = newUnit != null ? newUnit : typeRoot;
        }
    }

    if (handleInfo.isType) {
        switch (typeRoot.getElementType()) {
        case IJavaElement.CLASS_FILE:
            candidate = ((IClassFile) typeRoot).getType();
            break;
        case IJavaElement.COMPILATION_UNIT:
            candidate = CompilationUnitTools.findType((ICompilationUnit) typeRoot,
                    classFile.getType().getElementName(), true);
            break;

        default:
            // shouldn't happen
            break;
        }
    } else if (!handleInfo.isFile && !handleInfo.isType) {
        // program element will exist only if coming from aspect path
        IProgramElement ipe = getProgramElement(handleInfo.origAJHandle);
        if (ipe != IHierarchy.NO_STRUCTURE) {
            candidate = typeRoot.getElementAt(offsetFromLine(typeRoot, ipe.getSourceLocation()));
        } else {
            String newHandle = typeRoot.getHandleIdentifier() + handleInfo.restHandle;
            candidate = AspectJCore.create(newHandle);
        }
    }
    return candidate;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitStructureRequestor.java

License:Open Source License

/**
 * use {@link #acceptPackage(ImportReference)} instead
 * @deprecated/*from  w  w  w . j av  a  2s  .c o m*/
 */
public void acceptPackage(int declarationStart, int declarationEnd, char[] name) {

    // AJDT 1.6 ADE---copied from CompilationUnitStructureProvider.acceptPackage(ImportReference)
    JavaElementInfo parentInfo = (JavaElementInfo) this.infoStack.peek();
    JavaElement parentHandle = (JavaElement) this.handleStack.peek();
    PackageDeclaration handle = null;

    if (parentHandle.getElementType() == IJavaElement.COMPILATION_UNIT) {
        handle = createPackageDeclaration(parentHandle, new String(name));
    } else {
        Assert.isTrue(false); // Should not happen
    }
    resolveDuplicates(handle);

    AJAnnotatableInfo info = new AJAnnotatableInfo();
    info.setSourceRangeStart(declarationStart);
    info.setSourceRangeEnd(declarationEnd);

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

From source file:org.eclipse.ajdt.internal.ui.ajdocexport.AJdocOptionsManager.java

License:Open Source License

private IJavaElement getSelectableJavaElement(Object obj) throws JavaModelException {
    IJavaElement je = null;/* w w  w. j  a  va2  s.  co  m*/
    if (obj instanceof IAdaptable) {
        je = (IJavaElement) ((IAdaptable) obj).getAdapter(IJavaElement.class);
    }

    if (je != null) {
        switch (je.getElementType()) {
        case IJavaElement.JAVA_MODEL:
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.CLASS_FILE:
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            if (containsCompilationUnits((IPackageFragmentRoot) je)) {
                return je;
            }
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            if (containsCompilationUnits((IPackageFragment) je)) {
                return je;
            }
            break;
        default:
            ICompilationUnit cu = (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null) {
                return cu;
            }
        }
        IJavaProject project = je.getJavaProject();
        if (isValidProject(project))
            return project;
    }

    return null;
}