Add JackfruitDemo

This commit is contained in:
Hari Nair
2022-09-19 11:50:56 -04:00
parent fce1f4a4eb
commit 72c3a36a1a
15 changed files with 128 additions and 12 deletions

View File

@@ -23,6 +23,7 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
@@ -31,14 +32,13 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/jackfruit"/>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,75 @@
package jackfruit.demo;
import jackfruit.processor.ConfigFactory;
import java.lang.Double;
import java.lang.Override;
import java.lang.String;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public final class DemoConfigFactory implements ConfigFactory<DemoConfig> {
private static final Logger logger = LogManager.getLogger();
@Override
public PropertiesConfiguration toConfig(DemoConfig t) {
PropertiesConfiguration config = new PropertiesConfiguration();
config.setProperty("prefix.key", t.intMethod());
config.setProperty("prefix.doubleMethod", t.doubleMethod());
config.setProperty("prefix.StringMethod", t.StringMethod());
SomeRandomClassParser randomClassparser = new SomeRandomClassParser();
config.setProperty("prefix.randomClass", randomClassparser.toString(t.randomClass()));
return config;
}
@Override
public DemoConfig fromConfig(Configuration config) {
return new DemoConfig() {
/**
* field comment
*/
public int intMethod() {
return config.getInt("prefix.key");
}
public Double doubleMethod() {
return config.getDouble("prefix.doubleMethod");
}
public String StringMethod() {
return config.getString("prefix.StringMethod");
}
public SomeRandomClass randomClass() {
SomeRandomClassParser parser = new SomeRandomClassParser();
return parser.fromString(config.getString("prefix.randomClass"));
}
};
}
@Override
public DemoConfig getTemplate() {
return new DemoConfig() {
/**
* field comment
*/
public int intMethod() {
return 0;
}
public Double doubleMethod() {
return 0.;
}
public String StringMethod() {
return "Default String";
}
public SomeRandomClass randomClass() {
SomeRandomClassParser parser = new SomeRandomClassParser();
return parser.fromString("serialized string");
}
};
}
}

Binary file not shown.

View File

@@ -46,7 +46,7 @@ import jackfruit.annotations.ParserClass;
*/
@ConfigParams(prefix = "prefix")
public interface ConfigTemplate {
public interface DemoConfig {
// default key is field name
@Key("key")

Binary file not shown.

View File

@@ -0,0 +1,36 @@
package jackfruit.demo;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
public class JackfruitDemo {
public static void main(String[] args) {
// this factory is built by the annotation processor after reading the DemoConfig interface
DemoConfigFactory factory = new DemoConfigFactory();
// get an example config object
DemoConfig template = factory.getTemplate();
// generate an Apache PropertiesConfiguration object. This can be written out to a file
PropertiesConfiguration config = factory.toConfig(template);
try {
config.write(new PrintWriter(System.out));
} catch (ConfigurationException | IOException e) {
e.printStackTrace();
}
// create a config object from an Apache PropertiesConfiguration
template = factory.fromConfig(config);
System.out.printf("config.StringMethod() = %s\n", template.StringMethod());
}
}

View File

@@ -2,4 +2,14 @@ package jackfruit.demo;
public class SomeRandomClass {
private String internalString;
public String getInternalString() {
return internalString;
}
public SomeRandomClass(String internalString) {
this.internalString = internalString;
}
}

View File

@@ -1,22 +1,17 @@
package jackfruit.demo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import jackfruit.annotations.Parser;
public class SomeRandomClassParser implements Parser<SomeRandomClass> {
public static Logger logger = LogManager.getLogger();
@Override
public SomeRandomClass fromString(String s) {
logger.info("deserialize SomeRandomClass from " + s);
return new SomeRandomClass();
return new SomeRandomClass(s);
}
@Override
public String toString(SomeRandomClass src) {
return "";
return src.getInternalString();
}

Binary file not shown.

View File

@@ -36,7 +36,7 @@ public class TestProcessor {
System.out.println(System.getProperty("java.class.path"));
*/
boolean ignore = true;
boolean ignore = false;
if (!ignore) {
Log4j2Configurator lc = Log4j2Configurator.getInstance();
lc.setPattern("%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%c{1}:%L] %msg%n%throwable}");