Support for compiling on Java 14
This commit is contained in:
@@ -197,6 +197,8 @@ public class EntityIO{
|
||||
}
|
||||
|
||||
private void io(String type, String field) throws Exception{
|
||||
type = type.replace("mindustry.gen.", "");
|
||||
|
||||
if(BaseProcessor.isPrimitive(type)){
|
||||
s(type.equals("boolean") ? "bool" : type.charAt(0) + "", field);
|
||||
}else if(instanceOf(type, "mindustry.ctype.Content")){
|
||||
|
||||
@@ -36,8 +36,8 @@ public class EntityProcess extends BaseProcessor{
|
||||
ObjectMap<String, Stype> componentNames = new ObjectMap<>();
|
||||
ObjectMap<Stype, Seq<Stype>> componentDependencies = new ObjectMap<>();
|
||||
ObjectMap<Selement, Seq<Stype>> defComponents = new ObjectMap<>();
|
||||
ObjectMap<Svar, String> varInitializers = new ObjectMap<>();
|
||||
ObjectMap<Smethod, String> methodBlocks = new ObjectMap<>();
|
||||
ObjectMap<String, String> varInitializers = new ObjectMap<>();
|
||||
ObjectMap<String, String> methodBlocks = new ObjectMap<>();
|
||||
ObjectSet<String> imports = new ObjectSet<>();
|
||||
Seq<Selement> allGroups = new Seq<>();
|
||||
Seq<Selement> allDefs = new Seq<>();
|
||||
@@ -68,14 +68,14 @@ public class EntityProcess extends BaseProcessor{
|
||||
//add initializer if it exists
|
||||
if(tree.getInitializer() != null){
|
||||
String init = tree.getInitializer().toString();
|
||||
varInitializers.put(f, init);
|
||||
varInitializers.put(f.descString(), init);
|
||||
}
|
||||
}
|
||||
|
||||
for(Smethod elem : component.methods()){
|
||||
if(elem.is(Modifier.ABSTRACT) || elem.is(Modifier.NATIVE)) continue;
|
||||
//get all statements in the method, store them
|
||||
methodBlocks.put(elem, elem.tree().getBody().toString());
|
||||
methodBlocks.put(elem.descString(), elem.tree().getBody().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +184,7 @@ public class EntityProcess extends BaseProcessor{
|
||||
|
||||
//look at each definition
|
||||
for(Selement<?> type : allDefs){
|
||||
|
||||
EntityDef ann = type.annotation(EntityDef.class);
|
||||
boolean isFinal = ann.isFinal();
|
||||
|
||||
@@ -248,8 +249,8 @@ public class EntityProcess extends BaseProcessor{
|
||||
}
|
||||
|
||||
//add initializer if it exists
|
||||
if(varInitializers.containsKey(f)){
|
||||
fbuilder.initializer(varInitializers.get(f));
|
||||
if(varInitializers.containsKey(f.descString())){
|
||||
fbuilder.initializer(varInitializers.get(f.descString()));
|
||||
}
|
||||
|
||||
fbuilder.addModifiers(f.has(ReadOnly.class) ? Modifier.PROTECTED : Modifier.PUBLIC);
|
||||
@@ -278,7 +279,7 @@ public class EntityProcess extends BaseProcessor{
|
||||
}
|
||||
}
|
||||
|
||||
//get all utility methods from components
|
||||
//get all methods from components
|
||||
for(Smethod elem : comp.methods()){
|
||||
methods.get(elem.toString(), Seq::new).add(elem);
|
||||
}
|
||||
@@ -389,10 +390,12 @@ public class EntityProcess extends BaseProcessor{
|
||||
}
|
||||
|
||||
for(Smethod elem : entry.value){
|
||||
if(elem.is(Modifier.ABSTRACT) || elem.is(Modifier.NATIVE) || !methodBlocks.containsKey(elem)) continue;
|
||||
String descStr = elem.descString();
|
||||
|
||||
if(elem.is(Modifier.ABSTRACT) || elem.is(Modifier.NATIVE) || !methodBlocks.containsKey(descStr)) continue;
|
||||
|
||||
//get all statements in the method, copy them over
|
||||
String str = methodBlocks.get(elem);
|
||||
String str = methodBlocks.get(descStr);
|
||||
//name for code blocks in the methods
|
||||
String blockName = elem.up().getSimpleName().toString().toLowerCase().replace("comp", "");
|
||||
|
||||
@@ -435,13 +438,14 @@ public class EntityProcess extends BaseProcessor{
|
||||
for(FieldSpec spec : builder.fieldSpecs){
|
||||
@Nullable Svar variable = specVariables.get(spec);
|
||||
if(variable != null && variable.isAny(Modifier.STATIC, Modifier.FINAL)) continue;
|
||||
String desc = variable.descString();
|
||||
|
||||
if(spec.type.isPrimitive()){
|
||||
//set to primitive default
|
||||
resetBuilder.addStatement("$L = $L", spec.name, variable != null && varInitializers.containsKey(variable) ? varInitializers.get(variable) : getDefault(spec.type.toString()));
|
||||
resetBuilder.addStatement("$L = $L", spec.name, variable != null && varInitializers.containsKey(desc) ? varInitializers.get(desc) : getDefault(spec.type.toString()));
|
||||
}else{
|
||||
//set to default null
|
||||
if(!varInitializers.containsKey(variable)){
|
||||
if(!varInitializers.containsKey(desc)){
|
||||
resetBuilder.addStatement("$L = null", spec.name);
|
||||
} //else... TODO reset if poolable
|
||||
}
|
||||
@@ -646,10 +650,11 @@ public class EntityProcess extends BaseProcessor{
|
||||
builder.addStatement("return -1");
|
||||
}else{
|
||||
Svar variable = compType == null || method.params().size > 0 ? null : compType.fields().find(v -> v.name().equals(method.name()));
|
||||
if(variable == null || !varInitializers.containsKey(variable)){
|
||||
String desc = variable == null ? null : variable.descString();
|
||||
if(variable == null || !varInitializers.containsKey(desc)){
|
||||
builder.addStatement("return " + getDefault(method.ret().toString()));
|
||||
}else{
|
||||
String init = varInitializers.get(variable);
|
||||
String init = varInitializers.get(desc);
|
||||
builder.addStatement("return " + (init.equals("{}") ? "new " + variable.mirror().toString() : "") + init);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class RemoteReadGenerator{
|
||||
readBlock.addStatement("$L $L = read.$L()", typeName, varName, pname);
|
||||
}else{
|
||||
//else, try and find a serializer
|
||||
String ser = serializers.readers.get(typeName, SerializerResolver.locate(entry.element, var.asType(), false));
|
||||
String ser = serializers.readers.get(typeName.replace("mindustry.gen.", ""), SerializerResolver.locate(entry.element, var.asType(), false));
|
||||
|
||||
if(ser == null){ //make sure a serializer exists!
|
||||
BaseProcessor.err("No read method to read class type '" + typeName + "' in method " + entry.targetMethod + "; " + serializers.readers, var);
|
||||
|
||||
@@ -174,7 +174,7 @@ public class RemoteWriteGenerator{
|
||||
method.addStatement("WRITE.$L($L)", typeName.equals("boolean") ? "bool" : typeName.charAt(0) + "", varName);
|
||||
}else{
|
||||
//else, try and find a serializer
|
||||
String ser = serializers.writers.get(typeName, SerializerResolver.locate(elem, var.asType(), true));
|
||||
String ser = serializers.writers.get(typeName.replace("mindustry.gen.", ""), SerializerResolver.locate(elem, var.asType(), true));
|
||||
|
||||
if(ser == null){ //make sure a serializer exists!
|
||||
BaseProcessor.err("No @WriteClass method to write class type: '" + typeName + "'", var);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package mindustry.annotations.util;
|
||||
|
||||
import arc.func.*;
|
||||
import com.sun.tools.javac.code.*;
|
||||
import com.sun.tools.javac.code.Attribute.Array;
|
||||
import com.sun.tools.javac.code.Attribute.Enum;
|
||||
@@ -10,16 +11,17 @@ import com.sun.tools.javac.code.Type;
|
||||
import com.sun.tools.javac.code.Symbol.*;
|
||||
import com.sun.tools.javac.code.Type.ArrayType;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
import com.sun.tools.javac.util.*;
|
||||
import sun.reflect.annotation.*;
|
||||
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.*;
|
||||
import java.io.*;
|
||||
import java.lang.Class;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import java.util.Map.*;
|
||||
import java.lang.Class;
|
||||
|
||||
//replaces the standard Java AnnotationProxyMaker with one that doesn't crash
|
||||
//thanks, oracle.
|
||||
@@ -59,27 +61,58 @@ public class AnnotationProxyMaker{
|
||||
}
|
||||
|
||||
private Map<MethodSymbol, Attribute> getAllValues(){
|
||||
LinkedHashMap var1 = new LinkedHashMap();
|
||||
ClassSymbol var2 = (ClassSymbol)this.anno.type.tsym;
|
||||
LinkedHashMap map = new LinkedHashMap();
|
||||
ClassSymbol cl = (ClassSymbol)this.anno.type.tsym;
|
||||
|
||||
for(com.sun.tools.javac.code.Scope.Entry var3 = var2.members().elems; var3 != null; var3 = var3.sibling){
|
||||
if(var3.sym.kind == 16){
|
||||
MethodSymbol var4 = (MethodSymbol)var3.sym;
|
||||
Attribute var5 = var4.getDefaultValue();
|
||||
if(var5 != null){
|
||||
var1.put(var4, var5);
|
||||
//try to use Java 8 API for this if possible
|
||||
try{
|
||||
Class entryClass = Class.forName("com.sun.tools.javac.code.Scope$Entry");
|
||||
Object members = cl.members();
|
||||
Field field = members.getClass().getField("elems");
|
||||
Object elems = field.get(members);
|
||||
Field siblingField = entryClass.getField("sibling");
|
||||
Field symField = entryClass.getField("sym");
|
||||
for(Object currEntry = elems; currEntry != null; currEntry = siblingField.get(currEntry)){
|
||||
handleSymbol((Symbol)symField.get(currEntry), map);
|
||||
}
|
||||
|
||||
}catch(Throwable e){
|
||||
//otherwise try other API
|
||||
|
||||
try{
|
||||
Class lookupClass = Class.forName("com.sun.tools.javac.code.Scope$LookupKind");
|
||||
Field nonRecField = lookupClass.getField("NON_RECURSIVE");
|
||||
Object nonRec = nonRecField.get(null);
|
||||
Scope scope = cl.members();
|
||||
Method getSyms = scope.getClass().getMethod("getSymbols", lookupClass);
|
||||
Iterable<Symbol> it = (Iterable<Symbol>)getSyms.invoke(scope, nonRec);
|
||||
Iterator<Symbol> i = it.iterator();
|
||||
while(i.hasNext()){
|
||||
handleSymbol(i.next(), map);
|
||||
}
|
||||
|
||||
}catch(Throwable death){
|
||||
//I tried
|
||||
throw new RuntimeException(death);
|
||||
}
|
||||
}
|
||||
|
||||
Iterator var6 = this.anno.values.iterator();
|
||||
|
||||
while(var6.hasNext()){
|
||||
Pair var7 = (Pair)var6.next();
|
||||
var1.put(var7.fst, var7.snd);
|
||||
for(Pair var7 : this.anno.values){
|
||||
map.put(var7.fst, var7.snd);
|
||||
}
|
||||
|
||||
return var1;
|
||||
return map;
|
||||
}
|
||||
|
||||
private void handleSymbol(Symbol sym, LinkedHashMap map){
|
||||
|
||||
if(sym.getKind() == ElementKind.METHOD){
|
||||
MethodSymbol var4 = (MethodSymbol)sym;
|
||||
Attribute var5 = var4.getDefaultValue();
|
||||
if(var5 != null){
|
||||
map.put(var4, var5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Object generateValue(MethodSymbol var1, Attribute var2){
|
||||
@@ -87,70 +120,6 @@ public class AnnotationProxyMaker{
|
||||
return var3.getValue(var2);
|
||||
}
|
||||
|
||||
private static final class MirroredTypesExceptionProxy extends ExceptionProxy{
|
||||
static final long serialVersionUID = 269L;
|
||||
private transient List<TypeMirror> types;
|
||||
private final String typeStrings;
|
||||
|
||||
MirroredTypesExceptionProxy(List<TypeMirror> var1){
|
||||
this.types = var1;
|
||||
this.typeStrings = var1.toString();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return this.typeStrings;
|
||||
}
|
||||
|
||||
public int hashCode(){
|
||||
return (this.types != null ? this.types : this.typeStrings).hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object var1){
|
||||
return this.types != null && var1 instanceof AnnotationProxyMaker.MirroredTypesExceptionProxy && this.types.equals(((AnnotationProxyMaker.MirroredTypesExceptionProxy)var1).types);
|
||||
}
|
||||
|
||||
protected RuntimeException generateException(){
|
||||
return new MirroredTypesException(this.types);
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException{
|
||||
var1.defaultReadObject();
|
||||
this.types = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class MirroredTypeExceptionProxy extends ExceptionProxy{
|
||||
static final long serialVersionUID = 269L;
|
||||
private transient TypeMirror type;
|
||||
private final String typeString;
|
||||
|
||||
MirroredTypeExceptionProxy(TypeMirror var1){
|
||||
this.type = var1;
|
||||
this.typeString = var1.toString();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return this.typeString;
|
||||
}
|
||||
|
||||
public int hashCode(){
|
||||
return (this.type != null ? this.type : this.typeString).hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object var1){
|
||||
return this.type != null && var1 instanceof AnnotationProxyMaker.MirroredTypeExceptionProxy && this.type.equals(((AnnotationProxyMaker.MirroredTypeExceptionProxy)var1).type);
|
||||
}
|
||||
|
||||
protected RuntimeException generateException(){
|
||||
return new MirroredTypeException(this.type);
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException{
|
||||
var1.defaultReadObject();
|
||||
this.type = null;
|
||||
}
|
||||
}
|
||||
|
||||
private class ValueVisitor implements Visitor{
|
||||
private MethodSymbol meth;
|
||||
private Class<?> returnClass;
|
||||
@@ -182,7 +151,7 @@ public class AnnotationProxyMaker{
|
||||
}
|
||||
|
||||
public void visitClass(com.sun.tools.javac.code.Attribute.Class var1){
|
||||
this.value = new AnnotationProxyMaker.MirroredTypeExceptionProxy(var1.classType);
|
||||
this.value = mirrorProxy(var1.classType);
|
||||
}
|
||||
|
||||
public void visitArray(Array var1){
|
||||
@@ -199,7 +168,7 @@ public class AnnotationProxyMaker{
|
||||
var14.append(var8);
|
||||
}
|
||||
|
||||
this.value = new AnnotationProxyMaker.MirroredTypesExceptionProxy(var14.toList());
|
||||
this.value = mirrorProxy(var14.toList());
|
||||
}else{
|
||||
int var3 = var1.values.length;
|
||||
Class var4 = this.returnClass;
|
||||
@@ -236,7 +205,7 @@ public class AnnotationProxyMaker{
|
||||
try{
|
||||
this.value = java.lang.Enum.valueOf((Class)this.returnClass, var2);
|
||||
}catch(IllegalArgumentException var4){
|
||||
this.value = new EnumConstantNotPresentExceptionProxy((Class)this.returnClass, var2);
|
||||
this.value = proxify(() -> new EnumConstantNotPresentException((Class)this.returnClass, var2));
|
||||
}
|
||||
}else{
|
||||
this.value = null;
|
||||
@@ -256,7 +225,7 @@ public class AnnotationProxyMaker{
|
||||
|
||||
public void visitError(Error var1){
|
||||
if(var1 instanceof UnresolvedClass){
|
||||
this.value = new AnnotationProxyMaker.MirroredTypeExceptionProxy(((UnresolvedClass)var1).classType);
|
||||
this.value = mirrorProxy(((UnresolvedClass)var1).classType);
|
||||
}else{
|
||||
this.value = null;
|
||||
}
|
||||
@@ -264,24 +233,30 @@ public class AnnotationProxyMaker{
|
||||
}
|
||||
|
||||
private void typeMismatch(Method var1, final Attribute var2){
|
||||
class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy{
|
||||
static final long serialVersionUID = 269L;
|
||||
final transient Method method;
|
||||
|
||||
AnnotationTypeMismatchExceptionProxy(Method var2x){
|
||||
this.method = var2x;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "<error>";
|
||||
}
|
||||
|
||||
protected RuntimeException generateException(){
|
||||
return new AnnotationTypeMismatchException(this.method, var2.type.toString());
|
||||
}
|
||||
}
|
||||
|
||||
this.value = new AnnotationTypeMismatchExceptionProxy(var1);
|
||||
this.value = proxify(() -> new AnnotationTypeMismatchException(var1, var2.type.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static Object mirrorProxy(Type t){
|
||||
return proxify(() -> new MirroredTypeException(t));
|
||||
}
|
||||
|
||||
private static Object mirrorProxy(List<Type> t){
|
||||
return proxify(() -> new MirroredTypesException(t));
|
||||
}
|
||||
|
||||
private static <T extends Throwable> Object proxify(Prov<T> prov){
|
||||
try{
|
||||
|
||||
return new ExceptionProxy(){
|
||||
@Override
|
||||
protected RuntimeException generateException(){
|
||||
return (RuntimeException)prov.get();
|
||||
}
|
||||
};
|
||||
}catch(Throwable t){
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,6 @@ public class Selement<T extends Element>{
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
return o != null && o.getClass() == getClass() && e == ((Selement)o).e;
|
||||
return o != null && o.getClass() == getClass() && e.equals(((Selement)o).e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ public class Smethod extends Selement<ExecutableElement>{
|
||||
return false;
|
||||
}
|
||||
|
||||
public String descString(){
|
||||
return up().asType().toString() + "#" + super.toString().replace("mindustry.gen.", "");
|
||||
}
|
||||
|
||||
public boolean is(Modifier mod){
|
||||
return e.getModifiers().contains(mod);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ public class Svar extends Selement<VariableElement>{
|
||||
super(e);
|
||||
}
|
||||
|
||||
public String descString(){
|
||||
return up().asType().toString() + "#" + super.toString().replace("mindustry.gen.", "");
|
||||
}
|
||||
|
||||
public JCVariableDecl jtree(){
|
||||
return (JCVariableDecl)BaseProcessor.elementu.getTree(e);
|
||||
}
|
||||
|
||||
@@ -25,13 +25,13 @@ public class TypeIOResolver{
|
||||
Seq<Svar> params = meth.params();
|
||||
//2 params, second one is type, first is writer
|
||||
if(params.size == 2 && params.first().tname().toString().equals("arc.util.io.Writes")){
|
||||
out.writers.put(params.get(1).tname().toString(), type.fullName() + "." + meth.name());
|
||||
out.writers.put(fix(params.get(1).tname().toString()), type.fullName() + "." + meth.name());
|
||||
}else if(params.size == 1 && params.first().tname().toString().equals("arc.util.io.Reads") && !meth.isVoid()){
|
||||
//1 param, one is reader, returns type
|
||||
out.readers.put(meth.retn().toString(), type.fullName() + "." + meth.name());
|
||||
out.readers.put(fix(meth.retn().toString()), type.fullName() + "." + meth.name());
|
||||
}else if(params.size == 2 && params.first().tname().toString().equals("arc.util.io.Reads") && !meth.isVoid() && meth.ret() == meth.params().get(1).mirror()){
|
||||
//2 params, one is reader, other is type, returns type - these are made to reduce garbage allocated
|
||||
out.mutatorReaders.put(meth.retn().toString(), type.fullName() + "." + meth.name());
|
||||
out.mutatorReaders.put(fix(meth.retn().toString()), type.fullName() + "." + meth.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,11 @@ public class TypeIOResolver{
|
||||
return out;
|
||||
}
|
||||
|
||||
/** makes sure type names don't contain 'gen' */
|
||||
private static String fix(String str){
|
||||
return str.replace("mindustry.gen", "");
|
||||
}
|
||||
|
||||
/** Information about read/write methods for class types. */
|
||||
public static class ClassSerializer{
|
||||
public final ObjectMap<String, String> writers, readers, mutatorReaders;
|
||||
|
||||
Reference in New Issue
Block a user