Update demo

This commit is contained in:
Hari Nair
2023-09-02 16:27:08 -04:00
parent 5cb531fd26
commit 9752760ca5
2 changed files with 20 additions and 12 deletions

View File

@@ -9,9 +9,9 @@ package jackfruit.demo;
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,6 +23,7 @@ package jackfruit.demo;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.PropertiesConfigurationLayout;
@@ -89,14 +90,21 @@ public class JackfruitDemo {
System.out.println("random.toUpperCase() = " + random.toUpperCase());
// create a new factory with a different prefix, but same parameters
System.out.println();
System.out.println(
"Create a config with anotherPrefix and retrieve randoms. This will throw an exception.");
factory = new DemoClassFactory("anotherPrefix");
template = factory.fromConfig(config);
// this will not find anything, since template was created from the original config, which
// uses the original prefix.
randoms = template.randoms();
// this will throw an exception since anotherPrefix.randoms is not one of the keys in the
// configuration.
try {
randoms = template.randoms();
} catch (Exception e) {
System.out.println("Caught expected RuntimeException:\n" + e.getLocalizedMessage());
randoms = new ArrayList<>();
}
for (SomeRandomClass random : randoms)
System.out.println("random.toUpperCase() = " + random.toUpperCase());
}
}

View File

@@ -123,7 +123,7 @@ public class ConfigProcessor extends AbstractProcessor {
Jackfruit configParams = annotatedType.getAnnotation(Jackfruit.class);
String prefix = configParams.prefix().strip();
if (prefix.length() > 0 && !prefix.endsWith(".")) prefix += ".";
if (!prefix.isEmpty() && !prefix.endsWith(".")) prefix += ".";
// This is the templatized class with annotations to be processed (e.g.
// ConfigTemplate)
@@ -317,7 +317,7 @@ public class ConfigProcessor extends AbstractProcessor {
Diagnostic.Kind.ERROR,
String.format(
"Unsupported kind %s for type %s!",
erasure.getKind().toString(), erasure.toString()));
erasure.getKind().toString(), erasure));
}
builder.addAllTypeArgs(typeArgs);
@@ -354,7 +354,7 @@ public class ConfigProcessor extends AbstractProcessor {
AnnotationBundle bundle = builder.build();
if (ConfigProcessorUtils.isList(bundle.erasure(), processingEnv)
&& bundle.typeArgs().size() == 0)
&& bundle.typeArgs().isEmpty())
messager.printMessage(
Diagnostic.Kind.ERROR,
String.format("No parameter type for List on method %s!", e.getSimpleName()));
@@ -473,7 +473,7 @@ public class ConfigProcessor extends AbstractProcessor {
}
// add the comment
if (ab.comment().length() > 0) {
if (!ab.comment().isEmpty()) {
String commentName = String.format("%sComment", method.getSimpleName());
methodBuilder.addStatement("$T $L = $S", String.class, commentName, ab.comment());
methodBuilder.addStatement(
@@ -570,7 +570,7 @@ public class ConfigProcessor extends AbstractProcessor {
if (ConfigProcessorUtils.isString(bundle.erasure(), processingEnv)) {
builder.addStatement("return $S", bundle.defaultValue());
} else {
if (bundle.defaultValue().trim().length() == 0) {
if (bundle.defaultValue().trim().isEmpty()) {
processingEnv
.getMessager()
.printMessage(
@@ -624,7 +624,7 @@ public class ConfigProcessor extends AbstractProcessor {
builder.addStatement("String key = $N + $S", prefix, bundle.key());
builder
.beginControlFlow("if (!config.containsKey(key))")
.addStatement("throw new $T($S + key)", RuntimeException.class, "No such key")
.addStatement("throw new $T($S + key)", RuntimeException.class, "No such key ")
.endControlFlow();
TypeMirror parser = null;