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


Java CompilationUnit.getTypes方法代码示例

本文整理汇总了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;
}
 
开发者ID:renatoathaydes,项目名称:osgiaas,代码行数:25,代码来源:JavaCommand.java

示例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;
}
 
开发者ID:beihaifeiwu,项目名称:dolphin,代码行数:19,代码来源:CompilationUnitMerger.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,代码来源:VoidVisitorAdapter.java

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

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

示例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);
            }
        }
    }
}
 
开发者ID:bingoohuang,项目名称:javacode-demo,代码行数:13,代码来源:MethodChangerWithoutVisitor.java

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

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

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

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

示例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;
}
 
开发者ID:niaoge,项目名称:spring-dynamic,代码行数:12,代码来源:ParserUtils.java

示例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);

}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:19,代码来源:ASTHelper.java

示例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);

}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:19,代码来源:ASTHelper.java


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