import static java.lang.String.format;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
+import javax.lang.model.element.Modifier;
import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation;
-import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation.Parameter;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper;
String name, List<Field> fields, List<MethodDefinition> methods) {
// TODO header
super(null, runtimeBeanEntry.getPackageName(), name, Collections
- .<String> emptyList(), Collections.singletonList(Closeable.class
+ .emptyList(), Collections.singletonList(Closeable.class
.getCanonicalName()), fields, methods);
}
String registratorName = getJavaNameOfRuntimeRegistrator(rootRB);
List<MethodDefinition> methods = new ArrayList<>();
Field rootRuntimeBeanRegistratorField = new Field(
- Lists.newArrayList("final"),
+ Collections.singletonList(Modifier.FINAL),
RootRuntimeBeanRegistrator.class.getName(),
"rootRuntimeBeanRegistrator");
List<Field> constructorParameters = Lists
registratorName, constructorParameters, constructorBody);
methods.add(constructor);
- LinkedHashMap<String, RuntimeRegistratorFtlTemplate> RuntimeRegistratorFtlTemplates = createRegistrationHierarchy(
- rootRB, Collections.<String> emptySet());
+ LinkedHashMap<String, RuntimeRegistratorFtlTemplate> RuntimeRegistratorFtlTemplates =
+ createRegistrationHierarchy(rootRB, Collections.emptySet());
RuntimeRegistratorFtlTemplate rootFtlFile = RuntimeRegistratorFtlTemplates
.values().iterator().next();
// TODO add header
GeneralClassTemplate registrator = new GeneralClassTemplate(null,
rootRB.getPackageName(), registratorName,
- Collections.<String> emptyList(), Collections.singletonList(Closeable.class
+ Collections.emptyList(), Collections.singletonList(Closeable.class
.getCanonicalName()), constructorParameters, methods);
checkState(!RuntimeRegistratorFtlTemplates.containsKey(registrator
}
private static Field hierachicalRegistration = new Field(
- Lists.newArrayList("final"),
+ Collections.singletonList(Modifier.FINAL),
HierarchicalRuntimeBeanRegistration.class.getCanonicalName(),
"registration");
Set<String> currentOccupiedKeys = new HashSet<>(occupiedKeys);
currentOccupiedKeys.add(parent.getJavaNamePrefix());
- Field registratorsMapField = new Field(Collections.singletonList("final"),
+ Field registratorsMapField = new Field(Collections.singletonList(Modifier.FINAL),
TypeHelper.getGenericType(Map.class, String.class,
AtomicInteger.class), "unkeyedMap", "new "
+ TypeHelper.getGenericType(HashMap.class,
"String key = \"%s\"; //TODO: check for conflicts\n",
key));
- if (child.getKeyJavaName().isPresent()) {
- value = "bean.get" + child.getKeyJavaName().get() + "()";
+ Optional<String> childKeyJavaName = child.getKeyJavaName();
+ if (childKeyJavaName.isPresent()) {
+ value = "bean.get" + childKeyJavaName.get() + "()";
value = "String.valueOf(" + value + ")";
} else {
body.append("java.util.concurrent.atomic.AtomicInteger counter = unkeyedMap.get(key);\n"
body.append(format("return new %s(r);",
childRegistrator.getFullyQualifiedName()));
- Field param = new Field(Lists.newArrayList("final"),
+ Field param = new Field(Collections.singletonList(Modifier.FINAL),
child.getJavaNameOfRuntimeMXBean(), "bean");
MethodDefinition register = new MethodDefinition(
- Collections.singletonList("synchronized"),
+ Collections.singletonList(Modifier.SYNCHRONIZED),
childRegistrator.getFullyQualifiedName(), "register",
- Collections.singletonList(param), Collections.<String> emptyList(),
- Collections.<Annotation> emptyList(), body.toString());
+ Collections.singletonList(param), Collections.emptyList(),
+ Collections.emptyList(), body.toString());
methods.add(register);
}
// Arrays.asList(IOException.class.getCanonicalName()),
// Collections.<Annotation> emptyList(), body);
List<Annotation> annotations = Lists.newArrayList(new Annotation(
- "Override", Collections.<Parameter> emptyList()));
- return new MethodDefinition(Collections.<String> emptyList(), "void",
- "close", Collections.<Field> emptyList(),
- Collections.<String> emptyList(), annotations, body);
+ "Override", Collections.emptyList()));
+ return new MethodDefinition(Collections.emptyList(), "void",
+ "close", Collections.emptyList(),
+ Collections.emptyList(), annotations, body);
}
@VisibleForTesting
// convert JavaAttribute parameters into fields
List<Field> fields = new ArrayList<>();
for (JavaAttribute ja : rpc.getParameters()) {
- Field field = new Field(Collections.<String> emptyList(),
+ Field field = new Field(Collections.emptyList(),
ja.getType().getFullyQualifiedName(),
ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
fields.add(field);
import static com.google.common.base.Preconditions.checkNotNull;
-import com.google.common.collect.Lists;
+import java.util.ArrayList;
import java.util.List;
+import javax.lang.model.element.Modifier;
public class Field {
private final String type;
private final String name;
private final String definition;
- private final List<String> modifiers;
+ private final List<Modifier> modifiers;
private final boolean needsDepResolver;
public Field(String type, String name) {
- this(Lists.<String> newArrayList(), type, name, null, false);
+ this(new ArrayList<>(), type, name, null, false);
}
public Field(String type, String name, String definition) {
- this(Lists.<String> newArrayList(), type, name, definition, false);
+ this(new ArrayList<>(), type, name, definition, false);
}
- public Field(List<String> modifiers, String type, String name) {
+ public Field(List<Modifier> modifiers, String type, String name) {
this(modifiers, type, name, null, false);
}
- public Field(List<String> modifiers, String type, String name,
+ public Field(List<Modifier> modifiers, String type, String name,
String definition) {
this(modifiers, type, name, definition, false);
}
- public Field(List<String> modifiers, String type, String name,
+ public Field(List<Modifier> modifiers, String type, String name,
String nullableDefinition, boolean needsDepResolver) {
this.modifiers = checkNotNull(modifiers);
this.type = checkNotNull(type);
}
public Field(String type, String name, String definition, boolean needsDepResolver) {
- this(Lists.<String> newArrayList(), type, name, definition, needsDepResolver);
+ this(new ArrayList<>(), type, name, definition, needsDepResolver);
}
public boolean isNeedsDepResolver() {
return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
}
- public List<String> getModifiers() {
+ public List<Modifier> getModifiers() {
return modifiers;
}
public static String toString(Field field) {
StringBuilder build = new StringBuilder();
build.append("private ");
- for (String mod : field.getModifiers()) {
- build.append(mod).append(" ");
- }
+ field.getModifiers().forEach(mod -> build.append(mod).append(" "));
build.append(field.getType()).append(" ");
build.append(field.getName());
if (field.getDefinition() != null) {
package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
import java.util.List;
+import java.util.Optional;
+import javax.lang.model.element.Modifier;
public interface Method {
- String getVisibility();
+ Optional<Modifier> getVisibility();
- List<String> getModifiers();
+ List<Modifier> getModifiers();
String getReturnType();
String getJavadoc();
List<Annotation> getAnnotations();
+
+ List<String> getThrowsExceptions();
+
+ Optional<String> getBody();
}
import java.util.Collections;
import java.util.List;
-import org.apache.commons.lang3.StringUtils;
+import java.util.Optional;
+import javax.lang.model.element.Modifier;
public class MethodDeclaration implements Method {
private final String returnType;
public MethodDeclaration(String returnType, String name,
List<Field> parameters) {
- this(returnType, name, parameters, Collections.<Annotation> emptyList());
+ this(returnType, name, parameters, Collections.emptyList());
}
public MethodDeclaration(String returnType, String name,
return annotations;
}
+ @Override
+ public List<String> getThrowsExceptions() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Optional<String> getBody() {
+ return Optional.empty();
+ }
+
@Override
public String getJavadoc() {
return javadoc;
}
@Override
- public String getVisibility() {
- return StringUtils.EMPTY;
+ public Optional<Modifier> getVisibility() {
+ return Optional.empty();
}
@Override
}
@Override
- public List<String> getModifiers() {
+ public List<Modifier> getModifiers() {
return Collections.emptyList();
}
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import javax.lang.model.element.Modifier;
public class MethodDefinition implements Method {
- private static final String VISIBILITY_PUBLIC = Modifier.PUBLIC.toString();
-
- private final List<String> modifiers;
+ private final List<Modifier> modifiers;
private final String returnType;
private final String name;
private final List<Field> parameters;
public MethodDefinition(String returnType, String name,
List<Field> parameters, String body) {
- this(Collections.<String> emptyList(), returnType, name, parameters,
- Collections.<String> emptyList(), Collections
- .<Annotation> emptyList(), body);
+ this(Collections.emptyList(), returnType, name, parameters,
+ Collections.emptyList(), Collections.emptyList(), body);
}
public MethodDefinition(String returnType, String name,
List<Field> parameters, List<Annotation> annotations, String body) {
- this(Collections.<String> emptyList(), returnType, name, parameters,
- Collections.<String> emptyList(), annotations, body);
+ this(Collections.emptyList(), returnType, name, parameters,
+ Collections.emptyList(), annotations, body);
}
- public MethodDefinition(List<String> modifiers, String returnType,
+ public MethodDefinition(List<Modifier> modifiers, String returnType,
String name, List<Field> parameters, List<String> throwsExceptions,
List<Annotation> annotations, String body) {
this.modifiers = modifiers;
}
@Override
- public String getVisibility() {
- return VISIBILITY_PUBLIC;
+ public Optional<Modifier> getVisibility() {
+ return Optional.of(Modifier.PUBLIC);
}
@Override
return parameters;
}
+ @Override
public List<String> getThrowsExceptions() {
return throwsExceptions;
}
- public String getBody() {
- return body;
+ @Override
+ public Optional<String> getBody() {
+ return Optional.of(body);
}
@Override
- public List<String> getModifiers() {
+ public List<Modifier> getModifiers() {
return modifiers;
}
package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
+import java.util.Optional;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import javax.lang.model.element.Modifier;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
class MethodSerializer {
static String toString(Method method) {
StringBuilder build = new StringBuilder();
+ Consumer<Modifier> appendWithSpace = string -> build.append(string).append(" ");
+
if (method.getJavadoc() != null) {
build.append(StringUtil.writeComment(method.getJavadoc(), true));
}
- for(Annotation a: method.getAnnotations()) {
- build.append(a);
- }
+ method.getAnnotations().forEach(build::append);
build.append(" ");
- build.append(method.getVisibility()).append(" ");
- for (String mod : method.getModifiers()) {
- build.append(mod).append(" ");
- }
+ method.getVisibility().ifPresent(appendWithSpace);
+ method.getModifiers().forEach(appendWithSpace);
build.append(method.getReturnType()).append(" ");
build.append(method.getName()).append("(");
if (!firstParam) {
build.append(", ");
}
- for (String mod : param.getModifiers()) {
- build.append(mod).append(" ");
- }
+ param.getModifiers().forEach(appendWithSpace);
build.append(param.getType()).append(" ");
build.append(param.getName());
firstParam = false;
}
build.append(")");
- if (method instanceof MethodDeclaration) {
+ if (!method.getThrowsExceptions().isEmpty()) {
+ build.append(" throws ");
+ build.append(method.getThrowsExceptions().stream().collect(Collectors.joining(", ")));
+ }
+
+ Optional<String> body = method.getBody();
+ if (!body.isPresent()) {
build.append(";");
build.append("\n");
- } else if (method instanceof MethodDefinition) {
- MethodDefinition definition = (MethodDefinition) method;
- if (!definition.getThrowsExceptions().isEmpty()) {
- build.append(" throws ");
- }
- for (String ex : definition.getThrowsExceptions()) {
- build.append(ex).append(" ");
- }
+ } else {
build.append(" {");
build.append("\n");
build.append(" ");
- build.append(definition.getBody());
+ build.append(body.get());
build.append("\n");
build.append(" ");
build.append("}");
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import javax.lang.model.element.Modifier;
import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency;
public class ModuleField extends Field {
private final boolean dependent, isListOfDependencies;
private final Dependency dependency;
- private ModuleField(List<String> modifiers, String type, String name, String attributeName, String nullableDefault,
- boolean isDependency, Dependency dependency, boolean isListOfDependencies, boolean needsDepResolver) {
+ private ModuleField(List<Modifier> modifiers, String type, String name, String attributeName,
+ String nullableDefault, boolean isDependency, Dependency dependency, boolean isListOfDependencies,
+ boolean needsDepResolver) {
super(modifiers, type, name, null, needsDepResolver);
this.dependent = isDependency;
this.dependency = dependency;
public ModuleField(String type, String name, String attributeName, String nullableDefault, boolean isDependency,
Dependency dependency, boolean isListOfDependencies, boolean needsDepResolve) {
- this(Collections.<String> emptyList(), type, name, attributeName, nullableDefault, isDependency, dependency,
+ this(Collections.emptyList(), type, name, attributeName, nullableDefault, isDependency, dependency,
isListOfDependencies, needsDepResolve);
}
builder.append("\n");
builder.append(" private ");
- for (String mod : moduleField.getModifiers()) {
- builder.append(mod).append(" ");
- }
+ moduleField.getModifiers().forEach(mod -> builder.append(mod).append(" "));
builder.append(moduleField.getType()).append(" ");
builder.append(moduleField.getName());
if (moduleField.getNullableDefault() != null) {