Example usage for org.eclipse.jdt.core.dom CompilationUnit getTypeRoot

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getTypeRoot

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom CompilationUnit getTypeRoot.

Prototype

public ITypeRoot getTypeRoot() 

Source Link

Document

The Java type root (a org.eclipse.jdt.core.ICompilationUnit compilation unit or a org.eclipse.jdt.core.IClassFile class file ) this compilation unit was created from, or null if it was not created from a Java type root.

Usage

From source file:com.wuetherich.osgi.ds.annotations.internal.builder.ComponentDescription.java

License:Open Source License

/**
 * <p>// w w w.java  2 s .c o m
 * Creates a new instance of type {@link ComponentDescription}.
 * </p>
 * 
 * @param typeDeclaration
 */
public ComponentDescription(TypeDeclaration typeDeclaration) {
    Assert.isNotNull(typeDeclaration);

    //
    try {
        CompilationUnit compilationUnit = (CompilationUnit) typeDeclaration.getParent();
        _sourceFile = compilationUnit.getTypeRoot().getCorrespondingResource().getProjectRelativePath()
                .toPortableString();
    } catch (JavaModelException e) {
        //
        // TODO: LOG
    }

    this._typeDeclaration = typeDeclaration;
    _tcomponent = new Tcomponent();
    _problems = new LinkedList<DsAnnotationProblem>();
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.CompilationUnit node) {
    CompilationUnit element = (CompilationUnit) this.binding.get(node);
    this.jdtModel.getCompilationUnits().add(element);
    this.initializeNode(element, node);

    if (node.getTypeRoot() != null) {
        element.setName(node.getTypeRoot().getElementName());
        element.setOriginalFilePath(node.getTypeRoot().getPath().toString());
    } else {/*from  w ww  .j  a v a  2 s .c  o  m*/
        element.setProxy(true);
    }

    PackageDeclaration packageDeclaration = (PackageDeclaration) this.binding.get(node.getPackage());
    element.setPackage(packageDeclaration);

    for (Object importNode : node.imports()) {
        ImportDeclaration importDeclaration = (ImportDeclaration) this.binding.get(importNode);
        element.getImports().add(importDeclaration);
    }

    for (Object typeNode : node.types()) {
        AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) this.binding.get(typeNode);
        element.getTypes().add(typeDeclaration);
    }

}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

private String findRecommendedLineSeparator(CompilationUnit astRoot) {
    try {/* www .  j  a v  a 2s  .  c o m*/
        return astRoot.getTypeRoot().findRecommendedLineSeparator();
    } catch (JavaModelException e) {
        throw new UnhandledException(astRoot, e);
    }
}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Returns the file name where the node comes from, or "FakeClass.java" if this is a fake node.
 *
 * @param node the node//  w  w w . j av a2  s  . c  o  m
 * @return the file name where the node comes from, or "FakeClass.java" if this is a fake node.
 */
public static String getFileName(ASTNode node) {
    if (node.getRoot() instanceof CompilationUnit) {
        CompilationUnit cu = (CompilationUnit) node.getRoot();
        if (cu.getTypeRoot() != null) { // added for unit tests
            return cu.getTypeRoot().getElementName();
        }
        return "FakeClass.java";
    }
    return null;
}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Returns a string suitable for identifying a location in the source.
 *
 * @param node the node from which to retrieve the source location
 * @return a string suitable for identifying a location in the source
 *//*from   ww  w.  j  a v  a2s .com*/
public static String getSourceLocation(ASTNode node) {
    final ASTNode root = node != null ? node.getRoot() : null;
    if (root instanceof CompilationUnit) {
        final CompilationUnit cu = (CompilationUnit) root;
        final int position = node.getStartPosition();
        final int line = cu.getLineNumber(position);
        final int column = cu.getColumnNumber(position) + 1;
        if (cu.getTypeRoot() != null) {
            return cu.getTypeRoot().getElementName() + ":" + line + ":" + column;
        }
        // it was not created from a file
        return line + ":" + column;
    }
    return "";
}

From source file:org.eclipse.emf.texo.generator.ImportReferencesCollector.java

License:Open Source License

