X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fjava%2Fapi%2Fgenerator%2FGeneratorUtil.java;h=5b3b17dbf6c73334fe471dd3e797f8a18e228749;hb=ff1b4a79cca00743a00c3b0b1100bd0ab2b2fb31;hp=b85e2b0ed7ed78f1a43c23a2bf207f8c56df5b0e;hpb=699f2b3912b8cfae054c08f6f9af7e0061afce57;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java index b85e2b0ed7..5b3b17dbf6 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java @@ -26,8 +26,9 @@ import org.opendaylight.controller.sal.binding.model.api.MethodSignature; import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter; import org.opendaylight.controller.sal.binding.model.api.ParameterizedType; import org.opendaylight.controller.sal.binding.model.api.Type; +import org.opendaylight.controller.sal.binding.model.api.WildcardType; -public class GeneratorUtil { +public final class GeneratorUtil { private GeneratorUtil() { } @@ -35,15 +36,17 @@ public class GeneratorUtil { public static String createIfcDeclaration(final GeneratedType genType, final String indent, final Map> availableImports) { - return createFileDeclaration(IFC, genType, indent, availableImports); + return createFileDeclaration(IFC, genType, indent, availableImports, + false); } public static String createClassDeclaration( final GeneratedTransferObject genTransferObject, final String indent, - final Map> availableImports) { + final Map> availableImports, + boolean isIdentity) { return createFileDeclaration(CLASS, genTransferObject, indent, - availableImports); + availableImports, isIdentity); } public static String createPackageDeclaration(final String packageName) { @@ -52,7 +55,8 @@ public class GeneratorUtil { private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent, - final Map> availableImports) { + final Map> availableImports, + boolean isIdentity) { final StringBuilder builder = new StringBuilder(); final String currentPkg = genType.getPackageName(); @@ -63,14 +67,25 @@ public class GeneratorUtil { appendAnnotations(builder, annotations); builder.append(NL); } - builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP); + + if (isIdentity) { + if (!(CLASS.equals(type))) { + throw new IllegalArgumentException( + "'identity' has to be generated as a class"); + } + builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP + + genType.getName() + GAP); + } else { + builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP); + } if (genType instanceof GeneratedTransferObject) { GeneratedTransferObject genTO = (GeneratedTransferObject) genType; if (genTO.getExtends() != null) { builder.append(EXTENDS + GAP); - builder.append(genTO.getExtends() + GAP); + String gtoString = getExplicitType(genTO.getExtends(), availableImports, currentPkg); + builder.append(gtoString + GAP); } } @@ -168,7 +183,6 @@ public class GeneratorUtil { Map> availableImports, final String currentPkg) { final StringBuilder builder = new StringBuilder(); - builder.append(indent); if (!property.getAnnotations().isEmpty()) { final List annotations = property.getAnnotations(); appendAnnotations(builder, annotations); @@ -183,7 +197,7 @@ public class GeneratorUtil { /** * Create method declaration in interface. - * + * * @param method * @param indent * @return @@ -244,7 +258,8 @@ public class GeneratorUtil { public static String createConstructor( GeneratedTransferObject genTransferObject, final String indent, - Map> availableImports) { + Map> availableImports, + boolean isIdentity) { final StringBuilder builder = new StringBuilder(); final String currentPkg = genTransferObject.getPackageName(); @@ -258,7 +273,7 @@ public class GeneratorUtil { } builder.append(indent); - builder.append(PUBLIC); + builder.append(isIdentity ? PROTECTED : PUBLIC); builder.append(GAP); builder.append(genTransferObject.getName()); builder.append(LB); @@ -438,7 +453,7 @@ public class GeneratorUtil { builder.append(NL); builder.append(indent); builder.append(TAB); - builder.append("builder.append(\", "); + builder.append("builder.append("); builder.append(property.getName()); builder.append(");"); } @@ -461,20 +476,37 @@ public class GeneratorUtil { public static String createEnum(final Enumeration enumeration, final String indent) { - final StringBuilder builder = new StringBuilder(indent + ENUM + GAP - + enumeration.getName() + GAP + LCB + NL); + if (enumeration == null || indent == null) + throw new IllegalArgumentException(); + final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP + + ENUM + GAP + enumeration.getName() + GAP + LCB + NL); - String separator = COMMA; + String separator = COMMA + NL; final List values = enumeration.getValues(); - builder.append(indent + TAB); + for (int i = 0; i < values.size(); i++) { if (i + 1 == values.size()) { separator = SC; } - builder.append(values.get(i).getName() + separator); + builder.append(indent + TAB + values.get(i).getName() + LB + + values.get(i).getValue() + RB + separator); } builder.append(NL); + builder.append(NL); + final String ENUMERATION_NAME = "value"; + final String ENUMERATION_TYPE = "int"; + builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + + SC); + builder.append(NL); + builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() + + LB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + RB + GAP + + LCB + NL); + builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP + + "=" + GAP + ENUMERATION_NAME + SC + NL); + builder.append(indent + TAB + RCB + NL); + builder.append(indent + RCB); + builder.append(NL); return builder.toString(); } @@ -511,7 +543,12 @@ public class GeneratorUtil { if (packageName.startsWith("java.lang")) { builder.append(type.getName()); } else { - builder.append(packageName + "." + type.getName()); + if (!packageName.isEmpty()) { + builder.append(packageName + "." + type.getName()); + } else { + builder.append(type.getName()); + } + } if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; @@ -539,7 +576,13 @@ public class GeneratorUtil { if (i + 1 == pTypes.length) { separator = ""; } - builder.append(getExplicitType(t, availableImports, currentPkg) + + String wildcardParam = ""; + if(t instanceof WildcardType) { + wildcardParam = "? extends "; + } + + builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) + separator); } return builder.toString(); @@ -556,6 +599,7 @@ public class GeneratorUtil { if (currentValue > maxValue) { result.clear(); result.add(entry.getKey()); + maxValue = currentValue; } else if (currentValue == maxValue) { result.add(entry.getKey()); } @@ -628,7 +672,8 @@ public class GeneratorUtil { String genTypePkg) { String typeName = type.getName(); String typePkg = type.getPackageName(); - if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg)) { + if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) || + typePkg.isEmpty()) { return; } LinkedHashMap packages = importedTypes.get(typeName);