@Test public void testLoadedInterfaces() throws Exception { String[] namesToLoad = new String[] { "InterfaceA.json", "InterfaceB.json", "ABImpl.json", }; List<ObjectSchema> schemaList = new ArrayList<ObjectSchema>(); for (String name : namesToLoad) { String fileString = FileUtils.loadFileAsStringFromClasspath( PojoGeneratorDriverTest.class.getClassLoader(), name); ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(fileString)); // schema.setName(name); schema.setId(schema.getName()); schemaList.add(schema); } JCodeModel codeModel = new JCodeModel(); driver.createAllClasses(codeModel, schemaList); // Get the class JPackage _package = codeModel._package(""); JDefinedClass impl = null; try { impl = _package._class("ABImpl"); } catch (JClassAlreadyExistsException e) { impl = e.getExistingClass(); } String classString = declareToString(impl); // System.out.println(classString); Iterator<JClass> it = impl._implements(); assertNotNull(it); String intA = "InterfaceA"; String intB = "InterfaceB"; String jsonEntity = "JSONEntity"; Map<String, JClass> map = new HashMap<String, JClass>(); while (it.hasNext()) { JClass impClass = it.next(); if (intA.equals(impClass.name())) { map.put(intA, impClass); } else if (intB.equals(impClass.name())) { map.put(intB, impClass); } else if (jsonEntity.equals(impClass.name())) { map.put(jsonEntity, impClass); } } assertEquals("Should have implemented two interfaces", 3, map.size()); // Now get the fields from the object an confirm they are all there Map<String, JFieldVar> fields = impl.fields(); assertNotNull(fields); assertEquals(6, fields.size()); assertNotNull(fields.get("fromInterfaceA")); assertNotNull(fields.get("alsoFromInterfaceB")); assertNotNull(fields.get("fromMe")); }
public static void addEventHandlerRegistrationMethods(Option option, JDefinedClass jClass) { List<JClass> list = EventRegistry.INSTANCE.getRegistry().get(option.getFullname()); if (list != null) { for (JClass handlerClass : list) { String handlerClassName = handlerClass.name(); String paramName = handlerClassName.substring(0, 1).toLowerCase() + handlerClassName.substring(1); jClass .method(JMod.NONE, void.class, EventHelper.ADD_HANDLER_METHOD_PREFIX + handlerClassName) .param(handlerClass, paramName); } } }