public void writeFile(String baseDir, String fileName, String content, boolean format) throws Exception { File file = new File(baseDir + "/" + fileName); if (format) { ServiceBuilder.writeFile(file, content); } else { System.out.println("Writing " + file); FileUtil.write(file, content); } }
private void _copyInterface(String parentDir, String srcFile) throws IOException { JavaClass javaClass = _getJavaClass(parentDir, srcFile); JavaMethod[] methods = javaClass.getMethods(); Arrays.sort(methods, new JavaMethodComparator()); StringMaker sm = new StringMaker(); // Package sm.append("package " + javaClass.getPackage() + ";"); // Imports sm.append("[$IMPORTS$]"); // Class declaration sm.append( "public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {"); String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I); // Methods Set imports = new TreeSet(); for (int i = 0; i < methods.length; i++) { JavaMethod javaMethod = methods[i]; String methodName = javaMethod.getName(); if (javaMethod.isPublic()) { String returnValueName = javaMethod.getReturns().getValue(); imports.add(returnValueName); sm.append( "public " + javaMethod.getReturns().getJavaClass().getName() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "("); JavaParameter[] parameters = javaMethod.getParameters(); for (int j = 0; j < parameters.length; j++) { JavaParameter javaParameter = parameters[j]; sm.append( javaParameter.getType().getJavaClass().getName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName()); imports.add(javaParameter.getType().getValue()); if ((j + 1) != parameters.length) { sm.append(", "); } } sm.append(")"); Type[] thrownExceptions = javaMethod.getExceptions(); Set newExceptions = new LinkedHashSet(); for (int j = 0; j < thrownExceptions.length; j++) { Type thrownException = thrownExceptions[j]; newExceptions.add(thrownException.getJavaClass().getName()); imports.add(thrownException.getValue()); } if (newExceptions.size() > 0) { sm.append(" throws "); Iterator itr = newExceptions.iterator(); while (itr.hasNext()) { sm.append(itr.next()); if (itr.hasNext()) { sm.append(", "); } } } sm.append("{"); if (!returnValueName.equals("void")) { sm.append("return "); } sm.append(varName + "." + methodName + "("); for (int j = 0; j < parameters.length; j++) { JavaParameter javaParameter = parameters[j]; sm.append(javaParameter.getName()); if ((j + 1) != parameters.length) { sm.append(", "); } } sm.append(");"); sm.append("}"); } } // Fields sm.append("private " + javaClass.getName() + " " + varName + ";"); // Class close brace sm.append("}"); // Imports String content = sm.toString(); sm = new StringMaker(); Iterator itr = imports.iterator(); while (itr.hasNext()) { String importClass = (String) itr.next(); if (!importClass.equals("boolean") && !importClass.equals("double") && !importClass.equals("int") && !importClass.equals("long") && !importClass.equals("short") && !importClass.equals("void")) { sm.append("import " + importClass + ";"); } } content = StringUtil.replace(content, "[$IMPORTS$]", sm.toString()); // Write file File file = new File( parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/Copy" + javaClass.getName() + ".java"); ServiceBuilder.writeFile(file, content); }