use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class MethodChanger_2 method changeMethods.
private static void changeMethods(CompilationUnit cu) {
// Go through all the types in the file
NodeList<TypeDeclaration<?>> types = cu.getTypes();
for (TypeDeclaration<?> type : types) {
// Go through all fields, methods, etc. in this type
NodeList<BodyDeclaration<?>> members = type.getMembers();
for (BodyDeclaration<?> member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
changeMethod(method);
}
}
}
}
use of com.github.javaparser.ast.body.TypeDeclaration in project AndroidLife by CaMnter.
the class R2ClassBuilder method brewJava.
/**
* JavaPoet 生成 R2
*
* @param rFile R.java File
* @param outputDir R2.java 输出文件夹
* @param packageName 包名
* @param className R2 name
* @throws Exception exception
*/
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
/*
* JavaParser 解析 R.java File
* 获取到 TypeDeclaration
*/
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
/*
* 定义 R2.java class
*/
TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
/*
* 遍历 R.java File 的每一个节点( 内部类或者接口 --> ClassOrInterfaceDeclaration )
* 添加到 R2.java 内
* 这里是给 TypeSpec 添加生成 内部类 的 语句
*/
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from gradle plugin. Do not modify!").build();
finalR.writeTo(outputDir);
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class CloneVisitor method visit.
@Override
public Node visit(TypeDeclarationStmt _n, Object _arg) {
TypeDeclaration typeDecl = cloneNodes(_n.getTypeDeclaration(), _arg);
Comment comment = cloneNodes(_n.getComment(), _arg);
TypeDeclarationStmt r = new TypeDeclarationStmt(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), typeDecl);
r.setComment(comment);
return r;
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class CloneVisitor method visit.
@Override
public Node visit(CompilationUnit _n, Object _arg) {
PackageDeclaration package_ = cloneNodes(_n.getPackage(), _arg);
List<ImportDeclaration> imports = visit(_n.getImports(), _arg);
List<TypeDeclaration> types = visit(_n.getTypes(), _arg);
return new CompilationUnit(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), package_, imports, types);
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class DumpVisitor method visit.
@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);
}
Aggregations