本文整理汇总了Java中com.github.javaparser.ast.CompilationUnit.getImports方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.getImports方法的具体用法?Java CompilationUnit.getImports怎么用?Java CompilationUnit.getImports使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.javaparser.ast.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.getImports方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public void visit(final CompilationUnit n, final Object arg) {
printJavaComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (!isNullOrEmpty(n.getImports())) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (!isNullOrEmpty(n.getTypes())) {
for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
printOrphanCommentsEnding(n);
}
示例2: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public void visit(final CompilationUnit n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例3: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public void visit(final CompilationUnit n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例4: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public Node visit(final CompilationUnit n, final A arg) {
if (n.getPackage() != null) {
n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
}
final List<ImportDeclaration> imports = n.getImports();
if (imports != null) {
for (int i = 0; i < imports.size(); i++) {
imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
}
removeNulls(imports);
}
final List<TypeDeclaration> types = n.getTypes();
if (types != null) {
for (int i = 0; i < types.size(); i++) {
types.set(i, (TypeDeclaration) types.get(i).accept(this, arg));
}
removeNulls(types);
}
return n;
}
示例5: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public void visit(CompilationUnit node, Object arg) {
if (node == null || node.getPackage() == null || node.getPackage().getName() == null) {
return;
}
Log.d(TAG, "visit CompilationUnit, packageName: %s", node.getPackage().getName());
sourceInfo.setPackageName(node.getPackage().getName().toString());
if (node.getImports() != null && node.getImports().size() > 0) {
for (ImportDeclaration importDeclaration : node.getImports()) {
Log.d(TAG, "visit CompilationUnit, import: %s", importDeclaration.getName());
sourceInfo.addImports(importDeclaration.isAsterisk() ? importDeclaration.getName().toString() + ".*" :
importDeclaration.getName().toString());
}
}
super.visit(node, arg);
}
示例6: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public void visit(final CompilationUnit n, final Object arg) {
printJavaComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (n.getTypes() != null) {
for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
printOrphanCommentsEnding(n);
}
示例7: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public void visit(CompilationUnit n, Context arg) {
if (n.getPackageDeclaration().isPresent()) {
String pkg = n.getPackageDeclaration().get().getNameAsString();
arg.pkg = pkg;
arg.wildcardImports.add(pkg.replace('.', '/')+'/'); // TODO: verify ordering
} else {
arg.wildcardImports.add("");
}
arg.wildcardImports.add("java/lang/");
for (ImportDeclaration imp : n.getImports()) {
if (imp.isStatic()) continue;
if (imp.isAsterisk()) {
arg.wildcardImports.add(imp.getNameAsString().replace('.', '/')+'/');
} else {
String name = imp.getNameAsString();
int pos = name.lastIndexOf('.');
if (pos != -1) arg.imports.put(name.substring(pos + 1), name.replace('.', '/'));
}
}
n.getTypes().forEach(p -> p.accept(this, arg));
arg.pkg = null;
arg.imports.clear();
arg.wildcardImports.clear();
}
示例8: isImported
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@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;
}
示例9: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public void visit(final CompilationUnit n, final Object arg) {
printer.printLn("CompilationUnit");
printJavaComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (n.getTypes() != null) {
for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext(); ) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
printOrphanCommentsEnding(n);
}
示例10: visit
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public JCTree visit(final CompilationUnit n, final Object arg) {
// ARG0: List<JCAnnotation> packageAnnotations
List<JCAnnotation> arg0 = List.<JCAnnotation>nil();
// ARG1: JCExpression pid
// Package declaration
JCExpression arg1 = null;
if (n.getPackage() != null) {
arg1 = (JCExpression) n.getPackage().accept(this, arg);
if (n.getPackage().getAnnotations() != null) {
for (final AnnotationExpr packageAnnotation : n.getPackage().getAnnotations()) {
arg0 = arg0.append((JCAnnotation) packageAnnotation.accept(this, arg));
}
}
}
// ARG2 = List<JCTree> defs
// Imports and classes are passed in the same structure
List<JCTree> arg2 = List.<JCTree>nil();
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
arg2 = arg2.append(i.accept(this, arg));
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration typeDeclaration : n.getTypes()) {
arg2 = arg2.append(typeDeclaration.accept(this, arg));
}
}
return new AJCCompilationUnit(make.TopLevel(arg0, arg1, arg2), ((n.getComment() != null) ? n.getComment().getContent() : null));
}
示例11: apply
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public String apply(Node node) {
if (node instanceof NamedNode) {
String name = ((NamedNode)node).getName();
Node current = node;
while (!(current instanceof CompilationUnit)) {
current = current.getParentNode();
}
CompilationUnit compilationUnit = (CompilationUnit) current;
for (ImportDeclaration importDecl : compilationUnit.getImports()) {
NameExpr importExpr = importDecl.getName();
if (importExpr instanceof QualifiedNameExpr) {
QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr;
String className = qualifiedNameExpr.getName();
if (name.equals(className)) {
return qualifiedNameExpr.getQualifier().toString();
}
} else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) {
String importName = importDecl.getName().getName();
return importName.substring(0, importName.length() - name.length() -1);
}
}
try {
Class.forName(JAVA_LANG + "." + name);
return JAVA_LANG;
} catch (ClassNotFoundException ex) {
return compilationUnit.getPackage().getPackageName();
}
}
return null;
}
示例12: getImportDeclarationFor
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
* Looks up the import declaration applicable to the presented name
* expression.
* <p>
* If a fully-qualified name is passed to this method, the corresponding
* import will be evaluated for a complete match. If a simple name is passed
* to this method, the corresponding import will be evaluated if its simple
* name matches. This therefore reflects the normal Java semantics for using
* simple type names that have been imported.
*
* @param compilationUnit the types in the compilation unit
* (required)
* @param nameExpr the expression to locate an import for (which would
* generally be a {@link NameExpr} and thus not have a package
* identifier; required)
* @return the relevant import, or null if there is no import for the
* expression
*/
public static ImportDeclaration getImportDeclarationFor(
final CompilationUnit compilationUnit,
final NameExpr nameExpr) {
Validate.notNull(compilationUnit,
"Compilation unit services required");
Validate.notNull(nameExpr, "Name expression required");
final List<ImportDeclaration> imports = compilationUnit
.getImports();
for (final ImportDeclaration candidate : imports) {
final NameExpr candidateNameExpr = candidate.getName();
if (!candidate.toString().contains("*")) {
if(!(candidateNameExpr instanceof QualifiedNameExpr)) {
System.out.println("Expected import '" + candidate
+ "' to use a fully-qualified type name");
}
}
if (nameExpr instanceof QualifiedNameExpr) {
// User is asking for a fully-qualified name; let's see if there
// is a full match
if (NameUtil.isEqual(nameExpr, candidateNameExpr)) {
return candidate;
}
}
else {
// User is not asking for a fully-qualified name, so let's do a
// simple name comparison that discards the import's
// qualified-name package
if (candidateNameExpr.getName().equals(nameExpr.getName())) {
return candidate;
}
}
}
return null;
}