-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform listener classes to obtain privileged Lookups
- Loading branch information
Showing
12 changed files
with
190 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
103 changes: 103 additions & 0 deletions
103
.../applaunch/java/org/spongepowered/forge/applaunch/transformation/ListenerTransformer.java
This file contains 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,103 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.forge.applaunch.transformation; | ||
|
||
import static org.objectweb.asm.Opcodes.ACC_STATIC; | ||
import static org.objectweb.asm.Opcodes.INVOKESTATIC; | ||
|
||
import cpw.mods.modlauncher.api.ITransformer; | ||
import cpw.mods.modlauncher.api.ITransformerActivity; | ||
import cpw.mods.modlauncher.api.ITransformerVotingContext; | ||
import cpw.mods.modlauncher.api.TransformerVoteResult; | ||
import net.minecraftforge.fml.loading.LoadingModList; | ||
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo; | ||
import net.minecraftforge.forgespi.language.ModFileScanData; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.LdcInsnNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class ListenerTransformer implements ITransformer<ClassNode> { | ||
|
||
@NonNull | ||
@Override | ||
public ClassNode transform(final ClassNode input, final ITransformerVotingContext context) { | ||
MethodNode clinit = null; | ||
for (final MethodNode method : input.methods) { | ||
if (method.name.equals("<clinit>") && method.desc.equals("()V")) { | ||
clinit = method; | ||
break; | ||
} | ||
} | ||
|
||
if (clinit == null) { | ||
clinit = new MethodNode(ACC_STATIC, "<clinit>", "()V", null, null); | ||
input.methods.add(clinit); | ||
} | ||
|
||
|
||
final InsnList list = new InsnList(); | ||
list.add(new LdcInsnNode(Type.getObjectType(input.name))); | ||
list.add(new MethodInsnNode(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false)); | ||
list.add(new MethodInsnNode(INVOKESTATIC, "org/spongepowered/forge/launch/event/ListenerLookups", "set", "(Ljava/lang/Class;Ljava/lang/invoke/MethodHandles$Lookup;)V")); | ||
|
||
clinit.instructions.insert(list); | ||
|
||
return input; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public TransformerVoteResult castVote(final ITransformerVotingContext context) { | ||
return context.getReason().equals(ITransformerActivity.CLASSLOADING_REASON) ? TransformerVoteResult.YES : TransformerVoteResult.NO; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Set<Target> targets() { | ||
final Type listenerType = Type.getType("Lorg/spongepowered/api/event/Listener;"); | ||
|
||
final Set<Type> listenerClasses = new HashSet<>(); | ||
for (ModFileInfo fileInfo : LoadingModList.get().getModFiles()) { | ||
for (ModFileScanData.AnnotationData annotation : fileInfo.getFile().getScanResult().getAnnotations()) { | ||
if (listenerType.equals(annotation.annotationType())) { | ||
listenerClasses.add(annotation.clazz()); | ||
} | ||
} | ||
} | ||
|
||
final Set<Target> targets = new HashSet<>(); | ||
for (Type listener : listenerClasses) { | ||
targets.add(Target.targetClass(listener.getInternalName())); | ||
} | ||
return targets; | ||
} | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
...src/applaunch/resources/META-INF/services/cpw.mods.modlauncher.api.ITransformationService
This file contains 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 +1 @@ | ||
org.spongepowered.forge.applaunch.service.SpongeForgeTransformationService | ||
org.spongepowered.forge.applaunch.transformation.SpongeForgeTransformationService |
This file contains 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
43 changes: 43 additions & 0 deletions
43
forge/src/launch/java/org/spongepowered/forge/launch/event/ListenerLookups.java
This file contains 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,43 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.forge.launch.event; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class ListenerLookups { | ||
private static final Map<Class<?>, MethodHandles.Lookup> lookups = new ConcurrentHashMap<>(); | ||
|
||
public static MethodHandles.@Nullable Lookup get(final Class<?> listenerClass) { | ||
return lookups.get(listenerClass); | ||
} | ||
|
||
public static void set(final Class<?> listenerClass, final MethodHandles.Lookup lookup) { | ||
ListenerLookups.lookups.put(listenerClass, lookup); | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.