Created
September 19, 2016 07:04
-
-
Save rgolangh/2615eae37828e3ccf092e47a0a236cbf to your computer and use it in GitHub Desktop.
A java class generator out of properties file - mainly for oVirt resources files but can be tweaked to create any class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.ovirt.engine.common; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.lang.reflect.Method; | |
import java.nio.file.FileSystems; | |
import java.util.Map; | |
import java.util.Properties; | |
import com.google.gwt.i18n.client.Constants; | |
import com.google.gwt.i18n.client.ConstantsWithLookup; | |
import com.sun.codemodel.JAnnotationUse; | |
import com.sun.codemodel.JClassAlreadyExistsException; | |
import com.sun.codemodel.JCodeModel; | |
import com.sun.codemodel.JDefinedClass; | |
import com.sun.codemodel.JMethod; | |
public class AppErrorsCreator { | |
public static final String PACKAGE = System.getProperty("generated.apperrors.packageName"); | |
public static final String CLASS_NAME = System.getProperty("generated.apperrors.className"); | |
private static void from(Properties props) throws JClassAlreadyExistsException, IOException { | |
JCodeModel codeModel = new JCodeModel(); | |
JDefinedClass definedClass = codeModel._class(PACKAGE + "." + CLASS_NAME); | |
// class definition | |
definedClass._extends(ConstantsWithLookup.class); | |
// created methods | |
for (Map.Entry<Object, Object> entry : props.entrySet()) { | |
JMethod method = definedClass.method( | |
Method.PUBLIC, | |
String.class, | |
stripKey(entry)); | |
//annotate | |
JAnnotationUse annotate = method.annotate(Constants.DefaultStringValue.class); | |
annotate.param("value", entry.getValue().toString()); | |
} | |
//build class | |
codeModel.build(FileSystems.getDefault().getPath( | |
System.getProperty("generated.apperrors.targetDir")).toFile()); | |
} | |
private static String stripKey(Map.Entry<Object, Object> entry) { | |
return entry.getKey().toString().replace(".", "_"); | |
} | |
/** | |
* create the AppErrors java class. use the system properties documented in the pom.xml | |
* @throws IOException | |
* @throws JClassAlreadyExistsException | |
*/ | |
public static void main(String... args) throws IOException, JClassAlreadyExistsException { | |
from(loadAppErrors()); | |
} | |
private static Properties loadAppErrors() throws IOException { | |
Properties props = new Properties(); | |
InputStream resource = props.getClass().getResource( | |
System.getProperty("source.apperrors")).openStream(); | |
System.out.printf("\nresource ----%s %s\n", System.getProperty("source.apperrors"), resource); | |
props.load(resource); | |
return props; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment