Base parsing on processor
This commit is contained in:
parent
b2c26c0e19
commit
7689afeef9
4 changed files with 84 additions and 52 deletions
|
@ -2,6 +2,6 @@
|
|||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="PeerAtCodeParser.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="PeerAtCodeParser.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
Binary file not shown.
|
@ -6,6 +6,10 @@ import java.util.function.Function;
|
|||
|
||||
public class TreasureCache<T> {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Badge b = Badge.load("myTest");
|
||||
* Badge b = new Badge("test");
|
||||
|
|
|
@ -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<? extends TypeElement> 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<String, String> 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<File> 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<JavaElement> 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
|
||||
|
|
Loading…
Add table
Reference in a new issue