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; }
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 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()); } }
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"); }
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"); } }
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 addAccept(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Configure a pre-constructed instance of " + plan.getClassName() + " resource") .addTagValue("@parameter", "Instance of " + plan.getClassName() + " to configure") .addTagValue("@return", "nothing"); method.addParameter("T", "value"); method.setName("accept") .setReturnType("void"); }
protected void addAccept(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Configure a pre-constructed instance of " + plan.getClassName() + " resource") .addTagValue("@parameter", "Instance of " + plan.getClassName() + " to configure") .addTagValue("@return", "nothing"); method.addParameter("T", "value"); method.setName("accept") .setReturnType("void"); }
@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; }
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);"); }
@Override public MethodSource<O> createAccessor() { Assert.isTrue(getAccessor() == null, "Accessor method already exists"); final Type<O> type = getType(); final String accessorName = methodName(type.isType(boolean.class) ? "is" : "get", name); final MethodSource<O> result = getOrigin().addMethod().setReturnType(typeName()) .setName(accessorName); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("return %s;", getName()); result.setBody(body); } } return result; }
@Override public MethodSource<O> createAccessor() { Assert.isTrue(getAccessor() == null, "Accessor method already exists"); final Type<O> type = getType(); final String accessorName = methodName(type.isType(boolean.class) ? "is" : "get", name); final MethodSource<O> result = getOrigin().addMethod().setReturnType(typeName()) .setName(accessorName); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("return %s;", getName()); result.setBody(body); } } return result; }
private MethodSource<O> createAccessor() { Assert.isTrue(getAccessor() == null, "Accessor method already exists"); final Type<O> type = getType(); final String accessorName = methodName(type.isType(boolean.class) ? "is" : "get", name); final MethodSource<O> result = getOrigin().addMethod().setReturnType(typeName()) .setName(accessorName); if (!getOrigin().isInterface()) { result.setVisibility(Visibility.PUBLIC); if (hasField()) { final String body = String.format("return %s;", getName()); result.setBody(body); } } return result; }
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); } }