diff --git a/.classpath b/.classpath index c280ec5..63fd6b8 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,6 @@ - + diff --git a/PeerAtCodeParser.jar b/PeerAtCodeParser.jar index 5aace66..fc3a34b 100644 Binary files a/PeerAtCodeParser.jar and b/PeerAtCodeParser.jar differ diff --git a/src/dev/peerat/mapping/TreasureCache.java b/src/dev/peerat/mapping/TreasureCache.java index 971aaa7..c95107a 100644 --- a/src/dev/peerat/mapping/TreasureCache.java +++ b/src/dev/peerat/mapping/TreasureCache.java @@ -6,6 +6,10 @@ import java.util.function.Function; public class TreasureCache { + public static void main(String[] args) { + + } + /** * Badge b = Badge.load("myTest"); * Badge b = new Badge("test"); diff --git a/src/dev/peerat/mapping/TreasureProcessor.java b/src/dev/peerat/mapping/TreasureProcessor.java index 8fd56cf..b2c9187 100644 --- a/src/dev/peerat/mapping/TreasureProcessor.java +++ b/src/dev/peerat/mapping/TreasureProcessor.java @@ -1,76 +1,104 @@ package dev.peerat.mapping; -import java.io.IOException; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.annotation.processing.AbstractProcessor; -import javax.annotation.processing.Messager; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.SourceVersion; -import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; +import javax.tools.FileObject; import javax.tools.StandardLocation; +import be.jeffcheasey88.peeratcode.parser.Parser; +import be.jeffcheasey88.peeratcode.parser.TokenValidator; +import be.jeffcheasey88.peeratcode.parser.java.JavaElement; +import be.jeffcheasey88.peeratcode.parser.java.JavaFile; +import be.jeffcheasey88.peeratcode.parser.java.JavaParser; + public class TreasureProcessor extends AbstractProcessor{ @Override public boolean process(Set annotations, RoundEnvironment roundEnv){ - Messager messager = processingEnv.getMessager(); + String[] sources = processingEnv.getOptions().get("treasure.source").split(";"); - for(TypeElement annotation : annotations){ - for(Element element : roundEnv.getElementsAnnotatedWith(annotation)){ - -// for(Entry opt : processingEnv.getOptions().entrySet()){ -// messager.printMessage(Kind.WARNING, opt.getKey()+" -> "+opt.getValue(), element); -// } - -// File file = null; -// try { -// FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, "", ""); -// File clazz = new File(resource.toUri().toString().substring(6)+"/be/jeffcheasey88/peeratcode/model/Badge.class"); -// if(clazz.exists()){ -// messager.printMessage(Kind.WARNING, "["+roundEnv.processingOver()+"] I was created it", element); -// return true; -// } -// -// file = new File(resource.toUri().toString().substring(6)+"/be/jeffcheasey88/peeratcode/model/Badge.java"); -// if(!file.exists()){ -// File parent = file.getParentFile(); -// if(!parent.exists()) parent.mkdirs(); -// file.createNewFile(); -// } -// -// Writer writer = new FileWriter(file); -// writer.append("package be.jeffcheasey88.peeratcode.model;\n\n"); -// writer.append("public class Badge extends TreasureInABottle{\n"); -// writer.append("public Badge(){}\n"); -// writer.append("public static int getBadge(int player){ return context; }\n"); -// writer.append("}\n"); -// writer.flush(); -// writer.close(); -// -// Process p = Runtime.getRuntime().exec("javac be/jeffcheasey88/peeratcode/model/Badge.java", new String[0], new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\.generated")); -// p.waitFor(); -// -// file.delete(); -// -// messager.printMessage(Kind.WARNING, "["+roundEnv.processingOver()+"] We created it !", element); -// } catch (Exception e) { -// e.printStackTrace(); -// messager.printMessage(Kind.WARNING, "["+roundEnv.processingOver()+"] We not created it ! ("+file+") "+e.getMessage(), element); -// } - } - } - + List workingDir = new ArrayList<>(); + File output; + + try{ + FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, "", ""); + output = new File(resource.toUri()); + + for(String source : sources){ + File given = new File(source); + if(given.equals(output)) continue; + workingDir.add(given); + } + }catch(Exception e){ + return false; + } + + for(File workingDirectory : workingDir){ + try { + parseClazz(workingDirectory, output, workingDirectory); + }catch(Exception ex){ + annotations.forEach((a) -> roundEnv.getElementsAnnotatedWith(a).forEach((e) -> processingEnv.getMessager().printMessage(Kind.ERROR, ex.getMessage(), e))); + } + } return true; } - private void rework(){ - + private static final Parser PARSER = new JavaParser(); + + private void parseClazz(File source, File output, File file) throws Exception{ + if(file.isDirectory()){ + for(File child : file.listFiles()) parseClazz(source, output, child); + return; + } + if(!file.getName().endsWith(".java")) return; + BufferedReader reader = new BufferedReader(new FileReader(file)); + try { + JavaFile javaFile = new JavaFile(); + TokenValidator.TOKENS = 0; + TokenValidator.MAX_VALIDATE = 0; + PARSER.parse(reader, javaFile); + process(source, output, file, javaFile); + }catch(Exception ex){ + error(source, output, file, ex); + } + } + + private void process(File source, File output, File clazz, JavaFile file) throws Exception{ + File out = new File(output, clazz.getAbsolutePath().substring(source.getAbsolutePath().length())); + File parent = out.getParentFile(); + if(!parent.exists()) parent.mkdirs(); + if(!out.exists()) out.createNewFile(); + BufferedWriter writer = new BufferedWriter(new FileWriter(out)); + PARSER.build(writer); + } + + private void error(File source, File output, File clazz, Exception e) throws Exception{ + File out = new File(output, clazz.getAbsolutePath().substring(source.getAbsolutePath().length())); + File parent = out.getParentFile(); + if(!parent.exists()) parent.mkdirs(); + if(!out.exists()) out.createNewFile(); + BufferedWriter writer = new BufferedWriter(new FileWriter(out)); + writer.write(e.getMessage()+"\n"); + for(StackTraceElement trace : e.getStackTrace()){ + writer.write(trace+"\n"); + } + writer.flush(); + writer.close(); } @Override