Gen. Abstract*ModuleFactory with WORKING handleChangedClass() 50/40950/4
authorMichael Vorburger <vorburger@redhat.com>
Tue, 28 Jun 2016 15:15:26 +0000 (17:15 +0200)
committerTom Pantelis <tpanteli@brocade.com>
Fri, 8 Jul 2016 18:07:33 +0000 (18:07 +0000)
Bug: 2855
Change-Id: I243da5822265db3913f6b0afb2f9393f78b0c24c
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/gofactory/AbsFactoryGeneratedObjectFactory.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/java/GeneratedObjectBuilder.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/java/JavaFileInput.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/java/JavaFileInputBuilder.java

index b37d918..3ee43a6 100644 (file)
@@ -63,6 +63,10 @@ public class AbsFactoryGeneratedObjectFactory {
                                              FullyQualifiedName moduleFQN,
                                              List<Field> moduleFields) {
         JavaFileInputBuilder b = new JavaFileInputBuilder();
+
+        b.addImportFQN(new FullyQualifiedName(Module.class));
+        b.addImportFQN(new FullyQualifiedName(ModuleIdentifier.class));
+
         Annotation moduleQNameAnnotation = Annotation.createModuleQNameANnotation(yangModuleQName);
         b.addClassAnnotation(moduleQNameAnnotation);
 
@@ -110,8 +114,14 @@ public class AbsFactoryGeneratedObjectFactory {
         ));
 
         b.addToBody(format("\n"+
-                "public %s handleChangedClass(%s dependencyResolver, %s old, %s bundleContext) throws Exception {\n"+
-                    "return handleChangedClass(old);\n"+
+                "public %s handleChangedClass(%s dependencyResolver, %s old, %s bundleContext) throws Exception {\n" +
+                    // "// @Deprecated return handleChangedClass(old);\n" +
+                    "String instanceName = old.getModule().getIdentifier().getInstanceName();\n" +
+                    "%1$s newModule = new %1$s(new ModuleIdentifier(NAME, instanceName), dependencyResolver);\n" +
+                    "Module oldModule = old.getModule();\n" +
+                    "Class<? extends Module> oldModuleClass = oldModule.getClass();\n" +
+                    genCodeToCopyAttributes(moduleFields) +
+                    "return newModule;\n" +
                 "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), DynamicMBeanWithInstance.class.getCanonicalName(), BUNDLE_CONTEXT));
 
         b.addToBody(format("\n@Deprecated\n"+
@@ -129,6 +139,15 @@ public class AbsFactoryGeneratedObjectFactory {
         return new GeneratedObjectBuilder(b.build()).toGeneratedObject();
     }
 
+    private String genCodeToCopyAttributes(List<Field> moduleFields) {
+        StringBuilder sb = new StringBuilder("\n");
+        for (Field field : moduleFields) {
+            sb.append(format("newModule.set%1$s( (%2$s) oldModuleClass.getMethod(\"get%1$s\").invoke(oldModule));\n", field.getName(), field.getType()));
+        }
+        sb.append('\n');
+        return sb.toString();
+    }
+
     private static String getCreateModule(FullyQualifiedName moduleFQN, List<Field> moduleFields) {
         String result = "\n"+
             "@Override\n"+
index c2aeb89..13418f3 100644 (file)
@@ -34,6 +34,17 @@ public class GeneratedObjectBuilder {
             content.append(input.getFQN().getPackageName());
             content.append(";\n");
         }
+
+        if (!input.getImports().isEmpty())
+            content.append('\n');
+        for (FullyQualifiedName importedType : input.getImports()) {
+            content.append("import ");
+            content.append(importedType.toString());
+            content.append(";\n");
+        }
+        if (!input.getImports().isEmpty())
+            content.append('\n');
+
         content.append(maybeAddComment(input.getClassJavaDoc(), true));
 
         for (String classAnnotation : input.getClassAnnotations()) {
@@ -64,7 +75,6 @@ public class GeneratedObjectBuilder {
     }
 
     private static String maybeAddComment(Optional<String> comment, boolean isJavadoc) {
-
         if (comment.isPresent()) {
             String input = comment.get();
             return StringUtil.writeComment(input, isJavadoc);
index 588e218..c498c5b 100644 (file)
@@ -26,6 +26,8 @@ public class JavaFileInputBuilder {
 
     private final List<String> classAnnotations = new ArrayList<>();
 
+    private final List<FullyQualifiedName> importFQNs = new ArrayList<>();
+
     private final List<FullyQualifiedName> extendsFQNs = new ArrayList<>();
 
     private final List<FullyQualifiedName> implementsFQNs = new ArrayList<>();
@@ -44,6 +46,10 @@ public class JavaFileInputBuilder {
         classAnnotations.add(checkNotNull(annotation));
     }
 
+    public void addImportFQN(FullyQualifiedName fqn) {
+        importFQNs.add(fqn);
+    }
+
     public void addExtendsFQN(FullyQualifiedName fqn) {
         extendsFQNs.add(fqn);
     }
@@ -129,6 +135,11 @@ public class JavaFileInputBuilder {
                 return header;
             }
 
+            @Override
+            public List<FullyQualifiedName> getImports() {
+                return Collections.unmodifiableList(importFQNs);
+            }
+
             @Override
             public Optional<String> getClassJavaDoc() {
                 return classJavaDoc;
@@ -158,6 +169,7 @@ public class JavaFileInputBuilder {
             public List<String> getBodyElements() {
                 return Collections.unmodifiableList(bodyElements);
             }
+
         };
     }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.