@Override public O implementInterface(Class<?> type) { O obj = addInterface(type); for (MethodSource<?> methodSource : Methods.implementAbstractMethods(type, this)) { methodSource.setPublic().addAnnotation(Override.class); } return obj; }
@Override public O implementInterface(Class<?> type) { O obj = addInterface(type); for (MethodSource<?> methodSource : Methods.implementAbstractMethods(type, this)) { methodSource.setPublic().addAnnotation(Override.class); } return obj; }
private JavaClassSource createCommand(JavaClassSource command, String commandName, Iterable<String> categories) { MethodSource<JavaClassSource> commandMethod = command.addMethod().setName(toMethodName(command, commandName)) .setReturnType(String.class) .setBody(String.format("return \"Command %s executed\";", commandName)).setPublic(); AnnotationSource<JavaClassSource> commandAnn = commandMethod.addAnnotation(Command.class); List<String> categoryList = new ArrayList<>(Lists.toList(categories)); commandAnn.setStringValue("value", commandName); if (!categoryList.isEmpty()) { commandAnn.setStringArrayValue("categories", categoryList.toArray(new String[categoryList.size()])); } // build the thing return command; }
@Override public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source) throws Exception { source.addImport("org.wildfly.swarm.Swarm"); MethodSource<JavaClassSource> method = source.addMethod() .setPublic() .setStatic(true) .setReturnTypeVoid() .setName("main") .addThrows(Exception.class); method.addParameter("String[]", "args"); StringBuilder body = new StringBuilder(); body.append("Swarm swarm = new Swarm();").append(System.lineSeparator()); body.append("swarm.start();").append(System.lineSeparator()); body.append("swarm.deploy();"); method.setBody(body.toString()); WildFlySwarmFacet facet = project.getFacet(WildFlySwarmFacet.class); WildFlySwarmConfigurationBuilder newConfig = WildFlySwarmConfigurationBuilder.create(facet.getConfiguration()); newConfig.mainClass(source.getQualifiedName()); facet.setConfiguration(newConfig); return source; }
private void writeMapRemove(final JavaClassSource javaClass, final Property propDef) { final String name = "remove" + propDef.getPropertyName(); if (!javaClass.hasMethodSignature(name)) { final StringBuilder javadoc = new StringBuilder(); javadoc .append("@return the pSourcePath\n"); String body = ""; javaClass.addImport("javafx.collections.FXCollections"); body = Templates.use(TemplateName.PropertyRemoveMap, propDef); final MethodSource<?> method = javaClass.addMethod() .setName(name) .setPublic() .setBody(body.toString()) .setReturnType(javaClass.getTypeVariables().isEmpty() ? javaClass.getName() : javaClass.getTypeVariables().get(0).getName()); method.getJavaDoc().setFullText(javadoc.toString()); } else { // javaClass.getMethod(propDef.getName()).setBody(javaClass.getMethod(propDef.getName()).getBody() // + body.toString()); } }
private void writeMapPut(final JavaClassSource javaClass, final Property propDef) { final String name = "put" + propDef.getPropertyName(); if (!javaClass.hasMethodSignature(name)) { final StringBuilder javadoc = new StringBuilder(); javadoc .append("@return the pSourcePath\n"); String body = ""; javaClass.addImport("javafx.collections.FXCollections"); body = Templates.use(TemplateName.PropertyPutMap, propDef); final MethodSource<?> method = javaClass.addMethod() .setName(name) .setPublic() .setBody(body.toString()) .setReturnType(javaClass.getTypeVariables().isEmpty() ? javaClass.getName() : javaClass.getTypeVariables().get(0).getName()); method.getJavaDoc().setFullText(javadoc.toString()); } else { // javaClass.getMethod(propDef.getName()).setBody(javaClass.getMethod(propDef.getName()).getBody() // + body.toString()); } }
private void writeCreator(final JavaClassSource javaClass, final Class beanDef) { if (!beanDef.isAbstract() && !javaClass.hasMethodSignature("create")) { final StringBuilder javadoc = new StringBuilder(); javadoc .append("Build a new instance of {@link ").append(beanDef.name()).append("}."); final String body = Templates.use(TemplateName.Creator, beanDef); final MethodSource<?> method = javaClass.addMethod() .setName("of") .setPublic() .setStatic(true) .setBody(body) .setReturnType(beanDef.qualifiedName()); method.getJavaDoc().setFullText(javadoc.toString()) .addTagValue("@return", "a fresh instance"); } else { // javaClass.getMethod(propDef.getName()).setBody(javaClass.getMethod(propDef.getName()).getBody() // + body.toString()); } }
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; }
private void addTestMethod(JavaClassSource test) { test.addImport("org.junit.Test"); MethodSource<JavaClassSource> testMethod = test.addMethod() .setPublic() .setReturnTypeVoid() .setName("should_start_service") .setBody(""); testMethod.addAnnotation("Test"); }
/** * 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 + ";"); } }
private void addInitializeUIMethod(JavaClassSource command) { command.addMethod() .setPublic() .setName("initializeUI") .setReturnTypeVoid() .setBody("// not implemented") .setParameters("UIBuilder builder") .addThrows(Exception.class) .addAnnotation(Override.class); }
/** * 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 + ";"); } }
private void writeListAdd(final JavaClassSource javaClass, final Property propDef) { final String name = "add" + propDef.getUpperName(); if (!javaClass.hasMethodSignature(name)) { final StringBuilder javadoc = new StringBuilder(); javadoc .append("@return the pSourcePath\n"); String body = ""; javaClass.addImport("javafx.collections.FXCollections"); javaClass.addImport("java.util.Arrays"); body = Templates.use(TemplateName.PropertyAddList, propDef); final MethodSource<?> method = javaClass.addMethod() .setName(name) .setPublic() .setBody(body.toString()) .setReturnType(javaClass.getTypeVariables().isEmpty() ? javaClass.getName() : javaClass.getTypeVariables().get(0).getName()); method.addParameter(propDef.subtype(), propDef.name()).setVarArgs(true); method.getJavaDoc().setFullText(javadoc.toString()); } else { // javaClass.getMethod(propDef.getName()).setBody(javaClass.getMethod(propDef.getName()).getBody() // + body.toString()); } }
private void writeListRemove(final JavaClassSource javaClass, final Property propDef) { final String name = "remove" + propDef.getUpperName(); if (!javaClass.hasMethodSignature(name)) { final StringBuilder javadoc = new StringBuilder(); javadoc .append("@return the pSourcePath\n"); String body = ""; javaClass.addImport("javafx.collections.FXCollections"); javaClass.addImport("java.util.Arrays"); body = Templates.use(TemplateName.PropertyRemoveList, propDef); final MethodSource<?> method = javaClass.addMethod() .setName(name) .setPublic() .setBody(body.toString()) .setReturnType(javaClass.getTypeVariables().isEmpty() ? javaClass.getName() : javaClass.getTypeVariables().get(0).getName()); method.addParameter(propDef.subtype(), propDef.name()).setVarArgs(true); method.getJavaDoc().setFullText(javadoc.toString()); } else { // javaClass.getMethod(propDef.getName()).setBody(javaClass.getMethod(propDef.getName()).getBody() // + body.toString()); } }
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"); } }
protected void addGet(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Constructed instance of " + plan.getClassName() + " resource") .addTagValue("@return", "The instance"); method.setPublic() .setName("get") .setReturnType(plan.getClassName()); }
protected void addGet(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Constructed instance of " + plan.getClassName() + " resource") .addTagValue("@return", "The instance"); method.setPublic() .setName("get") .setReturnType(plan.getClassName()); }
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); }
protected void addPropertyChangeSupport(JavaClassSource type, ClassPlan plan) { // property change listeners type.addField() .setName("pcs") .setType(PropertyChangeSupport.class) .setPrivate(); final MethodSource<JavaClassSource> listenerAdd = type.addMethod(); listenerAdd.getJavaDoc().setText("Adds a property change listener"); listenerAdd.setPublic() .setName("addPropertyChangeListener") .addParameter(PropertyChangeListener.class, "listener"); listenerAdd.setBody("if(null==this.pcs) this.pcs = new PropertyChangeSupport(this);\n" + "this.pcs.addPropertyChangeListener(listener);"); final MethodSource<JavaClassSource> listenerRemove = type.addMethod(); listenerRemove.getJavaDoc().setText("Removes a property change listener"); listenerRemove.setPublic() .setName("removePropertyChangeListener") .addParameter(PropertyChangeListener.class, "listener"); listenerRemove.setBody("if(this.pcs!=null) this.pcs.removePropertyChangeListener(listener);"); }
protected void addPropertyChangeSupport(JavaClassSource type, ClassPlan plan) { // property change listeners type.addField() .setName("pcs") .setType(PropertyChangeSupport.class) .setPrivate(); final MethodSource<JavaClassSource> listenerAdd = type.addMethod(); listenerAdd.getJavaDoc().setText("Adds a property change listener"); listenerAdd.setPublic() .setName("addPropertyChangeListener") .addParameter(PropertyChangeListener.class, "listener"); listenerAdd.setBody("if(null==this.pcs) this.pcs = new PropertyChangeSupport(this);\n" + "this.pcs.addPropertyChangeListener(listener);"); final MethodSource<JavaClassSource> listenerRemove = type.addMethod(); listenerRemove.getJavaDoc().setText("Removes a property change listener"); listenerRemove.setPublic() .setName("removePropertyChangeListener") .addParameter(PropertyChangeListener.class, "listener"); listenerRemove.setBody("if(this.pcs!=null) this.pcs.removePropertyChangeListener(listener);"); }