Repository navigation
Support for source enum extension #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lukebemish
wants to merge
9
commits into
neoforged:main
Choose a base branch
from
lukebemish:enum-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
59fcd41
Initial implementation and tests for enum extension via neo JSON format
lukebemish 082f2bf
Get marker annotation on extension enum entries working
lukebemish e3868ef
Disallow extension of abstract enums (since that will not work right …
lukebemish 61f210e
Add casts for field-backed parameters and handle ambiguity with multi…
lukebemish 7317852
Support indexed enums; requires indexed-enum annotation on source/cla…
lukebemish 9f8636c
Update README
lukebemish 5b252c8
Update README
lukebemish 00482ef
Handle case where IndexedEnum has default value of 0
lukebemish f6b8817
Fix streaming fields to go front->back
lukebemish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| plugins { | ||
| id 'java-library' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation project(':api') | ||
| implementation "com.google.code.gson:gson:${project.gson_version}" | ||
| } |
16 changes: 16 additions & 0 deletions
16
enumextensions/src/main/java/net/neoforged/jst/enumextensions/EnumExtensionPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package net.neoforged.jst.enumextensions; | ||
|
|
||
| import net.neoforged.jst.api.SourceTransformer; | ||
| import net.neoforged.jst.api.SourceTransformerPlugin; | ||
|
|
||
| public class EnumExtensionPlugin implements SourceTransformerPlugin { | ||
| @Override | ||
| public String getName() { | ||
| return "enum-extensions"; | ||
| } | ||
|
|
||
| @Override | ||
| public SourceTransformer createTransformer() { | ||
| return new EnumExtensionTransformer(); | ||
| } | ||
| } |
191 changes: 191 additions & 0 deletions
191
enumextensions/src/main/java/net/neoforged/jst/enumextensions/EnumExtensionTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| package net.neoforged.jst.enumextensions; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.intellij.psi.PsiFile; | ||
| import com.intellij.util.containers.MultiMap; | ||
| import net.neoforged.jst.api.Replacements; | ||
| import net.neoforged.jst.api.SourceTransformer; | ||
| import net.neoforged.jst.api.TransformContext; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import picocli.CommandLine; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.lang.constant.ClassDesc; | ||
| import java.lang.constant.MethodTypeDesc; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class EnumExtensionTransformer implements SourceTransformer { | ||
| private static final Gson GSON = new Gson(); | ||
|
|
||
| @Nullable | ||
| @CommandLine.Option(names = "--enum-extensions-stubs", description = "The path to a zip to save reference stubs in") | ||
| public Path stubOut; | ||
|
|
||
| @CommandLine.Option(names = "--enum-extensions-data", description = "The paths to read enum extension JSON files from") | ||
| public List<Path> paths = new ArrayList<>(); | ||
|
|
||
| @Nullable | ||
| @CommandLine.Option(names = "--enum-extensions-marker", description = "The name (binary representation) of an annotation to use as a marker for extended enum entries") | ||
| public String annotationMarker; | ||
|
|
||
| @Nullable | ||
| @CommandLine.Option(names = "--enum-extensions-required-interface", description = "The name (binary representation) of an interface to enforce that extendeable enums implement") | ||
| public String requiredInterface; | ||
|
|
||
| @Nullable | ||
| @CommandLine.Option(names = "--enum-extensions-reserved-constructor-annotation", description = "The name (binary representation) of an annotation annotating constructors that should not be used for extensions") | ||
| public String reservedConstructorAnnotation; | ||
|
|
||
| @Nullable | ||
| @CommandLine.Option(names = "--enum-extensions-indexed-enum-annotation", description = "The name (binary representation) of an annotation annotating enums with an index parameter") | ||
| public String indexedEnumAnnotation; | ||
|
|
||
| private MultiMap<String, ExtensionPrototype> extensions; | ||
| private StubStore stubs; | ||
| private String marker; | ||
| private String requiredInterfaceFqn; | ||
| private String reserverCtorAnnotationFqn; | ||
| private String indexedEnumAnnotationFqn; | ||
|
|
||
| @Override | ||
| public void beforeRun(TransformContext context) { | ||
| extensions = new MultiMap<>(); | ||
| stubs = new StubStore(context.environment().getPsiFacade()); | ||
|
|
||
| if (annotationMarker != null) { | ||
| marker = annotationMarker.replace('/', '.').replace('$', '.'); | ||
| } | ||
|
|
||
| if (requiredInterface != null) { | ||
| requiredInterfaceFqn = requiredInterface.replace('/', '.').replace('$', '.'); | ||
| } | ||
|
|
||
| if (reservedConstructorAnnotation != null) { | ||
| reserverCtorAnnotationFqn = reservedConstructorAnnotation.replace('/', '.').replace('$', '.'); | ||
| } | ||
|
|
||
| if (indexedEnumAnnotation != null) { | ||
| indexedEnumAnnotationFqn = indexedEnumAnnotation.replace('/', '.').replace('$', '.'); | ||
| } | ||
|
|
||
| for (Path path : paths) { | ||
| try { | ||
| var json = GSON.fromJson(Files.readString(path), JsonObject.class); | ||
| JsonArray entries = json.getAsJsonArray("entries"); | ||
| for (JsonElement entry : entries) { | ||
| JsonObject entryObj = entry.getAsJsonObject(); | ||
|
|
||
| String enumName = entryObj.get("enum").getAsString(); | ||
| String fieldName = entryObj.get("name").getAsString(); | ||
| String ctorDesc = entryObj.get("constructor").getAsString(); | ||
| JsonElement paramElem = entryObj.get("parameters"); | ||
| ExtensionPrototype.EnumParameters parameters = null; | ||
| if (paramElem.isJsonArray()) { | ||
| parameters = loadConstantParameters(context, enumName, fieldName, ctorDesc, paramElem.getAsJsonArray()); | ||
| } else if (paramElem.isJsonObject()) { | ||
| JsonObject obj = paramElem.getAsJsonObject(); | ||
| String className = obj.get("class").getAsString(); | ||
| if (obj.has("method")) { | ||
| String srcMethodName = obj.get("method").getAsString(); | ||
| parameters = new ExtensionPrototype.EnumParameters.MethodReference(className, srcMethodName); | ||
| } else if (obj.has("field")) { | ||
| String srcFieldName = obj.get("field").getAsString(); | ||
| parameters = new ExtensionPrototype.EnumParameters.FieldReference(className, srcFieldName); | ||
| } | ||
| } | ||
| extensions.putValue(enumName, new ExtensionPrototype( | ||
| fieldName, | ||
| ctorDesc, | ||
| parameters | ||
| )); | ||
| if (parameters == null) { | ||
| context.logger().error("Failed to read parameters for enum extension entry: %s", entryObj); | ||
| throw new IllegalArgumentException("Invalid parameters for enum extension entry"); | ||
| } | ||
| } | ||
| } catch (IOException exception) { | ||
| context.logger().error("Failed to read interface injection data file: %s", exception.getMessage()); | ||
| throw new UncheckedIOException(exception); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static ExtensionPrototype.EnumParameters loadConstantParameters(TransformContext context, String enumName, String fieldName, String ctorDesc, JsonArray obj) { | ||
| List<Object> params = new ArrayList<>(obj.size()); | ||
| var argTypes = MethodTypeDesc.ofDescriptor(ctorDesc).parameterArray(); | ||
| if (argTypes.length != obj.size()) { | ||
| var message = String.format( | ||
| "Parameter count %s does not match argument count %s of constructor %s for field %s in enum %s", | ||
| obj.size(), argTypes.length, ctorDesc, fieldName, enumName | ||
| ); | ||
| context.logger().error(message); | ||
| throw new IllegalArgumentException(message); | ||
| } | ||
|
|
||
| int idx = 0; | ||
| for (JsonElement element : obj) { | ||
| ClassDesc argType = argTypes[idx]; | ||
| switch (argType.descriptorString()) { | ||
| case "Z" -> params.add(element.getAsBoolean()); | ||
| case "C" -> { | ||
| String param = element.getAsString(); | ||
| if (param.length() != 1) { | ||
| var message = String.format( | ||
| "Invalid character %s at parameter index %s for field %s in enum %s", | ||
| param, idx, fieldName, enumName | ||
| ); | ||
| context.logger().error(message); | ||
| throw new IllegalArgumentException(message); | ||
| } | ||
| params.add(param.charAt(0)); | ||
| } | ||
| case "B" -> params.add(element.getAsByte()); | ||
| case "S" -> params.add(element.getAsShort()); | ||
| case "I" -> params.add(element.getAsInt()); | ||
| case "F" -> params.add(element.getAsFloat()); | ||
| case "J" -> params.add(element.getAsLong()); | ||
| case "D" -> params.add(element.getAsDouble()); | ||
| case "Ljava/lang/String;" -> params.add(element.isJsonNull() ? null : element.getAsString()); | ||
| default -> { | ||
| if (!element.isJsonNull()) { | ||
| var message = String.format( | ||
| "Unsupported immediate argument type %s at parameter index %s for field %s in enum %s", | ||
| argType, idx, fieldName, enumName | ||
| ); | ||
| context.logger().error(message); | ||
| throw new IllegalArgumentException(message); | ||
| } | ||
| params.add(null); | ||
| } | ||
| } | ||
| idx++; | ||
| } | ||
| return new ExtensionPrototype.EnumParameters.Constant(params); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean afterRun(TransformContext context) { | ||
| if (stubOut != null) { | ||
| try { | ||
| stubs.save(stubOut); | ||
| } catch (IOException e) { | ||
| context.logger().error("Failed to save stubs: %s", e.getMessage()); | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void visitFile(PsiFile psiFile, Replacements replacements) { | ||
| new EnumExtensionVisitor(replacements, extensions, stubs, marker, requiredInterfaceFqn, reserverCtorAnnotationFqn, indexedEnumAnnotationFqn).visitFile(psiFile); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind you'll have to update the readme too.