本文整理汇总了Java中com.sun.source.tree.CompilationUnitTree类的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnitTree类的具体用法?Java CompilationUnitTree怎么用?Java CompilationUnitTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompilationUnitTree类属于com.sun.source.tree包,在下文中一共展示了CompilationUnitTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findTopClasses
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
/**
*
* @return list of top classes, or an empty list of none were found
*/
static List<ClassTree> findTopClasses(
CompilationUnitTree compilationUnit,
TreeUtilities treeUtils) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<ClassTree>emptyList();
}
List<ClassTree> result = new ArrayList<ClassTree>(typeDecls.size());
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
ClassTree clsTree = (ClassTree) typeDecl;
if (isTestable(clsTree, treeUtils)) {
result.add(clsTree);
}
}
}
return result;
}
示例2: run
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
for (Tree typeDeclaration : cut.getTypeDecls()){
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
ClassTree clazz = (ClassTree) typeDeclaration;
EntityManagerGenerationStrategySupport strategy =
(EntityManagerGenerationStrategySupport) getStrategy(workingCopy, make, clazz, new GenerationOptions());
doAsserts(strategy);
} else {
fail("No class found"); // should not happen
}
}
}
示例3: run
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
@Override
public void run(WorkingCopy compiler) throws IOException {
compiler.toPhase(JavaSource.Phase.RESOLVED);
CompilationUnitTree cu = compiler.getCompilationUnit();
if (cu == null) {
ErrorManager.getDefault().log(ErrorManager.ERROR, "compiler.getCompilationUnit() is null " + compiler); // NOI18N
return;
}
FileObject file = compiler.getFileObject();
FileObject folder = file.getParent();
final String newName;
if(folder.getFileObject(oldName, file.getExt()) != null) {
newName = file.getName();
} else {
newName = oldName;
}
CopyTransformer findVisitor = new CopyTransformer(compiler, oldName, newName, insertImport, oldPackage);
findVisitor.scan(compiler.getCompilationUnit(), null);
}
示例4: findTopClassElems
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
/**
*
* @return list of {@code Element}s representing top classes,
* or an empty list of none were found
*/
private static List<TypeElement> findTopClassElems(
CompilationInfo compInfo,
CompilationUnitTree compilationUnit,
Filter filter) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<TypeElement>emptyList();
}
List<TypeElement> result = new ArrayList<TypeElement>(typeDecls.size());
Trees trees = compInfo.getTrees();
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
Element element = trees.getElement(
new TreePath(new TreePath(compilationUnit), typeDecl));
TypeElement typeElement = (TypeElement) element;
if (filter.passes(typeElement, compInfo)) {
result.add(typeElement);
}
}
}
return result;
}
示例5: testMoveFirst
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public void testMoveFirst() throws IOException, FileStateInvalidException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
ImportTree oneImport = imports.remove(0);
imports.add(3, oneImport);
CompilationUnitTree unit = make.CompilationUnit(
cut.getPackageName(),
imports,
cut.getTypeDecls(),
cut.getSourceFile()
);
workingCopy.rewrite(cut, unit);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertFiles("testMoveFirst_ImportFormatTest.pass");
}
示例6: performRewrite
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy copy = ctx.getWorkingCopy();
TreePath tp = ctx.getPath();
CompilationUnitTree cut = copy.getCompilationUnit();
TreeMaker make = copy.getTreeMaker();
CompilationUnitTree newCut = cut;
for (TreePathHandle tph : tphList) {
TreePath path = tph.resolve(copy);
if ( path != null && path.getLeaf() instanceof ImportTree) {
newCut = make.removeCompUnitImport(newCut, (ImportTree)path.getLeaf());
}
}
copy.rewrite(cut, newCut);
}
示例7: read
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
/**
* Read a file.
* @param file the file to be read
* @return the tree for the content of the file
* @throws IOException if any IO errors occur
* @throws TreePosTest.ParseException if any errors occur while parsing the file
*/
JCCompilationUnit read(File file) throws IOException, ParseException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
JavacTask task = tool.getTask(pw, fm, r, List.of("-proc:none"), null, files);
Iterable<? extends CompilationUnitTree> trees = task.parse();
pw.flush();
if (r.errors > 0)
throw new ParseException(sw.toString());
Iterator<? extends CompilationUnitTree> iter = trees.iterator();
if (!iter.hasNext())
throw new Error("no trees found");
JCCompilationUnit t = (JCCompilationUnit) iter.next();
if (iter.hasNext())
throw new Error("too many trees found");
return t;
}
示例8: testRemoveInnerImport
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public void testRemoveInnerImport() throws IOException, FileStateInvalidException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
imports.remove(1);
CompilationUnitTree unit = make.CompilationUnit(
cut.getPackageName(),
imports,
cut.getTypeDecls(),
cut.getSourceFile()
);
workingCopy.rewrite(cut, unit);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertFiles("testRemoveInnerImport_ImportFormatTest.pass");
}
示例9: getChangedTree
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
/**
* Returns tree which was reparsed by an incremental reparse.
* When the source file wasn't parsed yet or the parse was a full parse
* this method returns null.
* <p class="nonnormative">
* Currently the leaf tree is a MethodTree but this may change in the future.
* Client of this method is responsible to check the corresponding TreeKind
* to find out if it may perform on the changed subtree or it needs to
* reprocess the whole tree.
* </p>
* @return {@link TreePath} or null
* @since 0.31
*/
public @CheckForNull @CheckReturnValue TreePath getChangedTree () {
checkConfinement();
if (JavaSource.Phase.PARSED.compareTo (impl.getPhase())>0) {
return null;
}
final Pair<DocPositionRegion,MethodTree> changedTree = impl.getChangedTree();
if (changedTree == null) {
return null;
}
final CompilationUnitTree cu = impl.getCompilationUnit();
if (cu == null) {
return null;
}
return TreePath.getPath(cu, changedTree.second());
}
示例10: addImport
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
/** Creates new Class from package
* @param packageName destination
* @param className name
* @throws Exception
* @return
*/
public static void addImport(JavaSource js,final String importText,final boolean isStatic) throws IOException {
CancellableTask task = new CancellableTask<WorkingCopy>() {
public void cancel() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
CompilationUnitTree copy = make.addCompUnitImport(cut,make.Import(make.Identifier(importText), isStatic));
workingCopy.rewrite(cut, copy);
}
};
js.runModificationTask(task).commit();
}
示例11: testWrapAssignment
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public void testWrapAssignment() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
" public void t(AtomicBoolean ab) {\n" +
" new AtomicBoolean();\n" +
" }\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree method = (MethodTree) clazz.getMembers().get(1);
ExpressionStatementTree init = (ExpressionStatementTree) method.getBody().getStatements().get(0);
AssignmentTree bt = make.Assignment(make.Identifier("ab"), init.getExpression());
workingCopy.rewrite(init, make.ExpressionStatement(bt));
}
}, FmtOptions.wrapAssignOps, WrapStyle.WRAP_IF_LONG.name());
}
示例12: run
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public boolean run(DocletEnvironment root) {
DocTrees trees = root.getDocTrees();
SourcePositions sourcePositions = trees.getSourcePositions();
for (TypeElement klass : ElementFilter.typesIn(root.getIncludedElements())) {
for (ExecutableElement method : getMethods(klass)) {
if (method.getSimpleName().toString().equals("tabbedMethod")) {
TreePath path = trees.getPath(method);
CompilationUnitTree cu = path.getCompilationUnit();
long pos = sourcePositions.getStartPosition(cu, path.getLeaf());
LineMap lineMap = cu.getLineMap();
long columnNumber = lineMap.getColumnNumber(pos);
if (columnNumber == 9) {
System.out.println(columnNumber + ": OK!");
return true;
} else {
System.err.println(columnNumber + ": wrong tab expansion");
return false;
}
}
}
}
return false;
}
示例13: findSource
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName,
String binaryName) throws IOException {
JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
binaryName,
JavaFileObject.Kind.SOURCE);
if (jfo == null)
return null;
List<JavaFileObject> jfos = Arrays.asList(jfo);
JavaFileManager patchFM = moduleName != null
? new PatchModuleFileManager(baseFileManager, jfo, moduleName)
: baseFileManager;
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos);
Iterable<? extends CompilationUnitTree> cuts = task.parse();
task.enter();
return Pair.of(task, cuts.iterator().next());
}
示例14: getAllTree
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
public static List<? extends Tree> getAllTree(JavaSource source) {
final List<? extends Tree>[] allTree = new List[1];
try {
source.runUserActionTask(new AbstractTask<CompilationController>() {
public void run(CompilationController controller) throws IOException {
String className = controller.getFileObject().getName();
CompilationUnitTree cu = controller.getCompilationUnit();
if (cu != null) {
allTree[0] = cu.getTypeDecls();
}
}
}, true);
} catch (IOException ex) {
ex.printStackTrace();
}
return allTree[0];
}
示例15: testOperatorMissingError
import com.sun.source.tree.CompilationUnitTree; //导入依赖的package包/类
@Test
void testOperatorMissingError() throws IOException {
String code = "package test; public class ErrorTest { "
+ "void method() { int x = y z } }";
CompilationUnitTree cut = getCompilationUnitTree(code);
final List<String> values = new ArrayList<>();
final List<String> expectedValues =
new ArrayList<>(Arrays.asList("[z]"));
new TreeScanner<Void, Void>() {
@Override
public Void visitErroneous(ErroneousTree node, Void p) {
values.add(getErroneousTreeValues(node).toString());
return null;
}
}.scan(cut, null);
assertEquals("testOperatorMissingError: The Erroneous tree "
+ "error values: " + values
+ " do not match expected error values: "
+ expectedValues, values, expectedValues);
}