Updated older annotation processors

This commit is contained in:
Anuken
2020-07-03 20:26:23 -04:00
parent 8576d535bd
commit 685275237a
4 changed files with 46 additions and 50 deletions

View File

@@ -1,17 +1,14 @@
package mindustry.annotations.impl;
import arc.struct.*;
import com.squareup.javapoet.*;
import mindustry.annotations.Annotations.*;
import mindustry.annotations.*;
import mindustry.annotations.Annotations.Struct;
import mindustry.annotations.Annotations.StructField;
import mindustry.annotations.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic.Kind;
import java.util.List;
import java.util.Set;
import javax.lang.model.type.*;
/**
* Generates ""value types"" classes that are packed into integer primitives of the most aproppriate size.
@@ -24,28 +21,28 @@ public class StructProcess extends BaseProcessor{
@Override
public void process(RoundEnvironment env) throws Exception{
Set<TypeElement> elements = ElementFilter.typesIn(env.getElementsAnnotatedWith(Struct.class));
Seq<Stype> elements = types(Struct.class);
for(TypeElement elem : elements){
for(Stype elem : elements){
if(!elem.getSimpleName().toString().endsWith("Struct")){
BaseProcessor.err("All classes annotated with @Struct must have their class names end in 'Struct'.", elem);
if(!elem.name().endsWith("Struct")){
err("All classes annotated with @Struct must have their class names end in 'Struct'.", elem);
continue;
}
String structName = elem.getSimpleName().toString().substring(0, elem.getSimpleName().toString().length() - "Struct".length());
String structName = elem.name().substring(0, elem.name().length() - "Struct".length());
String structParam = structName.toLowerCase();
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(structName)
.addModifiers(Modifier.FINAL, Modifier.PUBLIC);
try{
List<VariableElement> variables = ElementFilter.fieldsIn(elem.getEnclosedElements());
int structSize = variables.stream().mapToInt(StructProcess::varSize).sum();
Seq<Svar> variables = elem.fields();
int structSize = variables.mapInt(StructProcess::varSize).sum();
int structTotalSize = (structSize <= 8 ? 8 : structSize <= 16 ? 16 : structSize <= 32 ? 32 : 64);
if(variables.size() == 0){
BaseProcessor.err("making a struct with no fields is utterly pointles.", elem);
if(variables.size == 0){
err("making a struct with no fields is utterly pointles.", elem);
continue;
}
@@ -62,21 +59,21 @@ public class StructProcess extends BaseProcessor{
doc.append("Bits used: ").append(structSize).append(" / ").append(structTotalSize).append("\n");
int offset = 0;
for(VariableElement var : variables){
for(Svar var : variables){
int size = varSize(var);
TypeName varType = TypeName.get(var.asType());
String varName = var.getSimpleName().toString();
TypeName varType = var.tname();
String varName = var.name();
//add val param to constructor
constructor.addParameter(varType, varName);
//[get] field(structType) : fieldType
MethodSpec.Builder getter = MethodSpec.methodBuilder(var.getSimpleName().toString())
MethodSpec.Builder getter = MethodSpec.methodBuilder(var.name().toString())
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
.returns(varType)
.addParameter(structType, structParam);
//[set] field(structType, fieldType) : structType
MethodSpec.Builder setter = MethodSpec.methodBuilder(var.getSimpleName().toString())
MethodSpec.Builder setter = MethodSpec.methodBuilder(var.name().toString())
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
.returns(structType)
.addParameter(structType, structParam).addParameter(varType, "value");
@@ -133,7 +130,7 @@ public class StructProcess extends BaseProcessor{
JavaFile.builder(packageName, classBuilder.build()).build().writeTo(BaseProcessor.filer);
}catch(IllegalArgumentException e){
e.printStackTrace();
BaseProcessor.err(e.getMessage(), elem);
err(e.getMessage(), elem);
}
}
@@ -154,21 +151,21 @@ public class StructProcess extends BaseProcessor{
return "0b" + builder.reverse().toString() + "L";
}
static int varSize(VariableElement var) throws IllegalArgumentException{
if(!var.asType().getKind().isPrimitive()){
static int varSize(Svar var) throws IllegalArgumentException{
if(!var.mirror().getKind().isPrimitive()){
throw new IllegalArgumentException("All struct fields must be primitives: " + var);
}
StructField an = var.getAnnotation(StructField.class);
if(var.asType().getKind() == TypeKind.BOOLEAN && an != null && an.value() != 1){
StructField an = var.annotation(StructField.class);
if(var.mirror().getKind() == TypeKind.BOOLEAN && an != null && an.value() != 1){
throw new IllegalArgumentException("Booleans can only be one bit long... why would you do this?");
}
if(var.asType().getKind() == TypeKind.FLOAT && an != null && an.value() != 32){
if(var.mirror().getKind() == TypeKind.FLOAT && an != null && an.value() != 32){
throw new IllegalArgumentException("Float size can't be changed. Very sad.");
}
return an == null ? typeSize(var.asType().getKind()) : an.value();
return an == null ? typeSize(var.mirror().getKind()) : an.value();
}
static Class<?> typeForSize(int size) throws IllegalArgumentException{

View File

@@ -1,15 +1,15 @@
package mindustry.annotations.remote;
import arc.struct.*;
import com.squareup.javapoet.*;
import mindustry.annotations.*;
import mindustry.annotations.Annotations.*;
import mindustry.annotations.*;
import mindustry.annotations.util.*;
import mindustry.annotations.util.TypeIOResolver.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import java.util.*;
import java.util.stream.*;
/** The annotation processor for generating remote method call code. */
@@ -33,13 +33,13 @@ public class RemoteProcess extends BaseProcessor{
//class serializers
private ClassSerializer serializer;
//all elements with the Remote annotation
private Set<? extends Element> elements;
private Seq<Smethod> elements;
//map of all classes to generate by name
private HashMap<String, ClassEntry> classMap;
//list of all method entries
private ArrayList<MethodEntry> methods;
private Seq<MethodEntry> methods;
//list of all method entries
private ArrayList<ClassEntry> classes;
private Seq<ClassEntry> classes;
{
rounds = 2;
@@ -54,23 +54,23 @@ public class RemoteProcess extends BaseProcessor{
//last method ID used
int lastMethodID = 0;
//find all elements with the Remote annotation
elements = roundEnv.getElementsAnnotatedWith(Remote.class);
elements = methods(Remote.class);
//map of all classes to generate by name
classMap = new HashMap<>();
//list of all method entries
methods = new ArrayList<>();
methods = new Seq<>();
//list of all method entries
classes = new ArrayList<>();
classes = new Seq<>();
List<Element> orderedElements = new ArrayList<>(elements);
orderedElements.sort(Comparator.comparing(Object::toString));
Seq<Smethod> orderedElements = elements.copy();
orderedElements.sortComparing(Object::toString);
//create methods
for(Element element : orderedElements){
Remote annotation = element.getAnnotation(Remote.class);
for(Smethod element : orderedElements){
Remote annotation = element.annotation(Remote.class);
//check for static
if(!element.getModifiers().contains(Modifier.STATIC) || !element.getModifiers().contains(Modifier.PUBLIC)){
if(!element.is(Modifier.STATIC) || !element.is(Modifier.PUBLIC)){
err("All @Remote methods must be public and static: ", element);
}
@@ -89,8 +89,8 @@ public class RemoteProcess extends BaseProcessor{
ClassEntry entry = classMap.get(callLocation);
//create and add entry
MethodEntry method = new MethodEntry(entry.name, BaseProcessor.getMethodName(element), annotation.targets(), annotation.variants(),
annotation.called(), annotation.unreliable(), annotation.forward(), lastMethodID++, (ExecutableElement)element, annotation.priority());
MethodEntry method = new MethodEntry(entry.name, BaseProcessor.getMethodName(element.e), annotation.targets(), annotation.variants(),
annotation.called(), annotation.unreliable(), annotation.forward(), lastMethodID++, element.e, annotation.priority());
entry.methods.add(method);
methods.add(method);
@@ -105,15 +105,15 @@ public class RemoteProcess extends BaseProcessor{
RemoteReadGenerator readgen = new RemoteReadGenerator(serializer);
//generate server readers
readgen.generateFor(methods.stream().filter(method -> method.where.isClient).collect(Collectors.toList()), readServerName, packageName, true);
readgen.generateFor(methods.select(method -> method.where.isClient), readServerName, packageName, true);
//generate client readers
readgen.generateFor(methods.stream().filter(method -> method.where.isServer).collect(Collectors.toList()), readClientName, packageName, false);
readgen.generateFor(methods.select(method -> method.where.isServer), readClientName, packageName, false);
//create class for storing unique method hash
TypeSpec.Builder hashBuilder = TypeSpec.classBuilder("MethodHash").addModifiers(Modifier.PUBLIC);
hashBuilder.addJavadoc(autogenWarning);
hashBuilder.addField(FieldSpec.builder(int.class, "HASH", Modifier.STATIC, Modifier.PUBLIC, Modifier.FINAL)
.initializer("$1L", Objects.hash(methods)).build());
.initializer("$1L", Arrays.hashCode(methods.map(m -> m.element).toArray())).build());
//build and write resulting hash class
TypeSpec spec = hashBuilder.build();

View File

@@ -1,12 +1,12 @@
package mindustry.annotations.remote;
import arc.struct.*;
import arc.util.io.*;
import com.squareup.javapoet.*;
import mindustry.annotations.*;
import mindustry.annotations.util.TypeIOResolver.*;
import javax.lang.model.element.*;
import java.util.*;
/** Generates code for reading remote invoke packets on the client and server. */
public class RemoteReadGenerator{
@@ -24,7 +24,7 @@ public class RemoteReadGenerator{
* @param packageName Full target package name.
* @param needsPlayer Whether this read method requires a reference to the player sender.
*/
public void generateFor(List<MethodEntry> entries, String className, String packageName, boolean needsPlayer) throws Exception{
public void generateFor(Seq<MethodEntry> entries, String className, String packageName, boolean needsPlayer) throws Exception{
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC);
classBuilder.addJavadoc(RemoteProcess.autogenWarning);

View File

@@ -9,7 +9,6 @@ import mindustry.annotations.util.TypeIOResolver.*;
import javax.lang.model.element.*;
import java.io.*;
import java.util.*;
/** Generates code for writing remote invoke packets on the client and server. */
public class RemoteWriteGenerator{
@@ -21,7 +20,7 @@ public class RemoteWriteGenerator{
}
/** Generates all classes in this list. */
public void generateFor(List<ClassEntry> entries, String packageName) throws IOException{
public void generateFor(Seq<ClassEntry> entries, String packageName) throws IOException{
for(ClassEntry entry : entries){
//create builder