Java Code Examples for com.sun.source.util.TreePath#getCompilationUnit()

The following examples show how to use com.sun.source.util.TreePath#getCompilationUnit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProgramElementDocImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    if (treePath != null) {
        tree = (JCTree) treePath.getLeaf();
        lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
    }
}
 
Example 2
Source File: MoveMembersTransformer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean changeIfMatch(TreePath currentPath, Tree node, final Element target) throws IllegalArgumentException {
    Element el = workingCopy.getTrees().getElement(currentPath);
    if (el == null) {
        return false;
    }
    TreePathHandle elementBeingMoved = isElementBeingMoved(el);
    if (elementBeingMoved != null) {

        final FileObject folder = targetHandle.getFileObject().getParent();
        final CompilationUnitTree compilationUnit = currentPath.getCompilationUnit();
        checkForUsagesOutsideOfPackage(folder, compilationUnit, elementBeingMoved);
        checkForUsagesOutsideOfType(target, currentPath, elementBeingMoved);

        if (node instanceof MethodInvocationTree) {
            if (!delegate) {
                changeMethodInvocation((ExecutableElement) el, (MethodInvocationTree) node, currentPath, target);
            }
        } else if (node instanceof IdentifierTree) {
            changeIdentifier(el, (IdentifierTree) node, currentPath, target);
        } else if (node instanceof MemberSelectTree) {
            changeMemberSelect(el, (MemberSelectTree) node, currentPath, target);
        } else if (node.getKind() == Tree.Kind.MEMBER_REFERENCE) {
            changeMemberRefer(el, (MemberReferenceTree) node, currentPath, target);

        }
        return true;
    } else {
        return false;
    }
}
 
Example 3
Source File: ClassSymbols.java    From manifold with Apache License 2.0 5 votes vote down vote up
private Pair<Symbol.ClassSymbol, JCTree.JCCompilationUnit> getClassSymbol( Context ctx, Object moduleCtx, String fqn )
  {
    Symbol.ClassSymbol typeElement = IDynamicJdk.instance().getTypeElement( ctx, moduleCtx, fqn );
    if( typeElement == null )
    {
      // For the case where the class is generated from a type manifold esp. from a IExtensionClassProducer
      return getClassSymbolForProducedClass( fqn, new BasicJavacTask[1] );

//## want this instead, but the typeElement is not complete in this case, investigate this
//      if( JavacPlugin.instance() != null )
//      {
//        typeElement = IDynamicJdk.instance().getTypeElement( JavacPlugin.instance().getContext(), moduleCtx, fqn );
//        typeElement.complete();
//      }
    }

    JavacTrees trees = JavacTrees.instance( ctx );
    TreePath path = trees.getPath( typeElement );
    if( path != null )
    {
      return new Pair<>( typeElement, (JCTree.JCCompilationUnit)path.getCompilationUnit() );
    }
    else
    {
      // TreePath is only applicable to a source file;
      // if fqn is not a source file, there is no compilation unit available
      return new Pair<>( typeElement, null );
    }
  }
 
Example 4
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public DocCommentTree getDocCommentTree(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    Tree leaf = path.getLeaf();
    if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.getCommentTree((JCTree) leaf);
        }
    }
    return null;
}
 
Example 5
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public String getDocComment(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    Tree leaf = path.getLeaf();
    if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.getCommentText((JCTree) leaf);
        }
    }
    return null;
}
 
Example 6
Source File: JavacTrees.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public String getDocComment(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    if (t instanceof JCCompilationUnit) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.get(path.getLeaf());
        }
    }
    return null;
}
 
Example 7
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public long getLineNumber(Element e) {
    TreePath path = getTreePath(e);
    if (path == null) { // maybe null if synthesized
        TypeElement encl = getEnclosingTypeElement(e);
        path = getTreePath(encl);
    }
    CompilationUnitTree cu = path.getCompilationUnit();
    LineMap lineMap = cu.getLineMap();
    DocSourcePositions spos = docTrees.getSourcePositions();
    long pos = spos.getStartPosition(cu, path.getLeaf());
    return lineMap.getLineNumber(pos);
}
 
