本文整理汇总了Java中com.github.javaparser.ast.CompilationUnit.getTypes方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.getTypes方法的具体用法?Java CompilationUnit.getTypes怎么用?Java CompilationUnit.getTypes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.javaparser.ast.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.getTypes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractClassName
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Nullable
private static String extractClassName( String code, PrintStream err ) {
InputStream inputStream = new ByteArrayInputStream( code.getBytes( StandardCharsets.UTF_8 ) );
try {
CompilationUnit compilationUnit = JavaParser.parse( inputStream );
List<TypeDeclaration> types = compilationUnit.getTypes();
if ( types.size() == 1 ) {
String simpleType = types.get( 0 ).getName();
return Optional.ofNullable( compilationUnit.getPackage() )
.map( PackageDeclaration::getPackageName )
.map( it -> it + "." + simpleType )
.orElse( simpleType );
} else if ( types.size() == 0 ) {
err.println( "No class definition found" );
} else {
err.println( "Too many class definitions found. Only one class can be defined at a time." );
}
} catch ( ParseException e ) {
// ignore error, let the compiler provide an error message
return "Err";
}
return null;
}
示例2: doIsEquals
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public boolean doIsEquals(CompilationUnit first, CompilationUnit second) {
// 检测包声明
if (!isEqualsUseMerger(first.getPackage(), second.getPackage())) return false;
// 检查公共类声明
for (TypeDeclaration outer : first.getTypes()) {
for (TypeDeclaration inner : second.getTypes()) {
if (ModifierSet.isPublic(outer.getModifiers()) && ModifierSet.isPublic(inner.getModifiers())) {
if (outer.getName().equals(inner.getName())) {
return true;
}
}
}
}
return false;
}
示例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 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);
}
}
}
示例5: 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;
}
示例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: fromCode
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public static CodeStubBuilder fromCode(String code) {
CompilationUnit cu = JavaParser.parse(code);
for (TypeDeclaration<?> type : cu.getTypes()) {
if (type instanceof ClassOrInterfaceDeclaration
&& type.getModifiers().contains(Modifier.PUBLIC)) {
String className = type.getNameAsString();
return new CodeStubBuilder(className);
}
}
throw new IllegalArgumentException("Task instance's code did not contain any public class");
}
示例8: changeMethods
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
private static void changeMethods(CompilationUnit cu) {
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
changeMethod(method);
}
}
}
}
示例9: calculateRelations
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public void calculateRelations(Collection<File> files) {
System.out.println();
int totalFiles = files.size();
int fileIndex = 1;
for (File file : files) {
try {
CompilationUnit cu = parse(file);
NodeList<TypeDeclaration<?>> types = cu.getTypes();
for (TypeDeclaration<?> type : types) {
boolean isInterface = type instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) type).isInterface();
boolean isAnnotation = type instanceof AnnotationDeclaration;
boolean isEnumeration = type instanceof EnumDeclaration;
boolean isClass = !isAnnotation && !isEnumeration && !isInterface;
if (isInterface) {
// check if this interface extends another interface and persist relation in EXTENDS table
ClassOrInterfaceDeclaration interfaceDeclaration = (ClassOrInterfaceDeclaration) type;
extendsRelationCalculator.calculate(interfaceDeclaration, cu);
}
if (isClass) {
ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) type;
// check if this class implements an interface and persist relation in IMPLEMENTS table
implementsRelationCalculator.calculate(classDeclaration, cu);
// check if this class extends another class and persist relation in EXTENDS table
extendsRelationCalculator.calculate(classDeclaration, cu);
}
if (isClass || isInterface) {
annotatedWithCalculator.calculate((ClassOrInterfaceDeclaration) type, cu);
}
}
} catch (ParseProblemException | IOException e) {
System.err.println("Error while parsing " + file.getAbsolutePath());
}
System.out.print("\rCalculating relations: " + getPercent(fileIndex, totalFiles) + "% " + ("(" + fileIndex + "/" + totalFiles + ")"));
fileIndex++;
}
System.out.println();
}
示例10: 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);
}
示例11: 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));
}
示例12: testParser
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Test
public void testParser() throws Exception {
CompilationUnit cu = Sources.FROM_CLASSPATH_TO_COMPILATIONUNIT.apply("io/sundr/builder/BaseFluent.java");
String packageName = cu.getPackage().getPackageName();
Assert.assertEquals("io.sundr.builder", packageName);
for (TypeDeclaration typeDeclaration : cu.getTypes()) {
TypeDef typeDef = Sources.TYPEDEF.apply(typeDeclaration);
System.out.print(typeDef);
}
}
示例13: getClassName
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public static ClassOrInterfaceDeclaration getClassName(CompilationUnit cu) {
List<TypeDeclaration> types = cu.getTypes();
if (Utils.hasLength(types)) {
TypeDeclaration real = types.get(0);
if (real instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration rl = (ClassOrInterfaceDeclaration) real;
return rl;
}
}
return null;
}
示例14: addTypeDeclaration
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
* Adds the given type declaration to the compilation unit. The list of
* types will be initialized if it is <code>null</code>.
*
* @param cu
* compilation unit
* @param type
* type declaration
*/
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
List<TypeDeclaration> types = cu.getTypes();
if (isNullOrEmpty(types)) {
types = new ArrayList<TypeDeclaration>();
cu.setTypes(types);
}
types.add(type);
}
示例15: addTypeDeclaration
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
* Adds the given type declaration to the compilation unit. The list of
* types will be initialized if it is <code>null</code>.
*
* @param cu
* compilation unit
* @param type
* type declaration
*/
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
List<TypeDeclaration> types = cu.getTypes();
if (types == null) {
types = new ArrayList<TypeDeclaration>();
cu.setTypes(types);
}
types.add(type);
}