Refactoring so that service impls don't need to camp on transformer pkg

This commit is contained in:
Mumfrey committed 2021-02-24 22:18:08 +00:00
1 parent 57055a150e
commit e9f7d752f2
13 files changed
+328 -81

No files matched your search

@@ -24,6 +24,8 @@
*/
package org.spongepowered.asm.launch;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
@@ -32,6 +34,9 @@ import org.spongepowered.asm.launch.platform.CommandLineOptions;
import org.spongepowered.asm.launch.platform.MixinPlatformManager;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.throwables.MixinError;
import org.spongepowered.asm.service.IMixinInternal;
import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;
/**
@@ -63,11 +68,17 @@ public abstract class MixinBootstrap {
*/
public static final String VERSION = "0.8.3";
/**
* Transformer factory
*/
private static final String MIXIN_TRANSFORMER_FACTORY_CLASS = "org.spongepowered.asm.mixin.transformer.MixinTransformer$Factory";
/**
* Log all the things
*/
private static final Logger logger = LogManager.getLogger("mixin");
// These are Klass local, with luck this shouldn't be a problem
private static boolean initialised = false;
private static boolean initState = true;
@@ -77,7 +88,7 @@ public abstract class MixinBootstrap {
MixinService.boot();
MixinService.getService().prepare();
}
/**
* Platform manager instance
*/
@@ -134,6 +145,7 @@ public abstract class MixinBootstrap {
}
MixinBootstrap.registerSubsystem(MixinBootstrap.VERSION);
MixinBootstrap.offerInternals();
if (!MixinBootstrap.initialised) {
MixinBootstrap.initialised = true;
@@ -207,4 +219,31 @@ public abstract class MixinBootstrap {
GlobalProperties.put(GlobalProperties.Keys.INIT, version);
}
private static void offerInternals() {
IMixinService service = MixinService.getService();
try {
for (IMixinInternal internal : MixinBootstrap.getInternals()) {
service.offer(internal);
}
} catch (AbstractMethodError ex) {
// outdated service
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private static List<IMixinInternal> getInternals() throws MixinError {
List<IMixinInternal> internals = new ArrayList<IMixinInternal>();
try {
Class<IMixinInternal> clTransformerFactory = (Class<IMixinInternal>)Class.forName(MixinBootstrap.MIXIN_TRANSFORMER_FACTORY_CLASS);
Constructor<IMixinInternal> ctor = clTransformerFactory.getDeclaredConstructor();
ctor.setAccessible(true);
internals.add(ctor.newInstance());
} catch (ReflectiveOperationException ex) {
throw new MixinError(ex);
}
return internals;
}
}
@@ -52,6 +52,20 @@ public interface IMixinTransformer {
*/
public abstract List<String> reload(String mixinClass, ClassNode classNode);
/**
* Called when the transformation reason is computing_frames. The only
* operation we care about here is adding interfaces to target classes but
* at the moment we don't have sufficient scaffolding to determine that
* without triggering re-entrance. Currently just a no-op in order to not
* cause a re-entrance crash under ModLauncher 7.0+.
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
public abstract boolean computeFramesForClass(MixinEnvironment environment, String name, ClassNode classNode);
/**
* Callback from the hotswap agent and LaunchWrapper Proxy, transform class
* bytecode. This method delegates to class generation or class
@@ -67,6 +81,43 @@ public interface IMixinTransformer {
* @see ILegacyClassTransformer#transformClassBytes(String, String, byte[])
*/
public abstract byte[] transformClassBytes(String name, String transformedName, byte[] basicClass);
/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classBytes Class bytecode
* @return Transformed bytecode
*/
public abstract byte[] transformClass(MixinEnvironment environment, String name, byte[] classBytes);
/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
public abstract boolean transformClass(MixinEnvironment environment, String name, ClassNode classNode);
/**
* Generate the specified mixin-synthetic class
*
* @param environment Current environment
* @param name Class name to generate
* @return Generated bytecode or <tt>null</tt> if no class was generated
*/
public abstract byte[] generateClass(MixinEnvironment environment, String name);
/**
* @param environment Current environment
* @param name Class transformed name
* @param classNode Empty classnode to populate
* @return True if the class was generated successfully
*/
public abstract boolean generateClass(MixinEnvironment environment, String name, ClassNode classNode);
/**
* Get the transformer extensions
@@ -0,0 +1,42 @@
/*
* This file is part of Mixin, 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.asm.mixin.transformer;
import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.service.IMixinInternal;
/**
* Factory for the mixin transformer, concrete instances of this class are only
* provided to services since only the service should be able to decide when the
* mixin transformer is initialised.
*/
public interface IMixinTransformerFactory extends IMixinInternal {
/**
* Create a new transformer.
*/
public abstract IMixinTransformer createTransformer() throws MixinInitialisationError;
}
@@ -28,6 +28,7 @@ import java.lang.reflect.Constructor;
import java.util.List;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.throwables.MixinException;
@@ -41,10 +42,26 @@ import org.spongepowered.asm.util.asm.ASM;
/**
* Transformer which manages the mixin configuration and application process
*/
class MixinTransformer extends TreeTransformer implements IMixinTransformer {
final class MixinTransformer extends TreeTransformer implements IMixinTransformer {
/**
* Impl of mixin transformer factory
*/
static class Factory implements IMixinTransformerFactory {
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerFactory
* #createTransformer()
*/
@Override
public IMixinTransformer createTransformer() throws MixinInitialisationError {
return new MixinTransformer();
}
}
private static final String MIXIN_AGENT_CLASS = "org.spongepowered.tools.agent.MixinAgent";
/**
* Synthetic class registry
*/
@@ -70,7 +87,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
*/
private final MixinClassGenerator generator;
public MixinTransformer() {
MixinTransformer() {
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
Object globalMixinTransformer = environment.getActiveTransformer();
@@ -190,6 +207,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* @param classNode Class tree
* @return true if the class was transformed
*/
@Override
public boolean computeFramesForClass(MixinEnvironment environment, String name, ClassNode classNode) {
// TODO compute added interfaces
return false;
@@ -203,6 +221,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* @param classBytes Class bytecode
* @return Transformed bytecode
*/
@Override
public byte[] transformClass(MixinEnvironment environment, String name, byte[] classBytes) {
ClassNode classNode = this.readClass(classBytes);
if (this.processor.applyMixins(environment, name, classNode)) {
@@ -219,6 +238,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* @param classNode Class tree
* @return true if the class was transformed
*/
@Override
public boolean transformClass(MixinEnvironment environment, String name, ClassNode classNode) {
return this.processor.applyMixins(environment, name, classNode);
}
@@ -230,6 +250,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* @param name Class name to generate
* @return Generated bytecode or <tt>null</tt> if no class was generated
*/
@Override
public byte[] generateClass(MixinEnvironment environment, String name) {
ClassNode classNode = MixinTransformer.createEmptyClass(name);
if (this.generator.generateClass(environment, name, classNode)) {
@@ -244,6 +265,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* @param classNode Empty classnode to populate
* @return True if the class was generated successfully
*/
@Override
public boolean generateClass(MixinEnvironment environment, String name, ClassNode classNode) {
return this.generator.generateClass(environment, name, classNode);
}
@@ -0,0 +1,32 @@
/*
* This file is part of Mixin, 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.asm.service;
/**
* A mixin internal part, offered to services.
*/
public interface IMixinInternal {
}
@@ -62,6 +62,15 @@ public interface IMixinService {
*/
public abstract Phase getInitialPhase();
/**
* Called when the subsystem is offering internal components to the service,
* the service can determine whether to retain or ignore the component based
* on its own requirements.
*
* @param internal Internal component being offered
*/
public abstract void offer(IMixinInternal internal);
/**
* Called at the end of subsystem boot
*/
@@ -26,7 +26,9 @@ package org.spongepowered.asm.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -50,6 +52,7 @@ public abstract class MixinServiceAbstract implements IMixinService {
// Consts
protected static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
protected static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin.";
protected static final String SERVICE_PACKAGE = "org.spongepowered.asm.service.";
/**
* Logger
@@ -62,6 +65,11 @@ public abstract class MixinServiceAbstract implements IMixinService {
*/
protected final ReEntranceLock lock = new ReEntranceLock(1);
/**
* All internals offered to this service
*/
private final Map<Class<IMixinInternal>, IMixinInternal> internals = new HashMap<Class<IMixinInternal>, IMixinInternal>();
/**
* Service agent instances
*/
@@ -104,6 +112,46 @@ public abstract class MixinServiceAbstract implements IMixinService {
public CompatibilityLevel getMaxCompatibilityLevel() {
return null;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.IMixinService
* #offer(org.spongepowered.asm.service.IMixinInternal)
*/
@Override
public void offer(IMixinInternal internal) {
this.registerInternal(internal, internal.getClass());
}
@SuppressWarnings("unchecked")
private void registerInternal(IMixinInternal internal, Class<?> clazz) {
for (Class<?> iface : clazz.getInterfaces()) {
if (iface == IMixinInternal.class) {
this.internals.put((Class<IMixinInternal>)clazz, internal);
}
this.registerInternal(internal, iface);
}
}
@SuppressWarnings("unchecked")
protected final <T extends IMixinInternal> T getInternal(Class<T> type) {
for (Class<IMixinInternal> internalType : this.internals.keySet()) {
if (type.isAssignableFrom(internalType)) {
return (T)this.internals.get(internalType);
}
}
return null;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.IMixinService#init()
*/
@Override
public void init() {
for (IMixinPlatformServiceAgent agent : this.getServiceAgents()) {
agent.init();
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.IMixinService#beginPhase()
@@ -120,16 +168,6 @@ public abstract class MixinServiceAbstract implements IMixinService {
public void checkEnv(Object bootSource) {
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.IMixinService#init()
*/
@Override
public void init() {
for (IMixinPlatformServiceAgent agent : this.getServiceAgents()) {
agent.init();
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.IMixinService#getReEntranceLock()
*/
@@ -454,7 +454,8 @@ public final class Locals {
String desc = lastKnownType[j];
Type localType = local.getType();
if (localType != null) {
desc = "null".equals(localType.getInternalName()) ? Constants.OBJECT_DESC : localType.getDescriptor();
desc = localType.getSort() >= Type.ARRAY && "null".equals(localType.getInternalName())
? Constants.OBJECT_DESC : localType.getDescriptor();
}
localNodes[j] = new LocalVariableNode("var" + j, desc, null, label, null, j);