private static boolean processJavadocComments(final CompilationUnit astRoot) {
    // don't visit Javadoc for 'package-info' (bug 216432)
    if (astRoot != null && astRoot.getTypeRoot() != null) {
        return !"package-info.java".equals(astRoot.getTypeRoot().getElementName()); //$NON-NLS-1$
    }//from   www  .j  a  v  a  2s.  c o m
    return true;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.CompilationUnit node) {
    CompilationUnit element = (CompilationUnit) this.binding.get(node);

    // if any type of this cu already exists in the model, we don't
    // visit this cu
    if (this.isAlreadyVisited) {
        return;/*from   w  w w .j  a v a2s . c om*/
    }

    this.jdtModel.getCompilationUnits().add(element);
    ITypeRoot rootType = node.getTypeRoot();

    if (rootType != null) {
        if (rootType instanceof IClassFile) {
            // type comes from a .class file
            ClassFile classFile = this.factory.createClassFile();
            classFile.setName(rootType.getElementName());
            classFile.setAttachedSource(element);
            classFile.setOriginalFilePath(this.currentFilePath);

            Archive ar = LibraryReader.getArchive((IClassFile) rootType, this.factory, this.jdtModel);
            if (ar == null) {
                this.jdtModel.getClassFiles().add(classFile);
            } else {
                ar.getClassFiles().add(classFile);
            }
        } else if (rootType instanceof ICompilationUnit) {
            // type comes a .java file
            IPath absolutePath = null;
            try {
                absolutePath = rootType.getCorrespondingResource().getLocation();
            } catch (JavaModelException e) {
                absolutePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation()
                        .append(rootType.getPath());
            }
            element.setOriginalFilePath(absolutePath.toOSString());
        } else {
            element.setOriginalFilePath(""); //$NON-NLS-1$
        }
        element.setName(rootType.getElementName());
    } else {
        element.setProxy(true);
    }

    Package packageDeclaration = (Package) this.binding.get(node.getPackage());
    element.setPackage(packageDeclaration);

    for (Object importNode : node.imports()) {
        ImportDeclaration importDeclaration = (ImportDeclaration) this.binding.get(importNode);
        element.getImports().add(importDeclaration);
    }

    for (Object typeNode : node.types()) {
        AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) this.binding.get(typeNode);
        element.getTypes().add(typeDeclaration);
    }

    try {
        // accessing BlockComment and LineComment
        // (https://bugs.eclipse.org/bugs/show_bug.cgi?format=multiple&id=84528)
        List<?> comments = node.getCommentList();
        for (Object name : comments) {
            org.eclipse.jdt.core.dom.Comment aComment = (org.eclipse.jdt.core.dom.Comment) name;

            Comment commentElement = null;
            if (aComment.isLineComment()) {
                commentElement = this.factory.createLineComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isBlockComment()) {
                commentElement = this.factory.createBlockComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isDocComment()) {
                // one javadoc node (and its tag elements) should have been
                // already visited

                commentElement = (Javadoc) this.binding.get(aComment);
                if (commentElement == null) { // happen if more than one javadoc
                    // for a node
                    commentElement = this.factory.createJavadoc();
                    initializeNode(commentElement, aComment);
                }
                commentElement.setContent(aComment.toString());
            }

            getCommentsBinding().put(aComment, commentElement);
            // initialisation of element CompilationUnit
            element.getCommentList().add(commentElement);
        }

        CommentsManager.resolveCommentPositions(this);
    } catch (StringIndexOutOfBoundsException e) {
        // IGNORE hub, sam, markus
    }
}

From source file:org.eclipse.recommenders.codesearch.rcp.index.indexer.ResourcePathIndexer.java

License:Open Source License

public static File getFile(final CompilationUnit cu) {
    final ITypeRoot root = cu.getTypeRoot();
    if (root == null) {
        // this is a special treatment for cus created from source code w/o
        // a IClassFile or ICompilationUnit
        return ensureIsNotNull((File) cu.getProperty("location"));
    }//from  w  w w  .  j  av a2  s . c o m
    return getFile(root);
}

From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.ImportReferencesCollector.java

License:Open Source License

private static boolean processJavadocComments(CompilationUnit astRoot) {
    // don't visit Javadoc for 'package-info' (bug 216432)
    if (astRoot != null && astRoot.getTypeRoot() != null) {
        return !"package-info.java".equals(astRoot.getTypeRoot().getElementName()); //$NON-NLS-1$
    }/*from   w  ww  .  j a v  a  2 s  .c  o m*/
    return true;
}

From source file:org.hawkinssoftware.rns.analysis.compile.util.RNSBuildAnalyzerUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static ITypeBinding getTypeBinding(CompilationUnit ast) {
    for (AbstractTypeDeclaration type : (Iterable<AbstractTypeDeclaration>) ast.types()) {
        if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
            return type.resolveBinding();
        }//from   w ww.  j  a v a  2s .  c  o m
    }
    throw new IllegalArgumentException(
            "Could not find the top-level type for ast " + ast.getTypeRoot().getElementName());
}