本文整理汇总了Java中com.sun.source.tree.CompilationUnitTree.getImports方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnitTree.getImports方法的具体用法?Java CompilationUnitTree.getImports怎么用?Java CompilationUnitTree.getImports使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.CompilationUnitTree
的用法示例。
在下文中一共展示了CompilationUnitTree.getImports方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createImport
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public static CompilationUnitTree createImport(WorkingCopy wc, CompilationUnitTree modifiedCut, String fq) {
if (modifiedCut == null) {
modifiedCut = wc.getCompilationUnit(); //use committed cut as modifiedCut
}
List<? extends ImportTree> imports = modifiedCut.getImports();
boolean found = false;
for (ImportTree imp : imports) {
if (fq.equals(imp.getQualifiedIdentifier().toString())) {
found = true;
break;
}
}
if (!found) {
TreeMaker make = wc.getTreeMaker();
CompilationUnitTree newCut = make.addCompUnitImport(
modifiedCut,
make.Import(make.Identifier(fq), false)
); //create a newCut from modifiedCut
wc.rewrite(wc.getCompilationUnit(), newCut); //replace committed cut with newCut in change map
return newCut; //return the newCut we just created
}
return modifiedCut; //no newCut created from modifiedCut, so just return modifiedCut
}
示例2: testAddFirstImport
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddFirstImport() throws IOException, FileStateInvalidException {
System.err.println("testAddFirstImport");
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.add(0, make.Import(make.Identifier("java.util.AbstractList"), false));
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("testAddFirstImport_ImportFormatTest.pass");
}
示例3: testAddLastImport
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddLastImport() 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.add(make.Import(make.Identifier("java.io.IOException"), false));
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("testAddLastImport_ImportFormatTest.pass");
}
示例4: 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");
}
示例5: testRemoveFirstImport
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveFirstImport() 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(0);
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("testRemoveFirstImport_ImportFormatTest.pass");
}
示例6: testRemoveLastImport
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveLastImport() throws IOException, FileStateInvalidException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
TreeMaker make = workingCopy.getTreeMaker();
CompilationUnitTree cut = workingCopy.getCompilationUnit();
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("testRemoveLastImport_ImportFormatTest.pass");
}
示例7: testRemoveInside
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveInside() 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(4);
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("testRemoveInside_ImportFormatTest.pass");
}
示例8: 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");
}
示例9: testMoveLast
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testMoveLast() 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(7);
imports.add(1, 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("testMoveLast_ImportFormatTest.pass");
}
示例10: testReplaceLine
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testReplaceLine() 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(4);
imports.add(4, make.Import(make.Identifier("java.util.Collection"), false));
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("testReplaceLine_ImportFormatTest.pass");
}
示例11: main
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null,
fileManager.getJavaFileObjects(thisSrc));
CompilationUnitTree tree = task.parse().iterator().next();
int count = 0;
for (ImportTree importTree : tree.getImports()) {
System.out.println(importTree);
count++;
}
int expected = 7;
if (count != expected)
throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
}
示例12: testAddSeveral
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddSeveral() 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 = (List<ImportTree>) cut.getImports();
imports.add(make.Import(make.Identifier("java.util.List"), false));
imports.add(make.Import(make.Identifier("java.util.Set"), false));
imports.add(make.Import(make.Identifier("javax.swing.CellRendererPane"), false));
imports.add(make.Import(make.Identifier("javax.swing.BorderFactory"), false));
imports.add(make.Import(make.Identifier("javax.swing.ImageIcon"), false));
imports.add(make.Import(make.Identifier("javax.swing.InputVerifier"), false));
imports.add(make.Import(make.Identifier("javax.swing.GrayFilter"), false));
imports.add(make.Import(make.Identifier("javax.swing.JFileChooser"), false));
imports.add(make.Import(make.Identifier("javax.swing.AbstractAction"), false));
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("testAddSeveral_ImportFormatTest.pass");
}
示例13: getAllImportsOfKind
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
private static List<TreePathHandle> getAllImportsOfKind(CompilationInfo ci, ImportHintKind kind) {
//allow only default and samepackage
assert (kind == ImportHintKind.DEFAULT_PACKAGE || kind == ImportHintKind.SAME_PACKAGE);
CompilationUnitTree cut = ci.getCompilationUnit();
TreePath topLevel = new TreePath(cut);
List<TreePathHandle> result = new ArrayList<TreePathHandle>(3);
List<? extends ImportTree> imports = cut.getImports();
for (ImportTree it : imports) {
if (it.isStatic()) {
continue; // XXX
}
if (it.getQualifiedIdentifier() instanceof MemberSelectTree) {
MemberSelectTree ms = (MemberSelectTree) it.getQualifiedIdentifier();
if (kind == ImportHintKind.DEFAULT_PACKAGE) {
if (ms.getExpression().toString().equals(DEFAULT_PACKAGE)) {
result.add(TreePathHandle.create(new TreePath(topLevel, it), ci));
}
}
if (kind == ImportHintKind.SAME_PACKAGE) {
ExpressionTree packageName = cut.getPackageName();
if (packageName != null &&
ms.getExpression().toString().equals(packageName.toString())) {
result.add(TreePathHandle.create(new TreePath(topLevel, it), ci));
}
}
}
}
return result;
}
示例14: checkImports
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
@TriggerTreeKind(Kind.COMPILATION_UNIT)
public static ErrorDescription checkImports(final HintContext context) {
Source source = context.getInfo().getSnapshot().getSource();
ModificationResult result = null;
try {
result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() {
@Override
public void run(ResultIterator resultIterator) throws Exception {
WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult());
copy.toPhase(Phase.RESOLVED);
doOrganizeImports(copy, context.isBulkMode());
}
});
} catch (ParseException ex) {
Exceptions.printStackTrace(ex);
}
List<? extends Difference> diffs = result != null ? result.getDifferences(source.getFileObject()) : null;
if (diffs != null && !diffs.isEmpty()) {
Fix fix = new OrganizeImportsFix(context.getInfo(), context.getPath(), context.isBulkMode()).toEditorFix();
SourcePositions sp = context.getInfo().getTrees().getSourcePositions();
int offset = diffs.get(0).getStartPosition().getOffset();
CompilationUnitTree cu = context.getInfo().getCompilationUnit();
for (ImportTree imp : cu.getImports()) {
if (sp.getEndPosition(cu, imp) >= offset)
return ErrorDescriptionFactory.forTree(context, imp, NbBundle.getMessage(OrganizeImports.class, "MSG_OragnizeImports"), fix); //NOI18N
}
return ErrorDescriptionFactory.forTree(context, context.getInfo().getCompilationUnit().getImports().get(0), NbBundle.getMessage(OrganizeImports.class, "MSG_OragnizeImports"), fix); //NOI18N
}
return null;
}
示例15: getImports
import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public static Collection<String> getImports(CompilationController controller) {
Set<String> imports = new HashSet<String>();
CompilationUnitTree cu = controller.getCompilationUnit();
if (cu != null) {
List<? extends ImportTree> importTrees = cu.getImports();
for (ImportTree importTree : importTrees) {
imports.add(importTree.getQualifiedIdentifier().toString());
}
}
return imports;
}