本文整理了Java中com.github.javaparser.ast.CompilationUnit.getImports()
方法的一些代码示例,展示了CompilationUnit.getImports()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CompilationUnit.getImports()
方法的具体详情如下:
包路径:com.github.javaparser.ast.CompilationUnit
类名称:CompilationUnit
方法名:getImports
暂无
代码示例来源:origin: JCTools/JCTools
private static void organiseImports(CompilationUnit cu) {
List<ImportDeclaration> importDecls = new ArrayList<>();
for (ImportDeclaration importDeclaration : cu.getImports()) {
if (importDeclaration.getNameAsString().startsWith("org.jctools.util.Unsafe")) {
continue;
}
importDecls.add(importDeclaration);
}
cu.getImports().clear();
for (ImportDeclaration importDecl : importDecls) {
cu.addImport(importDecl);
}
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicLongFieldUpdater"));
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicReferenceArray"));
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicLongArray"));
}
代码示例来源:origin: JCTools/JCTools
private static void organiseImports(CompilationUnit cu) {
List<ImportDeclaration> importDecls = new ArrayList<>();
for (ImportDeclaration importDeclaration : cu.getImports()) {
String name = importDeclaration.getNameAsString();
if (name.startsWith("org.jctools.util.Unsafe")) {
cu.getImports().clear();
for (ImportDeclaration importDecl : importDecls) {
cu.addImport(importDecl);
代码示例来源:origin: org.apache.uima/uimaj-v3migration-jcas
private void removeImport(String s) {
Iterator<ImportDeclaration> it = cu.getImports().iterator();
while (it.hasNext()) {
ImportDeclaration impDcl = it.next();
if (impDcl.getNameAsString().equals(s)) {
it.remove();
break;
}
}
}
代码示例来源:origin: ImmobilienScout24/deadcode4j
@Nonnull
private Iterable<String> getStaticAsteriskImports() {
return transform(filter(emptyIfNull(compilationUnit.getImports()),
and(isAsterisk(), isStatic())), toImportedType());
}
代码示例来源:origin: ImmobilienScout24/deadcode4j
@Nullable
@SuppressWarnings("unchecked")
private String getStaticImport(String referenceName) {
return getOnlyElement(transform(filter(emptyIfNull(compilationUnit.getImports()),
and(refersTo(referenceName), not(isAsterisk()), isStatic())), toImportedType()), null);
}
代码示例来源:origin: ImmobilienScout24/deadcode4j
@Nonnull
@Override
protected Iterable<String> calculatePrefixes(@Nonnull Qualifier<?> topQualifier) {
ArrayList<String> asteriskImports = newArrayList();
CompilationUnit compilationUnit = Nodes.getCompilationUnit(topQualifier.getNode());
for (ImportDeclaration importDeclaration :
emptyIfNull(compilationUnit.getImports()).filter(isAsterisk())) {
StringBuilder buffy = prepend(importDeclaration.getName(), new StringBuilder());
buffy.append(importDeclaration.isStatic() ? '$' : '.');
asteriskImports.add(buffy.toString());
}
return asteriskImports;
}
代码示例来源:origin: com.paypal.butterfly/butterfly-utilities
@SuppressWarnings("PMD.SimplifyBooleanReturns")
private boolean isImported(CompilationUnit compilationUnit, String typeSimpleName) {
if (StringUtils.isBlank(typeSimpleName) || typeSimpleName.contains(".")) {
throw new IllegalArgumentException("Invalid type simple name");
}
// If the specified type is part of the JDK,
// then it won't need need an explicit import statement
if (specifiedTypePackageName.startsWith("java.lang")) {
return true;
}
// Check if the compilation unit has an explicit import declaration whose
// type name matches the specified type simple name
String importClass;
for (ImportDeclaration importDeclaration : compilationUnit.getImports()) {
importClass = importDeclaration.getName().getIdentifier();
if (importClass.equals(typeSimpleName)) {
return true;
}
}
// Check if the annotation is declared
// at the same package where the class is
if (compilationUnit.getPackageDeclaration().get().getNameAsString().equals(specifiedTypePackageName)) {
return true;
}
return false;
}
代码示例来源:origin: ImmobilienScout24/deadcode4j
@Nullable
@Override
protected String calculatePrefix(@Nonnull Qualifier<?> topQualifier) {
String firstQualifier = topQualifier.getFirstQualifier().getName();
CompilationUnit compilationUnit = Nodes.getCompilationUnit(topQualifier.getNode());
ImportDeclaration importDeclaration = getOnlyElement(emptyIfNull(compilationUnit.getImports()).filter(
and(not(isAsterisk()), refersTo(firstQualifier))), null);
if (importDeclaration == null) {
return null;
}
StringBuilder buffy = prepend(importDeclaration.getName(), new StringBuilder());
int beginIndex = buffy.length() - firstQualifier.length();
return beginIndex == 0 ? "" :
buffy.replace(beginIndex - 1, buffy.length(), importDeclaration.isStatic() ? "$" : ".").toString();
}
代码示例来源:origin: AlbertBrand/react-native-android-tablayout
private boolean updateImports() {
List<ImportDeclaration> imports = compilationUnit.getImports();
if (!hasImport(imports, TAB_LAYOUT_PACKAGE_CLASS)) {
imports.add(new ImportDeclaration(new NameExpr(TAB_LAYOUT_PACKAGE_CLASS), false, false));
return true;
}
return false;
}
代码示例来源:origin: AlbertBrand/react-native-android-tablayout
private boolean updateImports() {
List<ImportDeclaration> imports = compilationUnit.getImports();
if (!hasImport(imports, TAB_LAYOUT_PACKAGE_CLASS)) {
imports.add(new ImportDeclaration(new NameExpr(TAB_LAYOUT_PACKAGE_CLASS), false, false));
return true;
}
return false;
}
代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core
@Override
public List<ResolvedFieldDeclaration> fieldsExposedToChild(Node child) {
List<ResolvedFieldDeclaration> res = new LinkedList<>();
// Consider the static imports for static fields
for (ImportDeclaration importDeclaration : wrappedNode.getImports()) {
if (importDeclaration.isStatic()) {
Name typeNameAsNode = importDeclaration.isAsterisk() ? importDeclaration.getName() : importDeclaration.getName().getQualifier().get();
String typeName = typeNameAsNode.asString();
ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(typeName);
res.addAll(typeDeclaration.getAllFields().stream()
.filter(f -> f.isStatic())
.filter(f -> importDeclaration.isAsterisk() || importDeclaration.getName().getIdentifier().equals(f.getName()))
.collect(Collectors.toList()));
}
}
return res;
}
代码示例来源:origin: com.agapsys.libs/scanner-maven-plugin-lib
@Override
public void visit(CompilationUnit n, Object arg) {
currentPackage = n.getPackage() != null ? n.getPackage().getName().toString() : "";
imports = new LinkedList<String>();
for (ImportDeclaration importDec : n.getImports()) {
imports.add(importDec.getName().toString());
}
super.visit(n, arg);
}
代码示例来源:origin: com.agapsys/scanner-maven-plugin-lib
@Override
public void visit(CompilationUnit n, Object arg) {
currentPackage = n.getPackage() != null ? n.getPackage().getName().toString() : "";
imports = new LinkedList<String>();
for (ImportDeclaration importDec : n.getImports()) {
imports.add(importDec.getName().toString());
}
super.visit(n, arg);
}
代码示例来源:origin: org.apache.uima/uimaj-v3migration-jcas
private void addImport(String s) {
cu.getImports().add(new ImportDeclaration(new Name(s), false, false));
}
代码示例来源:origin: beihaifeiwu/dolphin
@Override
public CompilationUnit doMerge(CompilationUnit first, CompilationUnit second) {
CompilationUnit unit = new CompilationUnit();
unit.setPackage(mergeSingle(first.getPackage(), second.getPackage()));
unit.setImports(mergeCollections(first.getImports(), second.getImports()));
unit.setTypes(mergeCollections(first.getTypes(), second.getTypes()));
return unit;
}
代码示例来源:origin: javaparser/javasymbolsolver
@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if(importDecl.isStatic()){
if(importDecl.isAsterisk()){
代码示例来源:origin: com.github.javaparser/java-symbol-solver-core
@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if(importDecl.isStatic()){
if(importDecl.isAsterisk()){
代码示例来源:origin: kawasima/enkan
private Generator configureApplicationFactory(Generator gen, String tableName, EnkanSystem system) {
String path = findApplicationFactoryPath(system);
return gen.writing("app", g -> g.task(new RewriteJavaSourceTask("src/main/java/" + path, cu -> {
String controllerClassName = CaseConverter.pascalCase(tableName) + "Controller";
String pkgName = BasePackageDetector.detect();
cu.getImports().add(
new ImportDeclaration(
ASTHelper.createNameExpr(pkgName + "controller." + controllerClassName),
false, false));
cu.accept(new AppendRoutingVisitor(controllerClassName),
new RoutingDefineContext());
})));
}
代码示例来源:origin: beihaifeiwu/dolphin
public static String mergeContent(CompilationUnit one, CompilationUnit two) throws Exception {
// 包声明不同,返回null
if (!one.getPackage().equals(two.getPackage())) return null;
CompilationUnit cu = new CompilationUnit();
// add package declaration to the compilation unit
PackageDeclaration pd = new PackageDeclaration();
pd.setName(one.getPackage().getName());
cu.setPackage(pd);
// check and merge file comment;
Comment fileComment = mergeSelective(one.getComment(), two.getComment());
cu.setComment(fileComment);
// check and merge imports
List<ImportDeclaration> ids = mergeListNoDuplicate(one.getImports(), two.getImports());
cu.setImports(ids);
// check and merge Types
List<TypeDeclaration> types = mergeTypes(one.getTypes(), two.getTypes());
cu.setTypes(types);
return cu.toString();
}
代码示例来源:origin: javaparser/javasymbolsolver
if (wrappedNode.getImports() != null) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if(importDecl.isStatic()){
if(importDecl.isAsterisk()) {
内容来源于网络,如有侵权,请联系作者删除!