try { writer = new FileWriter(outputFile); writer.write(cu.toString()); } finally { if (writer != null) {
public static void main(String[] args) throws Exception { if (args.length < 2) { throw new IllegalArgumentException("Usage: outputDirectory inputSourceFiles"); } File outputDirectory = new File(args[0]); for (int i = 1; i < args.length; i++) { File file = new File(args[i]); System.out.println("Processing " + file); CompilationUnit cu = JavaParser.parse(file); new JavaParsingAtomicArrayQueueGenerator(file.getName()).visit(cu, null); organiseImports(cu); FileWriter writer = null; String outputFileName = file.getName(); if (outputFileName.endsWith(".java")) { outputFileName = translateQueueName(outputFileName.replace(".java", "")); } else { outputFileName = translateQueueName(outputFileName); } outputFileName += ".java"; try { writer = new FileWriter(new File(outputDirectory, outputFileName)); writer.write(cu.toString()); } finally { if (writer != null) { writer.close(); } } } }
code = old.toString();
public void updateMainActivity() throws Exception { findMainActivityPath(); if (mainActivityPath == null) { throw new FileNotFoundException("MainActivity.java not found"); } FileInputStream in = new FileInputStream(mainActivityPath); try { compilationUnit = JavaParser.parse(in); } finally { in.close(); } boolean importsChanged = updateImports(); boolean modulesChanged = updateModules(); if (importsChanged && modulesChanged) { InstallerUtil.writeToDisk(mainActivityPath, compilationUnit.toString()); System.out.println("Updated import and module statements in " + mainActivityPath); } else if (!importsChanged && !modulesChanged) { System.out.println("Import and module statements were not updated, is the class " + mainActivityPath + " updated already?"); } else { throw new Exception("Could not update import or module, update your code manually"); } }
public void updateMainActivity() throws Exception { findMainActivityPath(); if (mainActivityPath == null) { throw new FileNotFoundException("MainActivity.java not found"); } FileInputStream in = new FileInputStream(mainActivityPath); try { compilationUnit = JavaParser.parse(in); } finally { in.close(); } boolean importsChanged = updateImports(); boolean modulesChanged = updateModules(); if (importsChanged && modulesChanged) { InstallerUtil.writeToDisk(mainActivityPath, compilationUnit.toString()); System.out.println("Updated import and module statements in " + mainActivityPath); } else if (!importsChanged && !modulesChanged) { System.out.println("Import and module statements were not updated, is the class " + mainActivityPath + " updated already?"); } else { throw new Exception("Could not update import or module, update your code manually"); } }
/** * Util method to make source merge more convenient * * @param first merge params, specifically for the existing source * @param second merge params, specifically for the new source * @return merged result * @throws ParseException cannot parse the input params */ public static String merge(String first, String second) throws ParseException { JavaParser.setDoNotAssignCommentsPreceedingEmptyLines(false); CompilationUnit cu1 = JavaParser.parse(new StringReader(first), true); CompilationUnit cu2 = JavaParser.parse(new StringReader(second), true); AbstractMerger<CompilationUnit> merger = AbstractMerger.getMerger(CompilationUnit.class); CompilationUnit result = merger.merge(cu1, cu2); return result.toString(); }
@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()); } }
pathResolver.destinationAsStream( "src/main/java/db/migration/" + migrationClass.getName() + ".java"))) { writer.write(cu.toString());
public String format() { CompilationUnit cu = new CompilationUnit(); if (fileComment != null) { cu.addOrphanComment(new JavadocComment(fileComment)); } cu.setPackageDeclaration(pkg); for (String imp : imports) { cu.addImport(imp); } cu.addType(type); for (Member member : gatherFinalMembers(members, cu)) { type.addMember(member.getDeclaration()); } return cu.toString(); }
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(); }