diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2016-11-14 21:27:12 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2016-11-14 21:27:12 +0100 |
commit | 14110d66a455f8bee032e831d3453afccaa0b0c9 (patch) | |
tree | cf87ecd38277a2fa3801ded8abf9e508b00139ad /vespa-documentgen-plugin | |
parent | a03fb97bbb8e9d1515f1168efb7cd16367aca7c2 (diff) |
Static plus intellij recommendations.
Diffstat (limited to 'vespa-documentgen-plugin')
-rw-r--r-- | vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java | 101 |
1 files changed, 47 insertions, 54 deletions
diff --git a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java index 1307b25920a..d791e99702f 100644 --- a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java +++ b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java @@ -303,7 +303,7 @@ public class DocumentGenMojo extends AbstractMojo { StructDataType annStruct = (StructDataType)annType.getDataType(); StructDataType annStructTmp = new StructDataType("fields"); // Change the type name annStructTmp.assign(annStruct); - Collection<DataType> tmpList = new ArrayList<DataType>(); + Collection<DataType> tmpList = new ArrayList<>(); tmpList.add(annStructTmp); exportStructTypes(tmpList, out, 1, null); exportFieldsAndAccessors(className, annStruct.getFieldsThisTypeOnly(), out, 1, false); @@ -356,18 +356,18 @@ public class DocumentGenMojo extends AbstractMojo { } private String annTypeModifier(AnnotationType annType) { - if (isAbstract(annType.getName())) return "abstract "; - return ""; - } + if (isAbstract(annType.getName())) return "abstract "; + return ""; + } - private String exportInnerImportsFromDocAndSuperTypes(NewDocumentType docType, String packageName) { - String ret = ""; - ret = ret + "import "+packageName+"."+className(docType.getName())+".*;\n"; - ret = ret + exportInnerImportsFromSuperTypes(docType, packageName); - return ret; - } + private static String exportInnerImportsFromDocAndSuperTypes(NewDocumentType docType, String packageName) { + String ret = ""; + ret = ret + "import "+packageName+"."+className(docType.getName())+".*;\n"; + ret = ret + exportInnerImportsFromSuperTypes(docType, packageName); + return ret; + } - private String exportInnerImportsFromSuperTypes(NewDocumentType docType, String packageName) { + private static String exportInnerImportsFromSuperTypes(NewDocumentType docType, String packageName) { String ret = ""; for (NewDocumentType inherited : docType.getInherited()) { if (inherited.getName().equals("document")) continue; @@ -384,8 +384,7 @@ public class DocumentGenMojo extends AbstractMojo { return className(annType.getInheritedTypes().iterator().next().getName()); } - private void exportDocumentSources(File outputDir, NewDocumentType docType, String packageName) - throws MojoFailureException { + private void exportDocumentSources(File outputDir, NewDocumentType docType, String packageName) throws MojoFailureException { File dirForSources = new File(outputDir, packageName.replaceAll("\\.", "/")); dirForSources.mkdirs(); File target = new File(dirForSources, className(docType.getName())+".java"); @@ -436,8 +435,8 @@ public class DocumentGenMojo extends AbstractMojo { // Mimic header and body to make serialization work. // This can be improved by generating a method to serialize the document _here_, and use that in serialization. - exportOverriddenStructGetter(((StructDataType) docType.allHeader()).getFields(), out, 1, "getHeader", className+".headerStructType"); - exportOverriddenStructGetter(((StructDataType) docType.allBody()).getFields(), out, 1, "getBody", className+".bodyStructType"); + exportOverriddenStructGetter(docType.allHeader().getFields(), out, 1, "getHeader", className+".headerStructType"); + exportOverriddenStructGetter(docType.allBody().getFields(), out, 1, "getBody", className+".bodyStructType"); exportStructTypeGetter(docType.getName()+".header", docType.allHeader().getFields(), out, 1, "getHeaderStructType", "com.yahoo.document.StructDataType"); exportStructTypeGetter(docType.getName()+".body", docType.allBody().getFields(), out, 1, "getBodyStructType", "com.yahoo.document.StructDataType"); @@ -459,14 +458,14 @@ public class DocumentGenMojo extends AbstractMojo { * The Java class the class of the given type should inherit from. If the input type inherits from _one_ * other type, use that, otherwise Document. */ - private String javaSuperType(NewDocumentType docType) { + private static String javaSuperType(NewDocumentType docType) { String ret = "com.yahoo.document.Document"; Collection<NewDocumentType> specInheriteds = specificInheriteds(docType); if (!specInheriteds.isEmpty() && singleInheritance(specInheriteds)) ret = className(specInheriteds.iterator().next().getName()); return ret; } - private boolean singleInheritance(Collection<NewDocumentType> specInheriteds) { + private static boolean singleInheritance(Collection<NewDocumentType> specInheriteds) { if (specInheriteds.isEmpty()) return true; if (specInheriteds.size()>1) return false; return singleInheritance(specificInheriteds(specInheriteds.iterator().next())); @@ -476,8 +475,8 @@ public class DocumentGenMojo extends AbstractMojo { * The inherited types that are not Document * @return collection of specific inherited types */ - private Collection<NewDocumentType> specificInheriteds(NewDocumentType type) { - List<NewDocumentType> ret = new ArrayList<NewDocumentType>(); + private static Collection<NewDocumentType> specificInheriteds(NewDocumentType type) { + List<NewDocumentType> ret = new ArrayList<>(); for (NewDocumentType t : type.getInherited()) { if (!"document".equals(t.getName())) ret.add(t); } @@ -489,7 +488,7 @@ public class DocumentGenMojo extends AbstractMojo { * * NOTE: This is important, the docproc framework uses that constructor. */ - private void exportCopyConstructor(String className, Collection<Field> fieldSet, Writer out, int ind, boolean docId) throws IOException { + private static void exportCopyConstructor(String className, Collection<Field> fieldSet, Writer out, int ind, boolean docId) throws IOException { out.write( ind(ind)+"/**\n"+ ind(ind)+" * Constructs a "+className+" by taking a deep copy of the provided StructuredFieldValue.\n" + @@ -518,7 +517,7 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+"}\n\n"); } - private void exportStructTypeGetter(String name, Collection<Field> fields, Writer out, int ind, String methodName, String retType) throws IOException { + private static void exportStructTypeGetter(String name, Collection<Field> fields, Writer out, int ind, String methodName, String retType) throws IOException { out.write(ind(ind)+"private static "+retType+" "+methodName+"() {\n" + ind(ind+1)+retType+" ret = new "+retType+"(\""+name+"\");\n"); for (Field f : fields) { @@ -529,7 +528,7 @@ public class DocumentGenMojo extends AbstractMojo { out.write(ind(ind)+"}\n\n"); } - private void exportOverriddenStructGetter(Collection<Field> fields, Writer out, int ind, String methodName, String structType) throws IOException { + private static void exportOverriddenStructGetter(Collection<Field> fields, Writer out, int ind, String methodName, String structType) throws IOException { out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.Struct "+methodName+"() {\n" + ind(ind+1)+"com.yahoo.document.datatypes.Struct ret = new com.yahoo.document.datatypes.Struct("+structType+");\n"); for (Field f : fields) { @@ -543,7 +542,7 @@ public class DocumentGenMojo extends AbstractMojo { /** * Exports the necessary overridden methods from Document/StructuredFieldValue */ - private void exportDocumentMethods(Collection<Field> fieldSet, Writer out, + private static void exportDocumentMethods(Collection<Field> fieldSet, Writer out, int ind) throws IOException { exportGetFieldCount(fieldSet, out, ind); exportGetField(fieldSet, out, ind); @@ -556,7 +555,7 @@ public class DocumentGenMojo extends AbstractMojo { } - private void exportEquals(String className, Collection<Field> fieldSet, Writer out, int ind) throws IOException { + private static void exportEquals(String className, Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public boolean equals(Object o) {\n"); out.write(ind(ind+1)+"if (!(o instanceof "+className+")) return false;\n"); out.write(ind(ind+1)+className+" other = ("+className+")o;\n"); @@ -568,7 +567,7 @@ public class DocumentGenMojo extends AbstractMojo { out.write(ind(ind)+"}\n\n"); } - private void exportToNamedMap(Writer out, int ind) throws IOException { + private static void exportToNamedMap(Writer out, int ind) throws IOException { // A helper to convert from SpanTree collection to Map. Can be removed if StringFieldValue is fixed to expose the map. out.write( ind()+"private java.util.Map<java.lang.String,com.yahoo.document.annotation.SpanTree> toNamedMap(java.util.Collection<com.yahoo.document.annotation.SpanTree> coll) {\n" + @@ -580,8 +579,7 @@ public class DocumentGenMojo extends AbstractMojo { ); } - private void exportHashCode(Collection<Field> fieldSet, Writer out, int ind, String hcBase) - throws IOException { + private static void exportHashCode(Collection<Field> fieldSet, Writer out, int ind, String hcBase) throws IOException { out.write(ind(ind)+"@Override public int hashCode() {\n"); out.write(ind(ind+1)+"int hc = "+hcBase+";\n"); for (Field field: fieldSet) { @@ -591,8 +589,7 @@ public class DocumentGenMojo extends AbstractMojo { out.write(ind(ind)+"}\n\n"); } - private void exportClear(Collection<Field> fieldSet, Writer out, int ind) - throws IOException { + private static void exportClear(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public void clear() {\n"); for (Field field: fieldSet) { out.write(ind(ind+1)+setter(field.getName())+"(null);\n"); @@ -600,8 +597,7 @@ public class DocumentGenMojo extends AbstractMojo { out.write(ind(ind)+"}\n\n"); } - private void exportIterator(Collection<Field> fieldSet, Writer out, int ind) - throws IOException { + private static void exportIterator(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public java.util.Iterator<java.util.Map.Entry<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue>> iterator() {\n"); out.write( ind(ind+1)+"java.util.Map<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue> ret = new java.util.HashMap<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue>();\n" + @@ -628,8 +624,7 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+"}\n\n"); } - private void exportRemoveFieldValue(Collection<Field> fieldSet, Writer out, - int ind) throws IOException { + private static void exportRemoveFieldValue(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.FieldValue removeFieldValue(com.yahoo.document.Field field) {\n"); out.write(ind(ind+1)+"if (field==null) return null;\n"); for (Field field: fieldSet) { @@ -655,8 +650,7 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+"}\n\n"); } - private void exportSetFieldValue(Collection<Field> fieldSet, Writer out, - int ind) throws IOException { + private static void exportSetFieldValue(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.FieldValue setFieldValue(com.yahoo.document.Field field, com.yahoo.document.datatypes.FieldValue value) {\n"); for (Field field: fieldSet) { String name = field.getName(); @@ -677,7 +671,7 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+"}\n\n"); } - private void exportGetFieldValue(Collection<Field> fieldSet, Writer out, + private static void exportGetFieldValue(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.FieldValue getFieldValue(com.yahoo.document.Field field) {\n"); out.write(ind(ind+1)+"if (field==null) return null;\n"); @@ -705,8 +699,7 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+"}\n\n"); } - private void exportGetField(Collection<Field> fieldSet, Writer out, int ind) - throws IOException { + private static void exportGetField(Collection<Field> fieldSet, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public com.yahoo.document.Field getField(String fieldName) {\n"); out.write(ind(ind+1)+"if (fieldName==null) return null;\n"); for (Field field: fieldSet) { @@ -722,8 +715,8 @@ public class DocumentGenMojo extends AbstractMojo { /** * Exports the struct types found in this collection of fields as separate Java classes */ - private Set<DataType> exportStructTypes(Collection<DataType> fields, Writer out, int ind, Set<DataType> exportedStructs) throws IOException { - if (exportedStructs==null) exportedStructs=new HashSet<DataType>(); + private static Set<DataType> exportStructTypes(Collection<DataType> fields, Writer out, int ind, Set<DataType> exportedStructs) throws IOException { + if (exportedStructs==null) exportedStructs=new HashSet<>(); for (DataType f : fields) { if ((f instanceof StructDataType) && ! f.getName().contains(".")) { if (exportedStructs.contains(f)) continue; @@ -761,7 +754,7 @@ public class DocumentGenMojo extends AbstractMojo { /** * Override this, serialization of structs relies on it */ - private void exportGetFieldCount(Collection<Field> fields, Writer out, int ind) throws IOException { + private static void exportGetFieldCount(Collection<Field> fields, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public int getFieldCount() {\n"); out.write(ind(ind+1)+"int ret=0;\n"); for (Field f : fields) { @@ -774,7 +767,7 @@ public class DocumentGenMojo extends AbstractMojo { /** * Override the getFields() method of Struct, since serialization of Struct relies on it. */ - private void exportGetFields(Collection<Field> fields, Writer out, int ind) throws IOException { + private static void exportGetFields(Collection<Field> fields, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public java.util.Set<java.util.Map.Entry<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue>> getFields() {\n" + ind(ind+1)+"java.util.Map<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue> ret = new java.util.LinkedHashMap<com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue>();\n" + ind(ind+1)+"com.yahoo.document.Field f;\n"); @@ -788,7 +781,7 @@ public class DocumentGenMojo extends AbstractMojo { out.write(ind(ind)+"}\n\n"); } - private void exportAssign(StructDataType structType, String structClassName, Writer out, int ind) throws IOException { + private static void exportAssign(StructDataType structType, String structClassName, Writer out, int ind) throws IOException { out.write(ind(ind)+"@Override public void assign(Object o) {\n"+ ind(ind+1)+"if (!(o instanceof "+structClassName+")) { super.assign(o); return; }\n"+ ind(ind+1)+structClassName+" other = ("+structClassName+")o;\n"); @@ -803,7 +796,7 @@ public class DocumentGenMojo extends AbstractMojo { * Exports this set of fields with getters and setters * @param spanTrees If true, include a reference to the list of span trees for the string fields */ - private void exportFieldsAndAccessors(String className, Collection<Field> fields, Writer out, int ind, boolean spanTrees) throws IOException { + private static void exportFieldsAndAccessors(String className, Collection<Field> fields, Writer out, int ind, boolean spanTrees) throws IOException { // List the fields as Java fields for (Field field: fields) { DataType dt = field.getDataType(); @@ -828,18 +821,18 @@ public class DocumentGenMojo extends AbstractMojo { out.write("\n"); } - private String spanTreeSetter(String field) { + private static String spanTreeSetter(String field) { return setter(field)+"SpanTrees"; } - private String spanTreeGetter(String field) { + private static String spanTreeGetter(String field) { return field+"SpanTrees"; } /** * Returns spaces corresponding to the given levels of indentations */ - private String ind(int levels) { + private static String ind(int levels) { int indent = levels*STD_INDENT; StringBuilder sb = new StringBuilder(""); for (int i = 0 ; i<indent ; i++) { @@ -851,23 +844,23 @@ public class DocumentGenMojo extends AbstractMojo { /** * Returns spaces corresponding to 1 level of indentation */ - private String ind() { + private static String ind() { return ind(1); } - private String getter(String field) { + private static String getter(String field) { return "get"+upperCaseFirstChar(field); } - private String setter(String field) { + private static String setter(String field) { return "set"+upperCaseFirstChar(field); } - private String className(String field) { + private static String className(String field) { return upperCaseFirstChar(field); } - private String toJavaType(DataType dt) { + private static String toJavaType(DataType dt) { if (DataType.NONE.equals(dt)) return "void"; if (DataType.INT.equals(dt)) return "java.lang.Integer"; if (DataType.FLOAT.equals(dt)) return "java.lang.Float"; @@ -888,7 +881,7 @@ public class DocumentGenMojo extends AbstractMojo { } // bit stupid... - private String toJavaReference(DataType dt) { + private static String toJavaReference(DataType dt) { if (DataType.NONE.equals(dt)) return "com.yahoo.document.DataType.NONE"; if (DataType.INT.equals(dt)) return "com.yahoo.document.DataType.INT"; if (DataType.FLOAT.equals(dt)) return "com.yahoo.document.DataType.FLOAT"; @@ -921,7 +914,7 @@ public class DocumentGenMojo extends AbstractMojo { return searches; } - private String upperCaseFirstChar(String s) { + private static String upperCaseFirstChar(String s) { return s.substring(0, 1).toUpperCase()+s.substring(1, s.length()); } } |