protected void addBaseViewFieldsAndMethodImpls( SourceGenerationContext context, JavaClassSource viewClass) { StringBuffer inputNames = new StringBuffer(); StringBuffer readOnlyMethodSrc = new StringBuffer(); for (FieldDefinition fieldDefinition : context.getFormDefinition().getFields()) { if ((fieldDefinition.isAnnotatedId() && !displaysId()) || isBanned(fieldDefinition)) continue; InputCreatorHelper helper = creatorHelpers.get(fieldDefinition.getCode()); if (helper == null) continue; PropertySource<JavaClassSource> property = viewClass.addProperty(getWidgetFromHelper(helper), fieldDefinition.getName()); FieldSource<JavaClassSource> field = property.getField(); field.setPrivate(); initializeProperty(helper, context, fieldDefinition, field); if (helper instanceof RequiresCustomCode) ((RequiresCustomCode) helper).addCustomCode(fieldDefinition, context, viewClass); if (!(fieldDefinition instanceof SubFormFieldDefinition)) { field .addAnnotation(ERRAI_BOUND) .setStringValue("property", fieldDefinition.getBindingExpression()); } field.addAnnotation(ERRAI_DATAFIELD); property.removeAccessor(); property.removeMutator(); inputNames.append("inputNames.add(\"").append(fieldDefinition.getName()).append("\");"); readOnlyMethodSrc.append(helper.getReadonlyMethod(fieldDefinition.getName(), READONLY_PARAM)); } viewClass .addMethod() .setName("initInputNames") .setBody(inputNames.toString()) .setPublic() .setReturnTypeVoid() .addAnnotation(JAVA_LANG_OVERRIDE); if (isEditable()) { MethodSource<JavaClassSource> readonlyMethod = viewClass .addMethod() .setName("setReadOnly") .setBody(readOnlyMethodSrc.toString()) .setPublic() .setReturnTypeVoid(); readonlyMethod.addParameter(boolean.class, SourceGenerationUtil.READONLY_PARAM); readonlyMethod.addAnnotation(JAVA_LANG_OVERRIDE); } }
@Override public Result execute(UIExecutionContext context) throws Exception { JavaResource javaResource = targetClass.getValue(); JavaClassSource targetClass = javaResource.getJavaType(); GetSetMethodGenerator generator; if (builderPattern.getValue()) { generator = new BuilderGetSetMethodGenerator(); } else { generator = new DefaultGetSetMethodGenerator(); } List<PropertySource<JavaClassSource>> selectedProperties = new ArrayList<>(); if (properties == null || properties.getValue() == null) { return Results.fail("No properties were selected"); } for (String selectedProperty : properties.getValue()) { selectedProperties.add(targetClass.getProperty(selectedProperty)); } for (PropertySource<JavaClassSource> property : selectedProperties) { MethodSource<JavaClassSource> accessor = targetClass.getMethod("get" + Strings.capitalize(property.getName())); if (accessor == null) { generator.createAccessor(property); } else { if (!generator.isCorrectAccessor(accessor, property)) { if (promptToFixMethod(context, accessor.getName(), property.getName())) { targetClass.removeMethod(accessor); generator.createMutator(property); } } } String mutatorMethodName = "set" + Strings.capitalize(property.getName()); String mutatorMethodParameter = property.getType().getName(); MethodSource<JavaClassSource> mutator = targetClass.getMethod(mutatorMethodName, mutatorMethodParameter); if (mutator == null) { generator.createMutator(property); } else { if (!generator.isCorrectMutator(mutator, property)) { if (promptToFixMethod(context, mutator.getName(), property.getName())) { targetClass.removeMethod(mutator); generator.createMutator(property); } } } } setCurrentWorkingResource(context, targetClass); return Results.success("Mutators and accessors were generated successfully"); }
protected void createInitializers(final JavaClassSource entity) throws FacetNotFoundException, FileNotFoundException { boolean dirtyBit = false; for (FieldSource<JavaClassSource> field : entity.getFields()) { if (field.hasAnnotation(OneToOne.class)) { AnnotationSource<JavaClassSource> oneToOne = field.getAnnotation(OneToOne.class); if (oneToOne.getStringValue("mappedBy") == null && oneToOne.getStringValue("cascade") == null) { oneToOne.setEnumValue("cascade", CascadeType.ALL); dirtyBit = true; } String methodName = "new" + StringUtils.capitalize(field.getName()); if (!entity.hasMethodSignature(methodName)) { entity .addMethod() .setName(methodName) .setReturnTypeVoid() .setPublic() .setBody("this." + field.getName() + " = new " + field.getType().getName() + "();"); dirtyBit = true; } } } for (MethodSource<JavaClassSource> method : entity.getMethods()) { if (method.hasAnnotation(OneToOne.class)) { AnnotationSource<JavaClassSource> oneToOne = method.getAnnotation(OneToOne.class); if (oneToOne.getStringValue("mappedBy") == null && oneToOne.getStringValue("cascade") == null) { oneToOne.setEnumValue("cascade", CascadeType.ALL); dirtyBit = true; } String fieldName = StringUtils.camelCase(method.getName().substring(3)); String methodName = "new" + StringUtils.capitalize(fieldName); if (!entity.hasMethodSignature(methodName)) { entity .addMethod() .setName(methodName) .setReturnTypeVoid() .setPublic() .setBody("this." + fieldName + " = new " + method.getReturnType().getName() + "();"); dirtyBit = true; } } } if (dirtyBit) { this.project.getFacet(JavaSourceFacet.class).saveJavaSource(entity); } }
private void setPrimaryKeyMetaData(Map<Object, Object> context, final JavaClassSource entity) { String pkName = "id"; String pkType = "Long"; String nullablePkType = "Long"; for (MemberSource<JavaClassSource, ?> m : entity.getMembers()) { if (m.hasAnnotation(Id.class)) { if (m instanceof Field) { Field<?> field = (Field<?>) m; pkName = field.getName(); pkType = field.getType().getQualifiedName(); nullablePkType = pkType; break; } MethodSource<?> method = (MethodSource<?>) m; pkName = method.getName().substring(3); if (method.getName().startsWith("get")) { pkType = method.getReturnType().getQualifiedName(); } else { pkType = method.getParameters().get(0).getType().getQualifiedName(); } nullablePkType = pkType; break; } } if (Types.isJavaLang(pkType)) { nullablePkType = Types.toSimpleName(pkType); } else if ("int".equals(pkType)) { nullablePkType = Integer.class.getSimpleName(); } else if ("short".equals(pkType)) { nullablePkType = Short.class.getSimpleName(); } else if ("byte".equals(pkType)) { nullablePkType = Byte.class.getSimpleName(); } else if ("long".equals(pkType)) { nullablePkType = Long.class.getSimpleName(); } context.put("primaryKey", pkName); context.put("primaryKeyCC", StringUtils.capitalize(pkName)); context.put("primaryKeyType", pkType); context.put("nullablePrimaryKeyType", nullablePkType); }
@Test public void testJavaDocMethod() throws Exception { String text = "public class MyClass {" + "/**" + LINE_SEPARATOR + " * Do Something" + LINE_SEPARATOR + " * @author George Gastaldi" + LINE_SEPARATOR + " */" + LINE_SEPARATOR + "" + "private void foo(){};}"; JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, text); Assert.assertFalse(javaClass.hasJavaDoc()); Assert.assertEquals(1, javaClass.getMethods().size()); MethodSource<JavaClassSource> method = javaClass.getMethods().get(0); Assert.assertTrue(method.hasJavaDoc()); JavaDocSource<MethodSource<JavaClassSource>> javaDoc = method.getJavaDoc(); String expected = "Do Something" + LINE_SEPARATOR + "@author George Gastaldi"; Assert.assertEquals(expected, javaDoc.getFullText()); }