Source reformat
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
public class Annotations{
|
||||
|
||||
@@ -19,25 +16,25 @@ public class Annotations{
|
||||
|
||||
}
|
||||
|
||||
/** Marks a class as serializable.*/
|
||||
/** Marks a class as serializable. */
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Serialize{
|
||||
|
||||
}
|
||||
|
||||
/** Marks a class as a special value type struct. Class name must end in 'Struct'.*/
|
||||
/** Marks a class as a special value type struct. Class name must end in 'Struct'. */
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Struct{
|
||||
|
||||
}
|
||||
|
||||
/**Marks a field of a struct. Optional.*/
|
||||
/** Marks a field of a struct. Optional. */
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface StructField{
|
||||
/**Size of a struct field in bits. Not valid on booleans or floating point numbers.*/
|
||||
/** Size of a struct field in bits. Not valid on booleans or floating point numbers. */
|
||||
int value();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import io.anuke.annotations.MethodEntry;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/** Represents a class witha list method entries to include in it. */
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import io.anuke.annotations.Annotations.Loc;
|
||||
import io.anuke.annotations.Annotations.PacketPriority;
|
||||
import io.anuke.annotations.Annotations.Variant;
|
||||
import io.anuke.annotations.Annotations.*;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import com.squareup.javapoet.FieldSpec;
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
import com.squareup.javapoet.*;
|
||||
import io.anuke.annotations.Annotations.Loc;
|
||||
import io.anuke.annotations.Annotations.Remote;
|
||||
import io.anuke.annotations.IOFinder.ClassSerializer;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -21,14 +16,14 @@ import java.util.stream.Collectors;
|
||||
/** The annotation processor for generating remote method call code. */
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_8)
|
||||
@SupportedAnnotationTypes({
|
||||
"io.anuke.annotations.Annotations.Remote",
|
||||
"io.anuke.annotations.Annotations.WriteClass",
|
||||
"io.anuke.annotations.Annotations.ReadClass",
|
||||
"io.anuke.annotations.Annotations.Remote",
|
||||
"io.anuke.annotations.Annotations.WriteClass",
|
||||
"io.anuke.annotations.Annotations.ReadClass",
|
||||
})
|
||||
public class RemoteMethodAnnotationProcessor extends AbstractProcessor{
|
||||
/** Maximum size of each event packet. */
|
||||
public static final int maxPacketSize = 4096;
|
||||
/** Warning on top of each autogenerated file.*/
|
||||
/** Warning on top of each autogenerated file. */
|
||||
public static final String autogenWarning = "Autogenerated file. Do not modify!\n";
|
||||
/** Name of the base package to put all the generated classes. */
|
||||
private static final String packageName = "io.anuke.mindustry.gen";
|
||||
@@ -37,7 +32,7 @@ public class RemoteMethodAnnotationProcessor extends AbstractProcessor{
|
||||
private static final String readServerName = "RemoteReadServer";
|
||||
/** Name of class that handles reading and invoking packets on the client. */
|
||||
private static final String readClientName = "RemoteReadClient";
|
||||
/**Simple class name of generated class name.*/
|
||||
/** Simple class name of generated class name. */
|
||||
private static final String callLocation = "Call";
|
||||
|
||||
/** Processing round number. */
|
||||
@@ -115,7 +110,7 @@ public class RemoteMethodAnnotationProcessor extends AbstractProcessor{
|
||||
|
||||
//create and add entry
|
||||
MethodEntry method = new MethodEntry(entry.name, Utils.getMethodName(element), annotation.targets(), annotation.variants(),
|
||||
annotation.called(), annotation.unreliable(), annotation.forward(), lastMethodID++, (ExecutableElement) element, annotation.priority());
|
||||
annotation.called(), annotation.unreliable(), annotation.forward(), lastMethodID++, (ExecutableElement)element, annotation.priority());
|
||||
|
||||
entry.methods.add(method);
|
||||
methods.add(method);
|
||||
@@ -140,7 +135,7 @@ public class RemoteMethodAnnotationProcessor extends AbstractProcessor{
|
||||
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", Objects.hash(methods)).build());
|
||||
|
||||
//build and write resulting hash class
|
||||
TypeSpec spec = hashBuilder.build();
|
||||
|
||||
@@ -3,9 +3,7 @@ package io.anuke.annotations;
|
||||
import com.squareup.javapoet.*;
|
||||
import io.anuke.annotations.IOFinder.ClassSerializer;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -25,24 +23,23 @@ public class RemoteReadGenerator{
|
||||
|
||||
/**
|
||||
* Generates a class for reading remote invoke packets.
|
||||
*
|
||||
* @param entries List of methods to use.
|
||||
* @param className Simple target class name.
|
||||
* @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 IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, IOException{
|
||||
throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, IOException{
|
||||
|
||||
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC);
|
||||
classBuilder.addJavadoc(RemoteMethodAnnotationProcessor.autogenWarning);
|
||||
|
||||
//create main method builder
|
||||
MethodSpec.Builder readMethod = MethodSpec.methodBuilder("readPacket")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addParameter(ByteBuffer.class, "buffer") //buffer to read form
|
||||
.addParameter(int.class, "id") //ID of method type to read
|
||||
.returns(void.class);
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addParameter(ByteBuffer.class, "buffer") //buffer to read form
|
||||
.addParameter(int.class, "id") //ID of method type to read
|
||||
.returns(void.class);
|
||||
|
||||
if(needsPlayer){
|
||||
//since the player type isn't loaded yet, creating a type def is necessary
|
||||
@@ -115,13 +112,13 @@ public class RemoteReadGenerator{
|
||||
|
||||
//execute the relevant method before the forward
|
||||
//if it throws a ValidateException, the method won't be forwarded
|
||||
readBlock.addStatement("$N." + entry.element.getSimpleName() + "(" + varResult.toString() + ")", ((TypeElement) entry.element.getEnclosingElement()).getQualifiedName().toString());
|
||||
readBlock.addStatement("$N." + entry.element.getSimpleName() + "(" + varResult.toString() + ")", ((TypeElement)entry.element.getEnclosingElement()).getQualifiedName().toString());
|
||||
|
||||
//call forwarded method, don't forward on the client reader
|
||||
if(entry.forward && entry.where.isServer && needsPlayer){
|
||||
//call forwarded method
|
||||
readBlock.addStatement(packageName + "." + entry.className + "." + entry.element.getSimpleName() +
|
||||
"__forward(player.con.id" + (varResult.length() == 0 ? "" : ", ") + varResult.toString() + ")");
|
||||
"__forward(player.con.id" + (varResult.length() == 0 ? "" : ", ") + varResult.toString() + ")");
|
||||
}
|
||||
|
||||
readBlock.nextControlFlow("catch (java.lang.Exception e)");
|
||||
|
||||
@@ -4,10 +4,7 @@ import com.squareup.javapoet.*;
|
||||
import io.anuke.annotations.Annotations.Loc;
|
||||
import io.anuke.annotations.IOFinder.ClassSerializer;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -33,7 +30,7 @@ public class RemoteWriteGenerator{
|
||||
|
||||
//add temporary write buffer
|
||||
classBuilder.addField(FieldSpec.builder(ByteBuffer.class, "TEMP_BUFFER", Modifier.STATIC, Modifier.PRIVATE, Modifier.FINAL)
|
||||
.initializer("ByteBuffer.allocate($1L)", RemoteMethodAnnotationProcessor.maxPacketSize).build());
|
||||
.initializer("ByteBuffer.allocate($1L)", RemoteMethodAnnotationProcessor.maxPacketSize).build());
|
||||
|
||||
//go through each method entry in this class
|
||||
for(MethodEntry methodEntry : entry.methods){
|
||||
@@ -65,8 +62,8 @@ public class RemoteWriteGenerator{
|
||||
|
||||
//create builder
|
||||
MethodSpec.Builder method = MethodSpec.methodBuilder(elem.getSimpleName().toString() + (forwarded ? "__forward" : "")) //add except suffix when forwarding
|
||||
.addModifiers(Modifier.STATIC, Modifier.SYNCHRONIZED)
|
||||
.returns(void.class);
|
||||
.addModifiers(Modifier.STATIC, Modifier.SYNCHRONIZED)
|
||||
.returns(void.class);
|
||||
|
||||
//forwarded methods aren't intended for use, and are not public
|
||||
if(!forwarded){
|
||||
@@ -119,7 +116,7 @@ public class RemoteWriteGenerator{
|
||||
|
||||
//add the statement to call it
|
||||
method.addStatement("$N." + elem.getSimpleName() + "(" + results.toString() + ")",
|
||||
((TypeElement) elem.getEnclosingElement()).getQualifiedName().toString());
|
||||
((TypeElement)elem.getEnclosingElement()).getQualifiedName().toString());
|
||||
|
||||
if(methodEntry.local != Loc.both){
|
||||
method.endControlFlow();
|
||||
@@ -170,7 +167,7 @@ public class RemoteWriteGenerator{
|
||||
method.addStatement("TEMP_BUFFER.put(" + varName + " ? (byte)1 : 0)");
|
||||
}else{
|
||||
method.addStatement("TEMP_BUFFER.put" +
|
||||
capName + "(" + varName + ")");
|
||||
capName + "(" + varName + ")");
|
||||
}
|
||||
}else{
|
||||
//else, try and find a serializer
|
||||
@@ -209,7 +206,7 @@ public class RemoteWriteGenerator{
|
||||
|
||||
//send the actual packet
|
||||
method.addStatement("io.anuke.mindustry.net.Net." + sendString + "packet, " +
|
||||
(methodEntry.unreliable ? "io.anuke.mindustry.net.Net.SendMode.udp" : "io.anuke.mindustry.net.Net.SendMode.tcp") + ")");
|
||||
(methodEntry.unreliable ? "io.anuke.mindustry.net.Net.SendMode.udp" : "io.anuke.mindustry.net.Net.SendMode.tcp") + ")");
|
||||
|
||||
|
||||
//end check for server/client
|
||||
@@ -221,7 +218,7 @@ public class RemoteWriteGenerator{
|
||||
|
||||
private String getCheckString(Loc loc){
|
||||
return loc.isClient && loc.isServer ? "io.anuke.mindustry.net.Net.server() || io.anuke.mindustry.net.Net.client()" :
|
||||
loc.isClient ? "io.anuke.mindustry.net.Net.client()" :
|
||||
loc.isServer ? "io.anuke.mindustry.net.Net.server()" : "false";
|
||||
loc.isClient ? "io.anuke.mindustry.net.Net.client()" :
|
||||
loc.isServer ? "io.anuke.mindustry.net.Net.server()" : "false";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,9 @@ import io.anuke.annotations.Annotations.Serialize;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.util.ElementFilter;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -21,7 +17,7 @@ import java.util.Set;
|
||||
"io.anuke.annotations.Annotations.Serialize"
|
||||
})
|
||||
public class SerializeAnnotationProcessor extends AbstractProcessor{
|
||||
/**Target class name.*/
|
||||
/** Target class name. */
|
||||
private static final String className = "Serialization";
|
||||
/** Name of the base package to put all the generated classes. */
|
||||
private static final String packageName = "io.anuke.mindustry.gen";
|
||||
@@ -58,8 +54,8 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
||||
classBuilder.addField(jsonType, "bjson", Modifier.STATIC, Modifier.PRIVATE);
|
||||
classBuilder.addField(ubJsonReaderType, "bjsonReader", Modifier.STATIC, Modifier.PRIVATE);
|
||||
classBuilder.addStaticBlock(CodeBlock.builder()
|
||||
.addStatement("bjson = new " + jsonType + "()")
|
||||
.addStatement("bjsonReader = new " + ubJsonReaderType + "()")
|
||||
.addStatement("bjson = new " + jsonType + "()")
|
||||
.addStatement("bjsonReader = new " + ubJsonReaderType + "()")
|
||||
.build());
|
||||
|
||||
for(TypeElement elem : elements){
|
||||
@@ -99,7 +95,8 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
||||
|
||||
List<VariableElement> fields = ElementFilter.fieldsIn(Utils.elementUtils.getAllMembers(elem));
|
||||
for(VariableElement field : fields){
|
||||
if(field.getModifiers().contains(Modifier.STATIC) || field.getModifiers().contains(Modifier.TRANSIENT) || field.getModifiers().contains(Modifier.PRIVATE)) continue;
|
||||
if(field.getModifiers().contains(Modifier.STATIC) || field.getModifiers().contains(Modifier.TRANSIENT) || field.getModifiers().contains(Modifier.PRIVATE))
|
||||
continue;
|
||||
|
||||
String name = field.getSimpleName().toString();
|
||||
String typeName = Utils.typeUtils.erasure(field.asType()).toString().replace('$', '.');
|
||||
@@ -109,11 +106,11 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
||||
writeMethod.addStatement("stream.write" + capName + "(object." + name + ")");
|
||||
readMethod.addStatement("object." + name + "= stream.read" + capName + "()");
|
||||
|
||||
jsonWriteMethod.addStatement("json.writeValue(\"" + name + "\", object." + name +")");
|
||||
jsonWriteMethod.addStatement("json.writeValue(\"" + name + "\", object." + name + ")");
|
||||
jsonReadMethod.addStatement("if(value.has(\"" + name + "\")) object." + name + "= value.get" + capName + "(\"" + name + "\")");
|
||||
}else{
|
||||
writeMethod.addStatement("io.anuke.arc.Core.settings.getSerializer(" + typeName+ ".class).write(stream, object." + name + ")");
|
||||
readMethod.addStatement("object." + name + " = (" +typeName+")io.anuke.arc.Core.settings.getSerializer(" + typeName+ ".class).read(stream)");
|
||||
writeMethod.addStatement("io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).write(stream, object." + name + ")");
|
||||
readMethod.addStatement("object." + name + " = (" + typeName + ")io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).read(stream)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +120,7 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
||||
serializer.addMethod(writeMethod.build());
|
||||
serializer.addMethod(readMethod.build());
|
||||
|
||||
method.addStatement("io.anuke.arc.Core.settings.setSerializer($N, $L)", Utils.elementUtils.getBinaryName(elem).toString().replace('$', '.') + ".class", serializer.build());
|
||||
method.addStatement("io.anuke.arc.Core.settings.setSerializer($N, $L)", Utils.elementUtils.getBinaryName(elem).toString().replace('$', '.') + ".class", serializer.build());
|
||||
|
||||
name(writeMethod, "write" + simpleTypeName);
|
||||
name(readMethod, "read" + simpleTypeName);
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.MethodSpec;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
import com.squareup.javapoet.*;
|
||||
import io.anuke.annotations.Annotations.Struct;
|
||||
import io.anuke.annotations.Annotations.StructField;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
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;
|
||||
|
||||
/**Generates ""value types"" classes that are packed into integer primitives of the most aproppriate size.
|
||||
* It would be nice if Java didn't make crazy hacks like this necessary.*/
|
||||
/**
|
||||
* Generates ""value types"" classes that are packed into integer primitives of the most aproppriate size.
|
||||
* It would be nice if Java didn't make crazy hacks like this necessary.
|
||||
*/
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_8)
|
||||
@SupportedAnnotationTypes({
|
||||
"io.anuke.annotations.Annotations.Struct"
|
||||
"io.anuke.annotations.Annotations.Struct"
|
||||
})
|
||||
public class StructAnnotationProcessor extends AbstractProcessor{
|
||||
/** Name of the base package to put all the generated classes. */
|
||||
@@ -57,7 +54,7 @@ public class StructAnnotationProcessor extends AbstractProcessor{
|
||||
String structParam = structName.toLowerCase();
|
||||
|
||||
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(structName)
|
||||
.addModifiers(Modifier.FINAL, Modifier.PUBLIC);
|
||||
.addModifiers(Modifier.FINAL, Modifier.PUBLIC);
|
||||
|
||||
try{
|
||||
List<VariableElement> variables = ElementFilter.fieldsIn(elem.getEnclosedElements());
|
||||
@@ -92,14 +89,14 @@ public class StructAnnotationProcessor extends AbstractProcessor{
|
||||
|
||||
//[get] field(structType) : fieldType
|
||||
MethodSpec.Builder getter = MethodSpec.methodBuilder(var.getSimpleName().toString())
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.returns(varType)
|
||||
.addParameter(structType, structParam);
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.returns(varType)
|
||||
.addParameter(structType, structParam);
|
||||
//[set] field(structType, fieldType) : structType
|
||||
MethodSpec.Builder setter = MethodSpec.methodBuilder(var.getSimpleName().toString())
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.returns(structType)
|
||||
.addParameter(structType, structParam).addParameter(varType, "value");
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.returns(structType)
|
||||
.addParameter(structType, structParam).addParameter(varType, "value");
|
||||
|
||||
//[getter]
|
||||
if(varType == TypeName.BOOLEAN){
|
||||
@@ -209,7 +206,7 @@ public class StructAnnotationProcessor extends AbstractProcessor{
|
||||
throw new IllegalArgumentException("Too many fields, must fit in 64 bits. Curent size: " + size);
|
||||
}
|
||||
|
||||
/**returns a type's element size in bits.*/
|
||||
/** returns a type's element size in bits. */
|
||||
static int typeSize(TypeKind kind) throws IllegalArgumentException{
|
||||
switch(kind){
|
||||
case BOOLEAN:
|
||||
|
||||
@@ -14,11 +14,11 @@ public class Utils{
|
||||
public static Messager messager;
|
||||
|
||||
public static String getMethodName(Element element){
|
||||
return ((TypeElement) element.getEnclosingElement()).getQualifiedName().toString() + "." + element.getSimpleName();
|
||||
return ((TypeElement)element.getEnclosingElement()).getQualifiedName().toString() + "." + element.getSimpleName();
|
||||
}
|
||||
|
||||
public static boolean isPrimitive(String type){
|
||||
return type.equals("boolean") || type.equals("byte") || type.equals("short") || type.equals("int")
|
||||
|| type.equals("long") || type.equals("float") || type.equals("double") || type.equals("char");
|
||||
|| type.equals("long") || type.equals("float") || type.equals("double") || type.equals("char");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user