@Test public void addGenericSuperTypeWithoutPackage() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.setSuperType("Bar<T>"); Assert.assertTrue(javaClass.toString().contains("Bar<T>")); Assert.assertNull(javaClass.getImport("it.coopservice.test.Bar")); }
@Test public void addMultipleGenerics() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable().setName("I"); javaClass.addTypeVariable().setName("O"); Assert.assertTrue(javaClass.toString().contains("<I, O>")); javaClass.removeTypeVariable("I"); Assert.assertTrue(javaClass.toString().contains("<O>")); }
@Test public void addAndRemoveGenericType() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable().setName("T"); Assert.assertTrue(javaClass.getTypeVariables().get(0).getBounds().isEmpty()); Assert.assertTrue(javaClass.toString().contains("<T>")); javaClass.removeTypeVariable("T"); Assert.assertFalse(javaClass.toString().contains("<T>")); }
@Test public void classTypeVariableBounds() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable().setName("T").setBounds(CharSequence.class); Assert.assertTrue(javaClass.toString().contains("<T extends CharSequence>")); javaClass.getTypeVariable("T").setBounds(CharSequence.class, Serializable.class); Assert.assertTrue(javaClass.toString().contains("<T extends CharSequence & Serializable>")); javaClass.getTypeVariable("T").removeBounds(); Assert.assertTrue(javaClass.toString().contains("<T>")); }
@Test public void addMultipleConcreteGenericSuperTypeWithPackage() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.setSuperType( "it.coopservice.test.Bar<com.coopservice.test.MyConcreteSuperClass,com.coopservice.test.MyOtherClass>"); Assert.assertTrue( javaClass.toString().contains("extends Bar<MyConcreteSuperClass, MyOtherClass>")); Assert.assertNotNull(javaClass.getImport("it.coopservice.test.Bar")); Assert.assertNotNull(javaClass.getImport("com.coopservice.test.MyConcreteSuperClass")); Assert.assertNotNull(javaClass.getImport("com.coopservice.test.MyOtherClass")); }
@Test public void stringTypeVariableBounds() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable().setName("T").setBounds("com.something.Foo"); Assert.assertTrue(javaClass.toString().contains("<T extends com.something.Foo>")); javaClass.getTypeVariable("T").setBounds("com.something.Foo", "com.something.Bar<T>"); Assert.assertTrue( javaClass.toString().contains("<T extends com.something.Foo & com.something.Bar<T>>")); javaClass.getTypeVariable("T").removeBounds(); Assert.assertTrue(javaClass.toString().contains("<T>")); }
@Test public void getClassGenericsName() throws ClassNotFoundException { JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable("I"); javaClass.addTypeVariable("O"); List<TypeVariableSource<JavaClassSource>> typeVariables = javaClass.getTypeVariables(); Assert.assertNotNull(typeVariables); Assert.assertEquals(2, typeVariables.size()); Assert.assertEquals("I", typeVariables.get(0).getName()); Assert.assertTrue(typeVariables.get(0).getBounds().isEmpty()); Assert.assertEquals("O", typeVariables.get(1).getName()); Assert.assertTrue(typeVariables.get(1).getBounds().isEmpty()); }
@Test public void javaTypeTypeVariableBounds() throws ClassNotFoundException { JavaInterface<?> foo = Roaster.create(JavaInterfaceSource.class).setPackage("it.coopservice.test").setName("Foo"); JavaClassSource javaClass = Roaster.create(JavaClassSource.class); javaClass.setPackage("it.coopservice.test"); javaClass.setName("SimpleClass"); javaClass.addTypeVariable().setName("T").setBounds(foo); Assert.assertTrue(javaClass.toString().contains("<T extends Foo>")); JavaInterface<?> bar = Roaster.create(JavaInterfaceSource.class).setPackage("it.coopservice.test").setName("Bar"); javaClass.getTypeVariable("T").setBounds(foo, bar); Assert.assertTrue(javaClass.toString().contains("<T extends Foo & Bar>")); javaClass.getTypeVariable("T").removeBounds(); Assert.assertTrue(javaClass.toString().contains("<T>")); }
@SuppressWarnings({"unchecked", "rawtypes"}) private List<Resource<?>> generateFromEntity( String targetDir, final Resource<?> template, final JavaClassSource entity) { resetMetaWidgets(); // Track the list of resources generated List<Resource<?>> result = new ArrayList<>(); try { JavaSourceFacet java = this.project.getFacet(JavaSourceFacet.class); WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class); JPAFacet<PersistenceCommonDescriptor> jpa = this.project.getFacet(JPAFacet.class); loadTemplates(); Map<Object, Object> context = CollectionUtils.newHashMap(); context.put("entity", entity); String ccEntity = StringUtils.decapitalize(entity.getName()); context.put("ccEntity", ccEntity); context.put("rmEntity", ccEntity + "ToDelete"); setPrimaryKeyMetaData(context, entity); // Prepare qbeMetawidget this.qbeMetawidget.setPath(entity.getQualifiedName()); StringWriter stringWriter = new StringWriter(); this.qbeMetawidget.write(stringWriter, this.backingBeanTemplateQbeMetawidgetIndent); context.put("qbeMetawidget", stringWriter.toString().trim()); // Prepare removeEntityMetawidget this.rmEntityMetawidget.setPath(entity.getQualifiedName()); stringWriter = new StringWriter(); this.rmEntityMetawidget.write(stringWriter, this.backingBeanTemplateRmEntityMetawidgetIndent); context.put("rmEntityMetawidget", stringWriter.toString().trim()); // Prepare Java imports Set<String> qbeMetawidgetImports = this.qbeMetawidget.getImports(); Set<String> rmEntityMetawidgetImports = this.rmEntityMetawidget.getImports(); Set<String> metawidgetImports = CollectionUtils.newHashSet(); metawidgetImports.addAll(qbeMetawidgetImports); metawidgetImports.addAll(rmEntityMetawidgetImports); metawidgetImports.remove(entity.getQualifiedName()); context.put( "metawidgetImports", CollectionUtils.toString(metawidgetImports, ";\r\nimport ", true, false)); // Prepare JPA Persistence Unit context.put("persistenceUnitName", jpa.getConfig().getOrCreatePersistenceUnit().getName()); // Create the Backing Bean for this entity JavaClassSource viewBean = Roaster.parse( JavaClassSource.class, FreemarkerTemplateProcessor.processTemplate(context, this.backingBeanTemplate)); viewBean.setPackage(java.getBasePackage() + "." + DEFAULT_FACES_PACKAGE); result.add( ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewBean), viewBean.toString())); // Set new context for view generation context = getTemplateContext(targetDir, template); String beanName = StringUtils.decapitalize(viewBean.getName()); context.put("beanName", beanName); context.put("ccEntity", ccEntity); context.put("entityName", StringUtils.uncamelCase(entity.getName())); setPrimaryKeyMetaData(context, entity); // Prepare entityMetawidget this.entityMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + "." + ccEntity)); this.entityMetawidget.setPath(entity.getQualifiedName()); this.entityMetawidget.setReadOnly(false); this.entityMetawidget.setStyle(null); // Generate create writeEntityMetawidget( context, this.createTemplateEntityMetawidgetIndent, this.createTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/create.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.createTemplate))); // Generate view this.entityMetawidget.setReadOnly(true); writeEntityMetawidget( context, this.viewTemplateEntityMetawidgetIndent, this.viewTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/view.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.viewTemplate))); // Generate search this.searchMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".example")); this.searchMetawidget.setPath(entity.getQualifiedName()); this.beanMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".pageItems")); this.beanMetawidget.setPath(viewBean.getQualifiedName() + "/pageItems"); writeSearchAndBeanMetawidget( context, this.searchTemplateSearchMetawidgetIndent, this.searchTemplateBeanMetawidgetIndent, this.searchTemplateNamespaces); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource(targetDir + "/" + ccEntity + "/search.xhtml"), FreemarkerTemplateProcessor.processTemplate(context, this.searchTemplate))); // Generate navigation result.add(generateNavigation(targetDir)); // Need ViewUtils and forge.taglib.xml for forgeview:asList JavaClassSource viewUtils = Roaster.parse( JavaClassSource.class, FreemarkerTemplateProcessor.processTemplate(context, this.viewUtilsTemplate)); viewUtils.setPackage(viewBean.getPackage()); result.add( ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewUtils), viewUtils.toString())); context.put("viewPackage", viewBean.getPackage()); result.add( ScaffoldUtil.createOrOverwrite( web.getWebResource("WEB-INF/classes/META-INF/forge.taglib.xml"), FreemarkerTemplateProcessor.processTemplate(context, this.taglibTemplate))); createInitializers(entity); } catch (Exception e) { throw new RuntimeException("Error generating default scaffolding: " + e.getMessage(), e); } return result; }