private void addGetMetdataMethod(JavaClassSource command) { MethodSource<JavaClassSource> method = command.addMethod() .setPublic() .setName("getMetadata").setReturnType(UICommandMetadata.class) .setParameters("UIContext context"); method.addAnnotation(Override.class); StringBuilder body = new StringBuilder() .append("return Metadata.forCommand(") .append(command.getName()).append(".class)").append('\n') .append("\t.name(\"").append(commandName.getValue()) .append("\")"); Iterator<String> iterator = categories.getValue().iterator(); if (iterator.hasNext()) { body.append("\t.category(Categories.create("); while (iterator.hasNext()) { body.append('"').append(iterator.next()).append('"'); if (iterator.hasNext()) body.append(", "); } body.append("))"); } body.append(';'); method.setBody(body.toString()); }
private void addInitializeUIMethod(JavaClassSource command) { command.addMethod() .setPublic() .setName("initializeUI") .setReturnTypeVoid() .setBody("// not implemented") .setParameters("UIBuilder builder") .addThrows(Exception.class) .addAnnotation(Override.class); }
private void addNextMethod(JavaClassSource command) { MethodSource<JavaClassSource> method = command.addMethod().setPublic() .setName("next") .setReturnType(NavigationResult.class) .setParameters("UINavigationContext context"); method.addThrows(Exception.class).addAnnotation(Override.class); try { final String bodyTemplate = loadTextResource( Boolean.TRUE.equals(requireProject.getValue()) ? "nextMethodBodyP.txt" : "nextMethodBody.txt"); method.setBody(String.format(bodyTemplate, command.getName(), script.getValue().getName())); } catch (IOException e) { method.setBody("// ERROR READING BODY TEMPLATE"); } }
private void addExecuteMethod(JavaClassSource command) { command.addMethod() .setPublic() .setName("execute") .setReturnType(Result.class) .setParameters("UIExecutionContext context") .setBody( "return Results.success(\"Command '" + commandName.getValue() + "' successfully executed!\");") .addThrows(Exception.class) .addAnnotation(Override.class); }
@Override public MethodSource<O> createMutator() { Assert.isTrue(getMutator() == null, "Mutator method already exists"); final String mutatorName = methodName("set", name); final String parameters = String.format("%s %s", typeName(), getName()); final MethodSource<O> result = getOrigin().addMethod().setReturnTypeVoid().setName(mutatorName) .setParameters(parameters); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("this.%1$s = %1$s;", getName()); result.setBody(body); } } return result; }
@Override public MethodSource<O> createMutator() { Assert.isTrue(getMutator() == null, "Mutator method already exists"); final String mutatorName = methodName("set", name); final String parameters = String.format("%s %s", typeName(), getName()); final MethodSource<O> result = getOrigin().addMethod().setReturnTypeVoid().setName(mutatorName) .setParameters(parameters); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("this.%1$s = %1$s;", getName()); result.setBody(body); } } return result; }
@Override public MethodSource<O> createMutator() { Assert.isTrue(getMutator() == null, "Mutator method already exists"); final String mutatorName = methodName("set", name); final String parameters = String.format("%s %s", typeName(), getName()); final MethodSource<O> result = getOrigin().addMethod().setReturnTypeVoid().setName(mutatorName) .setParameters(parameters); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("this.%1$s = %1$s;", getName()); result.setBody(body); } } return result; }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
public static JavaClassSource createGreetingClass(JavaClassSource current) { JavaClassSource source = Roaster.create(JavaClassSource.class).setName("Greeting").setPackage(current.getPackage()); source.addMethod().setPublic().setConstructor(true).setBody("this.id = 0;this.content = null;"); source.addMethod().setPublic().setConstructor(true).setParameters("long id, String content").setBody("this.id = id; this.content = content;"); source.addProperty(String.class, "content"); source.addProperty("long", "id"); Roaster.format(source.toString()); return source; }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
.setName("getMetadata") .setReturnType(UICommandMetadata.class) .setParameters("UIContext context"); getMetadataMethod.addAnnotation(Override.class); .setReturnTypeVoid() .setBody("// not implemented") .setParameters("UIBuilder builder") .addThrows(Exception.class) .addAnnotation(Override.class); .setName("execute") .setReturnType(Result.class) .setParameters("UIExecutionContext context") .setBody("return Results.success(\"Command '" + commandName.getValue() + "' successfully executed!\");") .addThrows(Exception.class)
@Override public PropertySource<O> setType(String type) { final MethodSource<O> accessor = getAccessor(); final MethodSource<O> mutator = getMutator(); final FieldSource<O> field = getField(); if (accessor != null) { final Type<O> originalType = accessor.getReturnType(); accessor.setReturnType(type); if (originalType.isType(boolean.class) || accessor.getReturnType().isType(boolean.class)) { // potential name change: final String accessorName = methodName(accessor.getReturnType().isType(boolean.class) ? "is" : "get", getName()); accessor.setName(accessorName); } } if (mutator != null) { mutator.setParameters(String.format("%s %s", type, getName())); } if (field != null) { field.setType(type); } return this; }
private void addMethod(JavaClassSource javaClassSource, org.kie.workbench.common.services.datamodeller.core.Method method, ClassTypeResolver classTypeResolver) throws ClassNotFoundException { MethodSource<JavaClassSource> methodSource = javaClassSource.addMethod(); methodSource.setName(method.getName()); methodSource.setReturnType(buildMethodReturnTypeString(method.getReturnType(), classTypeResolver)); methodSource.setParameters(buildMethodParameterString(method.getParameters(), classTypeResolver)); methodSource.setBody(method.getBody()); methodSource.setVisibility(buildVisibility(method.getVisibilty())); for (Annotation annotation : method.getAnnotations()) { addAnnotation(methodSource, annotation); } }