Add optional instance fields in upgraders
This commit is contained in:
parent
9118f2271b
commit
686cef66ac
@ -0,0 +1,42 @@
|
|||||||
|
package it.cavallium.datagen.plugin;
|
||||||
|
|
||||||
|
import com.squareup.javapoet.ClassName;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
public sealed interface JInterfaceLocation {
|
||||||
|
|
||||||
|
static JInterfaceLocation parse(String className, String instanceFieldLocation) {
|
||||||
|
if ((instanceFieldLocation != null) == (className != null)) {
|
||||||
|
if (instanceFieldLocation != null) {
|
||||||
|
throw new IllegalArgumentException("instance field location and class name are both set! You must set one");
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("instance field location and class name are both empty! You must set one");
|
||||||
|
}
|
||||||
|
} else if (instanceFieldLocation != null) {
|
||||||
|
var fieldClass = StringUtils.substringBeforeLast(instanceFieldLocation, ".");
|
||||||
|
var fieldName = StringUtils.substringAfterLast(instanceFieldLocation, ".");
|
||||||
|
var fieldClassName = ClassName.bestGuess(fieldClass);
|
||||||
|
return new JInterfaceLocationInstanceField(new FieldLocation(fieldClassName, fieldName));
|
||||||
|
} else {
|
||||||
|
return new JInterfaceLocationClassName(ClassName.bestGuess(className));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String getIdentifier();
|
||||||
|
|
||||||
|
record JInterfaceLocationClassName(ClassName className) implements JInterfaceLocation {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "C:" + className.reflectionName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record JInterfaceLocationInstanceField(FieldLocation fieldLocation) implements JInterfaceLocation {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "F:" + fieldLocation.className() + "." + fieldLocation.fieldName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
public String to;
|
public String to;
|
||||||
public String type;
|
public String type;
|
||||||
public String initializer;
|
public String initializer;
|
||||||
|
public String initializerInstance;
|
||||||
@Nullable
|
@Nullable
|
||||||
public Integer index;
|
public Integer index;
|
||||||
|
|
||||||
@ -22,6 +23,10 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
return "new-data";
|
return "new-data";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JInterfaceLocation getInitializerLocation() {
|
||||||
|
return JInterfaceLocation.parse(initializer, initializerInstance);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@ -33,6 +38,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
NewDataConfiguration that = (NewDataConfiguration) o;
|
NewDataConfiguration that = (NewDataConfiguration) o;
|
||||||
return Objects.equals(transformClass, that.transformClass) && Objects.equals(to, that.to)
|
return Objects.equals(transformClass, that.transformClass) && Objects.equals(to, that.to)
|
||||||
&& Objects.equals(type, that.type) && Objects.equals(initializer, that.initializer)
|
&& Objects.equals(type, that.type) && Objects.equals(initializer, that.initializer)
|
||||||
|
&& Objects.equals(initializerInstance, that.initializerInstance)
|
||||||
&& Objects.equals(index, that.index);
|
&& Objects.equals(index, that.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +49,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
hash += ConfigUtils.hashCode(to);
|
hash += ConfigUtils.hashCode(to);
|
||||||
hash += ConfigUtils.hashCode(type);
|
hash += ConfigUtils.hashCode(type);
|
||||||
hash += ConfigUtils.hashCode(initializer);
|
hash += ConfigUtils.hashCode(initializer);
|
||||||
|
hash += ConfigUtils.hashCode(initializerInstance);
|
||||||
hash += ConfigUtils.hashCode(index);
|
hash += ConfigUtils.hashCode(index);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
@ -51,6 +58,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
var c = new NewDataConfiguration();
|
var c = new NewDataConfiguration();
|
||||||
if (this.transformClass != null) c.transformClass = this.transformClass;
|
if (this.transformClass != null) c.transformClass = this.transformClass;
|
||||||
if (this.initializer != null) c.initializer = this.initializer;
|
if (this.initializer != null) c.initializer = this.initializer;
|
||||||
|
if (this.initializerInstance != null) c.initializerInstance = this.initializerInstance;
|
||||||
if (this.to != null) c.to = this.to;
|
if (this.to != null) c.to = this.to;
|
||||||
if (this.type != null) c.type = this.type;
|
if (this.type != null) c.type = this.type;
|
||||||
if (this.index != null) c.index = this.index;
|
if (this.index != null) c.index = this.index;
|
||||||
|
@ -8,6 +8,7 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
public String from;
|
public String from;
|
||||||
public String type;
|
public String type;
|
||||||
public String upgrader;
|
public String upgrader;
|
||||||
|
public String upgraderInstance;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTransformClass() {
|
public String getTransformClass() {
|
||||||
@ -19,6 +20,10 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
return "upgrade-data";
|
return "upgrade-data";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JInterfaceLocation getUpgraderLocation() {
|
||||||
|
return JInterfaceLocation.parse(upgrader, upgraderInstance);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@ -29,7 +34,8 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
}
|
}
|
||||||
UpgradeDataConfiguration that = (UpgradeDataConfiguration) o;
|
UpgradeDataConfiguration that = (UpgradeDataConfiguration) o;
|
||||||
return Objects.equals(transformClass, that.transformClass) && Objects.equals(from, that.from)
|
return Objects.equals(transformClass, that.transformClass) && Objects.equals(from, that.from)
|
||||||
&& Objects.equals(type, that.type) && Objects.equals(upgrader, that.upgrader);
|
&& Objects.equals(type, that.type) && Objects.equals(upgrader, that.upgrader)
|
||||||
|
&& Objects.equals(upgraderInstance, that.upgraderInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,6 +45,7 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
hash += ConfigUtils.hashCode(from);
|
hash += ConfigUtils.hashCode(from);
|
||||||
hash += ConfigUtils.hashCode(type);
|
hash += ConfigUtils.hashCode(type);
|
||||||
hash += ConfigUtils.hashCode(upgrader);
|
hash += ConfigUtils.hashCode(upgrader);
|
||||||
|
hash += ConfigUtils.hashCode(upgraderInstance);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +55,7 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
if (this.from != null) c.from = this.from;
|
if (this.from != null) c.from = this.from;
|
||||||
if (this.type != null) c.type = this.type;
|
if (this.type != null) c.type = this.type;
|
||||||
if (this.upgrader != null) c.upgrader = this.upgrader;
|
if (this.upgrader != null) c.upgrader = this.upgrader;
|
||||||
|
if (this.upgraderInstance != null) c.upgraderInstance = this.upgraderInstance;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,9 @@ import it.cavallium.datagen.plugin.ComputedType;
|
|||||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||||
import it.cavallium.datagen.plugin.ComputedTypeBase;
|
import it.cavallium.datagen.plugin.ComputedTypeBase;
|
||||||
import it.cavallium.datagen.plugin.ComputedVersion;
|
import it.cavallium.datagen.plugin.ComputedVersion;
|
||||||
|
import it.cavallium.datagen.plugin.JInterfaceLocation;
|
||||||
|
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationClassName;
|
||||||
|
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationInstanceField;
|
||||||
import it.cavallium.datagen.plugin.MoveDataConfiguration;
|
import it.cavallium.datagen.plugin.MoveDataConfiguration;
|
||||||
import it.cavallium.datagen.plugin.NewDataConfiguration;
|
import it.cavallium.datagen.plugin.NewDataConfiguration;
|
||||||
import it.cavallium.datagen.plugin.RemoveDataConfiguration;
|
import it.cavallium.datagen.plugin.RemoveDataConfiguration;
|
||||||
@ -116,7 +119,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
var newDataConfiguration = e.getValue();
|
var newDataConfiguration = e.getValue();
|
||||||
var computedTypes = dataModel.getComputedTypes(nextTypeBase.getVersion());
|
var computedTypes = dataModel.getComputedTypes(nextTypeBase.getVersion());
|
||||||
var newFieldType = Objects.requireNonNull(computedTypes.get(DataModel.fixType(newDataConfiguration.type)));
|
var newFieldType = Objects.requireNonNull(computedTypes.get(DataModel.fixType(newDataConfiguration.type)));
|
||||||
var initializerClass = ClassName.bestGuess(newDataConfiguration.initializer);
|
var initializerLocation = newDataConfiguration.getInitializerLocation();
|
||||||
var genericInitializerClass = ParameterizedTypeName.get(ClassName.get(DataInitializer.class),
|
var genericInitializerClass = ParameterizedTypeName.get(ClassName.get(DataInitializer.class),
|
||||||
newFieldType.getJTypeName(basePackageName).box()
|
newFieldType.getJTypeName(basePackageName).box()
|
||||||
);
|
);
|
||||||
@ -124,7 +127,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
var initializerName = createInitializerStaticField(nextInitializerStaticFieldId,
|
var initializerName = createInitializerStaticField(nextInitializerStaticFieldId,
|
||||||
initializerStaticFieldNames,
|
initializerStaticFieldNames,
|
||||||
classBuilder,
|
classBuilder,
|
||||||
initializerClass,
|
initializerLocation,
|
||||||
genericInitializerClass
|
genericInitializerClass
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -163,7 +166,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
if (!upgradeDataConfiguration.from.equals(fieldName)) {
|
if (!upgradeDataConfiguration.from.equals(fieldName)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var upgraderClass = ClassName.bestGuess(upgradeDataConfiguration.upgrader);
|
var upgraderImplementationLocation = upgradeDataConfiguration.getUpgraderLocation();
|
||||||
var cb = CodeBlock.builder();
|
var cb = CodeBlock.builder();
|
||||||
var newFieldType = Objects
|
var newFieldType = Objects
|
||||||
.requireNonNull(dataModel.getComputedTypes(nextTypeBase.getVersion()).get(DataModel.fixType(upgradeDataConfiguration.type)));
|
.requireNonNull(dataModel.getComputedTypes(nextTypeBase.getVersion()).get(DataModel.fixType(upgradeDataConfiguration.type)));
|
||||||
@ -175,7 +178,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
var upgraderName = createUpgraderStaticField(nextUpgraderStaticFieldId,
|
var upgraderName = createUpgraderStaticField(nextUpgraderStaticFieldId,
|
||||||
upgraderStaticFieldNames,
|
upgraderStaticFieldNames,
|
||||||
classBuilder,
|
classBuilder,
|
||||||
upgraderClass,
|
upgraderImplementationLocation,
|
||||||
genericUpgraderClass
|
genericUpgraderClass
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -222,18 +225,25 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
private String createInitializerStaticField(AtomicInteger nextInitializerStaticFieldId,
|
private String createInitializerStaticField(AtomicInteger nextInitializerStaticFieldId,
|
||||||
HashMap<String, String> initializerStaticFieldNames,
|
HashMap<String, String> initializerStaticFieldNames,
|
||||||
Builder classBuilder,
|
Builder classBuilder,
|
||||||
ClassName initializerClass,
|
JInterfaceLocation initializerLocation,
|
||||||
TypeName genericInitializerClass) {
|
TypeName genericInitializerClass) {
|
||||||
var ref = initializerClass.reflectionName();
|
var identifier = initializerLocation.getIdentifier();
|
||||||
var initializerName = initializerStaticFieldNames.get(ref);
|
var initializerName = initializerStaticFieldNames.get(identifier);
|
||||||
if (initializerName == null) {
|
if (initializerName == null) {
|
||||||
initializerName = "I" + nextInitializerStaticFieldId.getAndIncrement();
|
initializerName = "I" + nextInitializerStaticFieldId.getAndIncrement();
|
||||||
classBuilder.addField(FieldSpec
|
var fieldBuilder = FieldSpec
|
||||||
.builder(genericInitializerClass, initializerName)
|
.builder(genericInitializerClass, initializerName)
|
||||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
|
||||||
.initializer("new $T()", initializerClass)
|
switch (initializerLocation) {
|
||||||
.build());
|
case JInterfaceLocationClassName className -> fieldBuilder.initializer("new $T()", className.className());
|
||||||
initializerStaticFieldNames.put(ref, initializerName);
|
case JInterfaceLocationInstanceField instanceField -> fieldBuilder.initializer("$T.$N",
|
||||||
|
instanceField.fieldLocation().className(),
|
||||||
|
instanceField.fieldLocation().fieldName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
classBuilder.addField(fieldBuilder.build());
|
||||||
|
initializerStaticFieldNames.put(identifier, initializerName);
|
||||||
}
|
}
|
||||||
return initializerName;
|
return initializerName;
|
||||||
}
|
}
|
||||||
@ -241,10 +251,9 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
private String createUpgraderStaticField(AtomicInteger nextUpgraderStaticFieldId,
|
private String createUpgraderStaticField(AtomicInteger nextUpgraderStaticFieldId,
|
||||||
HashMap<String, String> upgraderStaticFieldNames,
|
HashMap<String, String> upgraderStaticFieldNames,
|
||||||
Builder classBuilder,
|
Builder classBuilder,
|
||||||
ClassName upgraderClass,
|
String upgraderImplementationLocation,
|
||||||
TypeName genericUpgraderClass) {
|
TypeName genericUpgraderClass) {
|
||||||
var ref = upgraderClass.reflectionName();
|
var upgraderName = upgraderStaticFieldNames.get(upgraderImplementationLocation);
|
||||||
var upgraderName = upgraderStaticFieldNames.get(ref);
|
|
||||||
if (upgraderName == null) {
|
if (upgraderName == null) {
|
||||||
upgraderName = "U" + nextUpgraderStaticFieldId.getAndIncrement();
|
upgraderName = "U" + nextUpgraderStaticFieldId.getAndIncrement();
|
||||||
classBuilder.addField(FieldSpec
|
classBuilder.addField(FieldSpec
|
||||||
@ -252,7 +261,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
||||||
.initializer("new $T()", upgraderClass)
|
.initializer("new $T()", upgraderClass)
|
||||||
.build());
|
.build());
|
||||||
upgraderStaticFieldNames.put(ref, upgraderName);
|
upgraderStaticFieldNames.put(upgraderImplementationLocation, upgraderName);
|
||||||
}
|
}
|
||||||
return upgraderName;
|
return upgraderName;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user