Refactoring

This commit is contained in:
Anuken
2020-02-03 14:07:06 -05:00
parent f937cb30f0
commit 141cf518a2
56 changed files with 706 additions and 1418 deletions

View File

@@ -10,7 +10,14 @@ public class Annotations{
@Retention(RetentionPolicy.SOURCE)
public @interface Component{
/** Dependencies. */
Class[] value() default {};
//Class[] value() default {};
}
/** Indicates priority of a method in an entity. Methods with higher priority are done last. */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface MethodPriority{
float value();
}
/** Indicates that a component def is present on all entities. */

View File

@@ -21,6 +21,7 @@ import javax.lang.model.type.*;
public class EntityProcess extends BaseProcessor{
Array<Definition> definitions = new Array<>();
Array<Stype> baseComponents;
ObjectMap<String, Stype> componentNames = new ObjectMap<>();
ObjectMap<Stype, Array<Stype>> componentDependencies = new ObjectMap<>();
ObjectMap<Stype, Array<Stype>> defComponents = new ObjectMap<>();
ObjectSet<String> imports = new ObjectSet<>();
@@ -36,17 +37,12 @@ public class EntityProcess extends BaseProcessor{
if(round == 1){
baseComponents = types(BaseComponent.class);
Array<Stype> allDefs = types(EntityDef.class);
Array<Stype> allComponents = types(Component.class);
ObjectSet<Stype> allComponents = new ObjectSet<>();
//find all components used...
for(Stype type : allDefs){
allComponents.addAll(allComponents(type));
for(Stype type : allComponents){
componentNames.put(type.name(), type);
}
//add all components w/ dependencies
allComponents.addAll(types(Component.class).map(s -> Array.withArrays(getDependencies(s), s)).flatten());
//add component imports
for(Stype comp : allComponents){
imports.addAll(getImports(comp.e));
@@ -57,7 +53,7 @@ public class EntityProcess extends BaseProcessor{
TypeSpec.Builder inter = TypeSpec.interfaceBuilder(interfaceName(component)).addModifiers(Modifier.PUBLIC).addAnnotation(EntityInterface.class);
//implement extra interfaces these components may have, e.g. position
for(Stype extraInterface : component.interfaces()){
for(Stype extraInterface : component.interfaces().select(i -> !isCompInterface(i))){
inter.addSuperinterface(extraInterface.mirror());
}
@@ -130,6 +126,8 @@ public class EntityProcess extends BaseProcessor{
//add all methods from components
for(ObjectMap.Entry<String, Array<Smethod>> entry : methods){
entry.value.sort(m -> m.has(MethodPriority.class) ? m.annotation(MethodPriority.class).value() : 0);
//representative method
Smethod first = entry.value.first();
//build method using same params/returns
@@ -261,18 +259,8 @@ public class EntityProcess extends BaseProcessor{
Array<Stype> getDependencies(Stype component){
if(!componentDependencies.containsKey(component)){
ObjectSet<Stype> out = new ObjectSet<>();
out.addAll(component.superclasses());
//TODO extreme confusion
//out.addAll(component.interfaces().select(this::isComponent));
//get dependency classes
if(component.annotation(Component.class) != null){
try{
component.annotation(Component.class).value();
}catch(MirroredTypesException e){
out.addAll(Array.with(e.getTypeMirrors()).map(Stype::of));
}
}
//add base component interfaces
out.addAll(component.interfaces().select(this::isCompInterface).map(this::interfaceToComp));
//out now contains the base dependencies; finish constructing the tree
ObjectSet<Stype> result = new ObjectSet<>();
@@ -291,6 +279,15 @@ public class EntityProcess extends BaseProcessor{
return componentDependencies.get(component);
}
boolean isCompInterface(Stype type){
return interfaceToComp(type) != null;
}
Stype interfaceToComp(Stype type){
String name = type.name().substring(0, type.name().length() - 1) + "Comp";
return componentNames.get(name);
}
boolean isComponent(Stype type){
return type.annotation(Component.class) != null;
}

View File

@@ -5,6 +5,7 @@ import mindustry.annotations.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import java.lang.annotation.*;
public class Selement<T extends Element>{
public final T e;
@@ -13,6 +14,14 @@ public class Selement<T extends Element>{
this.e = e;
}
public <A extends Annotation> A annotation(Class<A> annotation){
return e.getAnnotation(annotation);
}
public <A extends Annotation> boolean has(Class<A> annotation){
return e.getAnnotation(annotation) != null;
}
public Element up(){
return e.getEnclosingElement();
}

View File

@@ -5,7 +5,6 @@ import mindustry.annotations.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import java.lang.annotation.*;
public class Stype extends Selement<TypeElement>{
@@ -35,10 +34,6 @@ public class Stype extends Selement<TypeElement>{
return new Stype((TypeElement)BaseProcessor.typeu.asElement(BaseProcessor.typeu.directSupertypes(mirror()).get(0)));
}
public <A extends Annotation> A annotation(Class<A> annotation){
return e.getAnnotation(annotation);
}
public Array<Svar> fields(){
return Array.with(e.getEnclosedElements()).select(e -> e instanceof VariableElement).map(e -> new Svar((VariableElement)e));
}