import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.JavaFileInputBuilder;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.TypeName;
import org.opendaylight.yangtools.yang.common.QName;
-import org.osgi.framework.BundleContext;
public class AbsFactoryGeneratedObjectFactory {
+ private static final String BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";
public GeneratedObject toGeneratedObject(ModuleMXBeanEntry mbe, Optional<String> copyright) {
FullyQualifiedName absFactoryFQN = new FullyQualifiedName(mbe.getPackageName(), mbe.getAbstractFactoryName());
"public %s createModule(String instanceName, %s dependencyResolver, %s bundleContext) {\n"+
"return instantiateModule(instanceName, dependencyResolver, bundleContext);\n"+
"}\n",
- Module.class.getCanonicalName(), DependencyResolver.class.getCanonicalName(), BundleContext.class.getCanonicalName()));
+ Module.class.getCanonicalName(), DependencyResolver.class.getCanonicalName(), BUNDLE_CONTEXT));
b.addToBody(getCreateModule(moduleFQN, moduleFields));
"return new %s(new %s(NAME, instanceName), dependencyResolver, oldModule, oldInstance);\n"+
"}\n",
moduleFQN, DependencyResolver.class.getCanonicalName(), moduleFQN, AutoCloseable.class.getCanonicalName(),
- BundleContext.class.getCanonicalName(), moduleFQN, ModuleIdentifier.class.getCanonicalName()));
+ BUNDLE_CONTEXT, moduleFQN, ModuleIdentifier.class.getCanonicalName()));
b.addToBody(format("\n"+
"public %s instantiateModule(String instanceName, %s dependencyResolver, %s bundleContext) {\n"+
"return new %s(new %s(NAME, instanceName), dependencyResolver);\n"+
- "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), BundleContext.class.getCanonicalName(),
+ "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), BUNDLE_CONTEXT,
moduleFQN, ModuleIdentifier.class.getCanonicalName()
));
b.addToBody(format("\n"+
+ "public %s handleChangedClass(%s dependencyResolver, %s old, %s bundleContext) throws Exception {\n"+
+ "return handleChangedClass(old);\n"+
+ "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), DynamicMBeanWithInstance.class.getCanonicalName(), BUNDLE_CONTEXT));
+
+ b.addToBody(format("\n@Deprecated\n"+
"public %s handleChangedClass(%s old) throws Exception {\n"+
"throw new UnsupportedOperationException(\"Class reloading is not supported\");\n"+
"}\n", moduleFQN, DynamicMBeanWithInstance.class.getCanonicalName()));
+ // TODO The generic specifier in HashSet<> isn't necessary, but the Eclipse AST parser used in the unit tests doesn't support this
b.addToBody(format("\n"+
"@Override\n"+
"public java.util.Set<%s> getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory dependencyResolverFactory, %s bundleContext) {\n"+
- "return new java.util.HashSet<%s>();\n"+
- "}\n", moduleFQN, BundleContext.class.getCanonicalName(), moduleFQN));
+ "return new java.util.HashSet<%1$s>();\n"+
+ "}\n", moduleFQN, BUNDLE_CONTEXT));
return new GeneratedObjectBuilder(b.build()).toGeneratedObject();
}
String result = "\n"+
"@Override\n"+
format("public %s createModule(String instanceName, %s dependencyResolver, %s old, %s bundleContext) throws Exception {\n",
- Module.class.getCanonicalName(),DependencyResolver.class.getCanonicalName(),
- DynamicMBeanWithInstance.class.getCanonicalName(),BundleContext.class.getCanonicalName())+
- format("%s oldModule = null;\n",moduleFQN)+
+ Module.class.getCanonicalName(), DependencyResolver.class.getCanonicalName(),
+ DynamicMBeanWithInstance.class.getCanonicalName(), BUNDLE_CONTEXT)+
+ format("%s oldModule;\n",moduleFQN)+
"try {\n"+
- format("oldModule = (%s) old.getModule();\n",moduleFQN)+
+ format("oldModule = (%s) old.getModule();\n", moduleFQN)+
"} catch(Exception e) {\n"+
- "return handleChangedClass(old);\n"+
+ "return handleChangedClass(dependencyResolver, old, bundleContext);\n"+
"}\n"+
format("%s module = instantiateModule(instanceName, dependencyResolver, oldModule, old.getInstance(), bundleContext);\n", moduleFQN);
private static String getServiceIfcsInitialization(List<FullyQualifiedName> providedServices) {
String generic = format("Class<? extends %s>", AbstractServiceInterface.class.getCanonicalName());
- String result = format("static {\n"+
- "java.util.Set<%1$s> serviceIfcs2 = new java.util.HashSet<%1$s>();\n", generic);
+ String result = "static {\n";
+ if (!providedServices.isEmpty()) {
+ // TODO The generic specifier in HashSet<> isn't necessary, but the Eclipse AST parser used in the unit tests doesn't support this
+ result += format("java.util.Set<%1$s> serviceIfcs2 = new java.util.HashSet<%1$s>();\n", generic);
- for(FullyQualifiedName fqn: providedServices) {
- result += format("serviceIfcs2.add(%s.class);\n", fqn);
+ for(FullyQualifiedName fqn: providedServices) {
+ result += format("serviceIfcs2.add(%s.class);\n", fqn);
+ }
+
+ result += "serviceIfcs = java.util.Collections.unmodifiableSet(serviceIfcs2);\n";
+ } else {
+ result += "serviceIfcs = java.util.Collections.emptySet();\n";
}
- result += "serviceIfcs = java.util.Collections.unmodifiableSet(serviceIfcs2);\n"+
- "}\n";
+ result += "}\n";
// add isModuleImplementingServiceInterface and getImplementedServiceIntefaces methods