Java Code Examples for com.github.javaparser.ast.CompilationUnit#setPackage()
The following examples show how to use
com.github.javaparser.ast.CompilationUnit#setPackage() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaSourceUtils.java From dolphin with Apache License 2.0 | 6 votes |
public static String mergeContent(CompilationUnit one, CompilationUnit two) throws Exception { // 包声明不同,返回null if (!one.getPackage().equals(two.getPackage())) return null; CompilationUnit cu = new CompilationUnit(); // add package declaration to the compilation unit PackageDeclaration pd = new PackageDeclaration(); pd.setName(one.getPackage().getName()); cu.setPackage(pd); // check and merge file comment; Comment fileComment = mergeSelective(one.getComment(), two.getComment()); cu.setComment(fileComment); // check and merge imports List<ImportDeclaration> ids = mergeListNoDuplicate(one.getImports(), two.getImports()); cu.setImports(ids); // check and merge Types List<TypeDeclaration> types = mergeTypes(one.getTypes(), two.getTypes()); cu.setTypes(types); return cu.toString(); }
Example 2
Source File: FormTask.java From enkan with Eclipse Public License 1.0 | 5 votes |
@Override public void execute(PathResolver pathResolver) throws Exception { CompilationUnit cu = new CompilationUnit(); String basePackage = BasePackageDetector.detect(); cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(basePackage + "form"))); ClassOrInterfaceDeclaration formClass = new ClassOrInterfaceDeclaration( ModifierSet.PUBLIC, false, CaseConverter.pascalCase(tableName) + "Form"); ASTHelper.addTypeDeclaration(cu, formClass); formClass.setExtends(Collections.singletonList( new ClassOrInterfaceType("FormBase") )); fields.stream() .filter(f -> !f.isId()) .forEach(f -> ASTHelper.addMember(formClass, fieldDeclaration(f))); fields.stream() .filter(f -> !f.isId()) .forEach(f -> ASTHelper.addMember(formClass, getterDeclaration(f))); fields.stream() .filter(f -> !f.isId()) .forEach(f -> ASTHelper.addMember(formClass, setterDeclaration(f))); try (Writer writer = new OutputStreamWriter(pathResolver.destinationAsStream(destination))) { writer.write(cu.toString()); } }
Example 3
Source File: JavaparserTest.java From dolphin with Apache License 2.0 | 5 votes |
@Before public void setup(){ CompilationUnit cu = new CompilationUnit(); // set the package cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test"))); // create the type declaration ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass"); ASTHelper.addTypeDeclaration(cu, type); // create a method MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main"); method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC)); ASTHelper.addMember(type, method); // add a parameter to the method Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args"); param.setVarArgs(true); ASTHelper.addParameter(method, param); // add a body to the method BlockStmt block = new BlockStmt(); method.setBody(block); // add a statement do the method body NameExpr clazz = new NameExpr("System"); FieldAccessExpr field = new FieldAccessExpr(clazz, "out"); MethodCallExpr call = new MethodCallExpr(field, "println"); ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!")); ASTHelper.addStmt(block, call); unit = cu; }
Example 4
Source File: FlywayTask.java From enkan with Eclipse Public License 1.0 | 4 votes |
@Override public void execute(PathResolver pathResolver) throws Exception { CompilationUnit cu = new CompilationUnit(); cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("db.migration"))); List<ImportDeclaration> imports = new ArrayList<>(); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.BaseJavaMigration"), false, false)); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.Context"), false, false)); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("java.sql.Statement"), false, false)); cu.setImports(imports); String version = LocalDateTime.now().format(VERSION_FMT); ClassOrInterfaceDeclaration migrationClass = new ClassOrInterfaceDeclaration( ModifierSet.PUBLIC, false, "V" + version + "__Create" + CaseConverter.pascalCase(tableName)); ASTHelper.addTypeDeclaration(cu, migrationClass); List<ClassOrInterfaceType> extendsList = new ArrayList<>(); extendsList.add(new ClassOrInterfaceType("BaseJavaMigration")); migrationClass.setExtends(extendsList); MethodDeclaration migrateMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "migrate"); ASTHelper.addMember(migrationClass, migrateMethod); ASTHelper.addParameter(migrateMethod, ASTHelper.createParameter( ASTHelper.createReferenceType("Context", 0), "context" )); migrateMethod.setThrows(Collections.singletonList(ASTHelper.createNameExpr("Exception"))); List<AnnotationExpr> annotations = new ArrayList<>(); annotations.add(new MarkerAnnotationExpr(ASTHelper.createNameExpr("Override"))); migrateMethod.setAnnotations(annotations); BlockStmt block = new BlockStmt(); migrateMethod.setBody(block); TryStmt tryStmt = new TryStmt(createStatementExecuteBlock(), null, null); tryStmt.setResources(Collections.singletonList(createAssignStatement())); ASTHelper.addStmt(block, tryStmt); try (Writer writer = new OutputStreamWriter( pathResolver.destinationAsStream( "src/main/java/db/migration/" + migrationClass.getName() + ".java"))) { writer.write(cu.toString()); } }
Example 5
Source File: CompilationUnitMerger.java From dolphin with Apache License 2.0 | 3 votes |
@Override public CompilationUnit doMerge(CompilationUnit first, CompilationUnit second) { CompilationUnit unit = new CompilationUnit(); unit.setPackage(mergeSingle(first.getPackage(), second.getPackage())); unit.setImports(mergeCollections(first.getImports(), second.getImports())); unit.setTypes(mergeCollections(first.getTypes(), second.getTypes())); return unit; }