Example 8
Source File: ClassSymbols.java    From manifold with Apache License 2.0 5 votes vote down vote up
private Pair<Symbol.ClassSymbol, JCTree.JCCompilationUnit> getClassSymbolForProducedClass( String fqn, BasicJavacTask[] task )
{
  StringWriter errors = new StringWriter();

  // need javac with ManifoldJavaFileManager because the produced class must come from manifold
  task[0] = getJavacTask_ManFileMgr();

  Symbol.ClassSymbol e = IDynamicJdk.instance().getTypeElement( task[0].getContext(), null, fqn );

  if( e != null && e.getSimpleName().contentEquals( ManClassUtil.getShortClassName( fqn ) ) )
  {
    JavacTrees trees = JavacTrees.instance( task[0].getContext() );
    TreePath path = trees.getPath( e );
    if( path != null )
    {
      return new Pair<>( e, (JCTree.JCCompilationUnit)path.getCompilationUnit() );
    }
    else
    {
      // TreePath is only applicable to a source file;
      // if fqn is not a source file, there is no compilation unit available
      return new Pair<>( e, null );
    }
  }

  StringBuffer errorText = errors.getBuffer();
  if( errorText.length() > 0 )
  {
    throw new RuntimeException( "Compile errors:\n" + errorText );
  }

  return null;
}
 
Example 9
Source File: PackageDocImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example 10
Source File: PackageDocImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example 11
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public DocCommentTree getDocCommentTree(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    Tree leaf = path.getLeaf();
    if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.getCommentTree((JCTree) leaf);
        }
    }
    return null;
}
 
Example 12
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public String getDocComment(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    Tree leaf = path.getLeaf();
    if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.getCommentText((JCTree) leaf);
        }
    }
    return null;
}
 
Example 13
Source File: DocImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static String getCommentText(TreePath p) {
    if (p == null)
        return null;

    JCCompilationUnit topLevel = (JCCompilationUnit) p.getCompilationUnit();
    JCTree tree = (JCTree) p.getLeaf();
    return topLevel.docComments.getCommentText(tree);
}
 
Example 14
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public DocCommentTree getDocCommentTree(TreePath path) {
    CompilationUnitTree t = path.getCompilationUnit();
    Tree leaf = path.getLeaf();
    if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
        JCCompilationUnit cu = (JCCompilationUnit) t;
        if (cu.docComments != null) {
            return cu.docComments.getCommentTree((JCTree) leaf);
        }
    }
    return null;
}
 
Example 15
Source File: PackageDocImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example 16
Source File: ProgramElementDocImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    if (treePath != null) {
        tree = (JCTree) treePath.getLeaf();
        lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
    }
}
 
Example 17
Source File: ProgramElementDocImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    if (treePath != null) {
        tree = (JCTree) treePath.getLeaf();
        lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
    }
}
 
Example 18
Source File: PackageDocImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example 19
Source File: ProgramElementDocImpl.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
void setTreePath(TreePath treePath) {
    super.setTreePath(treePath);
    this.tree = (JCTree) treePath.getLeaf();
    this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
}
 
Example 20
Source File: Checker.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public Void scan(DocCommentTree tree, TreePath p) {
    env.setCurrent(p, tree);

    boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();

    if (p.getLeaf() == p.getCompilationUnit()) {
        // If p points to a compilation unit, the implied declaration is the
        // package declaration (if any) for the compilation unit.
        // Handle this case specially, because doc comments are only
        // expected in package-info files.
        JavaFileObject fo = p.getCompilationUnit().getSourceFile();
        boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
        if (tree == null) {
            if (isPkgInfo)
                reportMissing("dc.missing.comment");
            return null;
        } else {
            if (!isPkgInfo)
                reportReference("dc.unexpected.comment");
        }
    } else {
        if (tree == null) {
            if (!isSynthetic() && !isOverridingMethod)
                reportMissing("dc.missing.comment");
            return null;
        }
    }

    tagStack.clear();
    currHeaderTag = null;

    foundParams.clear();
    foundThrows.clear();
    foundInheritDoc = false;
    foundReturn = false;

    scan(new DocTreePath(p, tree), null);

    if (!isOverridingMethod) {
        switch (env.currElement.getKind()) {
            case METHOD:
            case CONSTRUCTOR: {
                ExecutableElement ee = (ExecutableElement) env.currElement;
                checkParamsDocumented(ee.getTypeParameters());
                checkParamsDocumented(ee.getParameters());
                switch (ee.getReturnType().getKind()) {
                    case VOID:
                    case NONE:
                        break;
                    default:
                        if (!foundReturn
                                && !foundInheritDoc
                                && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
                            reportMissing("dc.missing.return");
                        }
                }
                checkThrowsDocumented(ee.getThrownTypes());
            }
        }
    }

    return null;
}