Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- public class JavaCodeGenerator {
- private static List<String> generateSource(String className, Map<String, String> fields)
- throws IllegalArgumentException {
- if (!validName(className)) { throw new IllegalArgumentException("className"); }
- if (fields == null) { throw new IllegalArgumentException("fields"); }
- final List<String> source = new ArrayList<>();
- source.add(String.format("public class %s {", className));
- for (Map.Entry<String, String> pair : fields.entrySet()) {
- if (!validName(pair.getKey())) { throw new IllegalArgumentException("fields"); }
- source.add("");
- source.add(String.format(" public %s %s;", pair.getValue(), pair.getKey()));
- }
- source.add("}");
- return source;
- }
- private static String buildClassFile(String className, List<String> source, String path)
- throws IllegalArgumentException, IOException, InterruptedException {
- if (!validName(className)) { throw new IllegalArgumentException("className"); }
- if (source == null || source.isEmpty()) { throw new IllegalArgumentException("source"); }
- if (path == null || path.isEmpty()) { throw new IllegalArgumentException("path"); }
- final File directory = new File(path, UUID.randomUUID().toString());
- directory.mkdir();
- final File sourceFile = new File(directory, className + ".java");
- try (
- FileOutputStream fos = new FileOutputStream(sourceFile);
- OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
- BufferedWriter bw = new BufferedWriter(osw)
- ) {
- for (String line : source) {
- bw.write(line);
- bw.newLine();
- }
- } catch (IOException ex) {
- throw ex;
- }
- if (sourceFile.exists()) {
- final String fs = System.getProperty("file.separator");
- final Process process = new ProcessBuilder(
- System.getenv("JAVA_HOME") + fs + "bin" + fs + "javac",
- "-source",
- "8",
- "-target",
- "8",
- "-encoding",
- "utf8",
- "-cp",
- directory.getAbsolutePath(),
- "-d",
- directory.getAbsolutePath(),
- sourceFile.getAbsolutePath()
- ).start();
- final int exitCode = process.waitFor();
- sourceFile.delete();
- if (exitCode != 0) {
- final File classFile = new File(directory, className + ".class");
- if (classFile.exists()) {
- classFile.delete();
- }
- directory.delete();
- return "";
- }
- }
- return directory.getAbsolutePath();
- }
- private static boolean validName(String name) {
- if (name == null || name.isEmpty()) return false;
- final char[] chars = name.toCharArray();
- if (chars[0] != '_' && !Character.isLetter(chars[0])) return false;
- for (int i = 1; i < chars.length; i++) {
- if (chars[i] != '_' && !Character.isLetter(chars[i]) && !Character.isDigit(chars[i])) return false;
- }
- return true;
- }
- private static void usingClass(String className, String path) {
- // User user = new User();
- // user.id = 1;
- // user.name = "admin";
- // user.password = "qwerty";
- // System.out.printf(
- // "{ 'id': '%d', 'name': '%s', 'password': '%s'}",
- // user.id,
- // user.name,
- // user.password
- // );
- // System.out.println();
- }
- public static void main(String[] args) {
- try {
- final Map<String, String> fields = new HashMap<>();
- fields.put("id", "Integer");
- fields.put("name", "String");
- fields.put("password", "String");
- final List<String> source = generateSource("User", fields);
- final String path = buildClassFile("User", source, System.getProperty("user.dir"));
- if (!path.isEmpty() && (new File(path, "User.class")).exists()) {
- System.out.println("Class User created in: " + path);
- usingClass("User", path);
- (new File(path, "User.class")).delete();
- (new File(path)).delete();
- }
- } catch (Exception ex) {
- System.err.println("[ERROR] " + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement