当前位置: 首页>>代码示例>>Java>>正文


Java CompilationUnit.getImports方法代码示例

本文整理汇总了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);
   }
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:27,代码来源:DumpVisitor.java

示例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);
		}
	}
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:17,代码来源:VoidVisitorAdapter.java

示例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);
        }
    }
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:17,代码来源:JsonVisitorAdapter.java

示例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;
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:21,代码来源:ModifierVisitorAdapter.java

示例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);
}
 
开发者ID:ragnraok,项目名称:JParserUtil,代码行数:17,代码来源:SourceTreeVisitor.java

示例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);
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:27,代码来源:DumpVisitor.java

示例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();
}
 
开发者ID:sfPlayer1,项目名称:Matcher,代码行数:32,代码来源:SrcRemapper.java

示例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;
}
 
开发者ID:paypal,项目名称:butterfly,代码行数:31,代码来源:AbstractTypeCheck.java

示例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);
}
 
开发者ID:pcgomes,项目名称:javaparser2jctree,代码行数:29,代码来源:ASTDumpVisitor.java

示例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));
}
 
开发者ID:pcgomes,项目名称:javaparser2jctree,代码行数:36,代码来源:JavaParser2JCTree.java

示例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;
}
 
开发者ID:sundrio,项目名称:sundrio,代码行数:34,代码来源:Sources.java

示例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;
}
 
开发者ID:BenDol,项目名称:Databind,代码行数:55,代码来源:TypeUtil.java


注:本文中的com.github.javaparser.ast.CompilationUnit.getImports方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。