String brewJava(String methodStr, String classNamesStr, String classSuffix) { StringBuilder builder = new StringBuilder(); builder.append("// Generated code from XLog. Do not modify!\n"); builder.append("package ").append(XLogUtils.PKG_NAME).append(";\n\n"); builder.append("import java.lang.String;\n"); builder.append('\n'); builder.append("public final class ").append(XLogUtils.CLASS_NAME + classSuffix); builder.append(" {\n"); builder .append("\tpublic static String ") .append(XLogUtils.FIELD_NAME_METHODS) .append(" = ") .append(stringLiteral(methodStr)) .append(";\n"); builder .append("\tpublic static String ") .append(XLogUtils.FIELD_NAME_CLASSES) .append(" = ") .append(stringLiteral(classNamesStr)) .append(";\n"); builder.append("}\n"); return builder.toString(); }
private String asString(List<? extends TypeMirror> ts, String sep) { StringBuilder sb = new StringBuilder(); for (TypeMirror t : ts) { if (sb.length() != 0) sb.append(sep); sb.append(asString(t)); } return sb.toString(); }
private void generateXLogProcessor( String classSuffix, List<MethodToLog> methodToLogs, List<String> classNames) { StringBuilder methodsSb = new StringBuilder(); methodsSb.append("["); if (methodToLogs != null) { for (int i = 0; i < methodToLogs.size(); i++) { methodsSb.append(methodToLogs.get(i).toString()); if (i < methodToLogs.size() - 1) { methodsSb.append(","); } } } methodsSb.append("]"); String methodToLogStr = methodsSb.toString(); StringBuilder classSb = new StringBuilder(); classSb.append("["); if (methodToLogs != null) { for (int i = 0; i < classNames.size(); i++) { classSb.append(classNames.get(i).toString()); if (i < classNames.size() - 1) { classSb.append(","); } } } classSb.append("]"); String classNamesStr = classSb.toString(); System.out.println(methodToLogStr); try { JavaFileObject jfo = filer.createSourceFile(XLogUtils.PKG_NAME + "." + XLogUtils.CLASS_NAME + classSuffix); Writer writer = jfo.openWriter(); writer.write(brewJava(methodToLogStr, classNamesStr, classSuffix)); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
private void printMethod(ExecutableElement executableElement) { StringBuilder s = new StringBuilder(256); s.append("method: " + executableElement).append("\n\t"); s.append("annotations: " + executableElement.getAnnotationMirrors()).append("\n\t"); s.append("return: " + executableElement.getReturnType()).append("\n\t"); for (VariableElement var : executableElement.getParameters()) { s.append("parameter: " + var + ", " + var.getAnnotation(ApiParameterDoc.class)) .append("\n\t"); } generator.log(s.toString()); }
private static String wrapText(Element element, AnnotationMirror mirror, String text) { StringBuilder b = new StringBuilder(); if (element != null) { b.append("Element " + element.toString()); } if (mirror != null) { b.append(" at annotation @" + Utils.getSimpleName(mirror.getAnnotationType())); } if (b.length() > 0) { b.append(" is erroneous: ").append(text); return b.toString(); } else { return text; } }
private void parsePackageName(TypeElement layoutElement) { final String[] packageComponents = layoutElement.getQualifiedName().toString().split("\\."); final StringBuilder packageBuilder = new StringBuilder(); for (int n = 0; n < packageComponents.length; n++) { if (Character.isUpperCase(packageComponents[n].charAt(0))) { break; } if (n > 0) { packageBuilder.append('.'); } packageBuilder.append(packageComponents[n]); } packageName = packageBuilder.toString(); }
String stringLiteral(String value) { StringBuilder result = new StringBuilder(); result.append('"'); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch (c) { case '"': result.append("\\\""); break; case '\\': result.append("\\\\"); break; case '\b': result.append("\\b"); break; case '\t': result.append("\\t"); break; case '\n': result.append("\\n"); if (i + 1 < value.length()) { result.append("\"\n").append(" ").append(" ").append("+ \""); } break; case '\f': result.append("\\f"); break; case '\r': result.append("\\r"); break; default: if (Character.isISOControl(c)) { new Formatter(result).format("\\u%04x", (int) c); } else { result.append(c); } } } result.append('"'); return result.toString(); }