-
-
Notifications
You must be signed in to change notification settings - Fork 407
Support for the SyntaxInfo suppliers #8124
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
Pesekjak
wants to merge
6
commits into
SkriptLang:dev/patch
Choose a base branch
from
Pesekjak:patch/syntaxinfo-supply
base: dev/patch
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
6 commits
Select commit
Hold shift + click to select a range
76ea4ab
added support for the syntax info suppliers when converting to the le…
Pesekjak 4bc9c5c
Merge remote-tracking branch 'refs/remotes/origin/dev/patch' into pat…
Pesekjak bbc9957
resolved conflicts
Pesekjak 04b1062
Update src/main/java/ch/njol/skript/lang/SkriptEventInfo.java
Pesekjak 39461d7
Update src/main/java/org/skriptlang/skript/util/ClassUtils.java
Pesekjak 38ecbd8
Update src/main/java/org/skriptlang/skript/util/ClassUtils.java
Pesekjak 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
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package org.skriptlang.skript.util; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.lang.invoke.*; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Utilities for interacting with classes. | ||
|
@@ -16,4 +23,36 @@ public static boolean isNormalClass(Class<?> clazz) { | |
&& !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()); | ||
} | ||
|
||
private static final Map<Class<?>, Supplier<?>> instanceSuppliers = new ConcurrentHashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think a cache is necessary? The method would probably only be called once per registered syntax, right? |
||
|
||
/** | ||
* Creates a supplier for the given class if its nullary constructor exists. | ||
* | ||
* @param type class to create the supplier for | ||
* @return supplier for the instances of given class, using its nullary constructor | ||
* @param <T> type | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> Supplier<T> instanceSupplier(Class<T> type) throws Throwable { | ||
Supplier<?> cached = instanceSuppliers.get(type); | ||
if (cached != null) | ||
return (Supplier<T>) cached; | ||
Preconditions.checkArgument( | ||
!Modifier.isAbstract(type.getModifiers()) && !Modifier.isInterface(type.getModifiers()), | ||
"You cannot create instance suppliers for abstract classes"); | ||
Constructor<T> nullaryConstructor = type.getDeclaredConstructor(); | ||
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(type, MethodHandles.lookup()); | ||
MethodHandle methodHandle = lookup.unreflectConstructor(nullaryConstructor); | ||
CallSite callSite = LambdaMetafactory.metafactory(lookup, | ||
"get", | ||
MethodType.methodType(Supplier.class), | ||
MethodType.methodType(Object.class), | ||
methodHandle, | ||
methodHandle.type() | ||
); | ||
Supplier<T> created = (Supplier<T>) callSite.getTarget().invokeExact(); | ||
instanceSuppliers.put(type, created); | ||
return created; | ||
} | ||
|
||
} |
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.
I think it'd be fine to set the supplier when the object is constructed so that the field can continue to be final.
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.
I think creating a lot of metafactories can get fairly expensive so I wanted to initialize them lazily. I'll check the performance cost during startup and see how bad it gets.