From 6f65e9f04314c647d2c4b5b34132d60765cff4d0 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 15 Nov 2019 12:45:31 +0000 Subject: [PATCH] Add more detail to activity tracking and some better exception messages --- .../asm/mixin/gen/AccessorInfo.java | 2 +- .../asm/mixin/struct/SpecialMethodInfo.java | 2 +- .../ClassMetadataNotFoundException.java | 65 ++++++++ .../MixinPreProcessorStandard.java | 7 +- .../mixin/transformer/MixinTargetContext.java | 143 +++++++++++++----- .../asm/util/asm/MethodNodeEx.java | 4 + 6 files changed, 181 insertions(+), 42 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/throwables/ClassMetadataNotFoundException.java diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java index f15aa07d..d0f14737 100644 --- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java @@ -455,7 +455,7 @@ public class AccessorInfo extends SpecialMethodInfo { public String toString() { String typeString = this.type != null ? this.type.toString() : "UNPARSED_ACCESSOR"; return String.format("%s->@%s[%s]::%s%s", this.mixin, Bytecode.getSimpleName(this.annotation), typeString, - this.method.name, this.method.desc); + this.methodName, this.method.desc); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java index 409a7e3a..2a91ad0c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java @@ -74,7 +74,7 @@ public abstract class SpecialMethodInfo implements IInjectionPointContext { this.annotation = annotation; this.annotationType = this.annotation != null ? "@" + Bytecode.getSimpleName(this.annotation) : "Undecorated injector"; this.classNode = mixin.getTargetClassNode(); - this.methodName = method instanceof MethodNodeEx ? ((MethodNodeEx)this.method).getOriginalName() : method.name; + this.methodName = MethodNodeEx.getName(method); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/throwables/ClassMetadataNotFoundException.java b/src/main/java/org/spongepowered/asm/mixin/throwables/ClassMetadataNotFoundException.java new file mode 100644 index 00000000..c9eaf10a --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/throwables/ClassMetadataNotFoundException.java @@ -0,0 +1,65 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.throwables; + +import org.spongepowered.asm.mixin.transformer.ActivityStack; +import org.spongepowered.asm.mixin.transformer.ClassInfo; + +/** + * Since it's inconvenient to throw the (checked) {@link ClassNotFoundException} + * this exception can be selectively thrown when {@link ClassInfo#forName} + * returns null (indicating class couldn't be loaded or was not found) + * in situations where we actually care about it failing and want to throw + * something. + */ +public class ClassMetadataNotFoundException extends MixinException { + + private static final long serialVersionUID = 1L; + + public ClassMetadataNotFoundException(String message) { + super(message); + } + + public ClassMetadataNotFoundException(String message, ActivityStack context) { + super(message, context); + } + + public ClassMetadataNotFoundException(Throwable cause) { + super(cause); + } + + public ClassMetadataNotFoundException(Throwable cause, ActivityStack context) { + super(cause, context); + } + + public ClassMetadataNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ClassMetadataNotFoundException(String message, Throwable cause, ActivityStack context) { + super(message, cause, context); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java index d54a9617..957b3396 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java @@ -47,6 +47,7 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException; +import org.spongepowered.asm.mixin.throwables.ClassMetadataNotFoundException; import org.spongepowered.asm.mixin.throwables.MixinException; import org.spongepowered.asm.mixin.transformer.ActivityStack.Activity; import org.spongepowered.asm.mixin.transformer.ClassInfo.Field; @@ -582,7 +583,7 @@ class MixinPreProcessorStandard { Activity fieldActivity = this.activities.begin("?"); for (Iterator iter = this.classNode.getFields().iterator(); iter.hasNext();) { FieldNode mixinField = iter.next(); - fieldActivity.next(String.format("%s:%s", mixinField.name, mixinField.desc)); + fieldActivity.next("%s:%s", mixinField.name, mixinField.desc); AnnotationNode shadow = Annotations.getVisible(mixinField, Shadow.class); boolean isShadow = shadow != null; @@ -732,7 +733,7 @@ class MixinPreProcessorStandard { Section metaTimer = this.profiler.begin("meta"); ClassInfo owner = ClassInfo.forDescriptor(methodNode.owner, TypeLookup.DECLARED_TYPE); if (owner == null) { - throw new RuntimeException(new ClassNotFoundException(methodNode.owner.replace('/', '.'))); + throw new ClassMetadataNotFoundException(methodNode.owner.replace('/', '.')); } Method method = owner.findMethodInHierarchy(methodNode, SearchType.ALL_CLASSES, ClassInfo.INCLUDE_PRIVATE); @@ -749,7 +750,7 @@ class MixinPreProcessorStandard { Section metaTimer = this.profiler.begin("meta"); ClassInfo owner = ClassInfo.forDescriptor(fieldNode.owner, TypeLookup.DECLARED_TYPE); if (owner == null) { - throw new RuntimeException(new ClassNotFoundException(fieldNode.owner.replace('/', '.'))); + throw new ClassMetadataNotFoundException(fieldNode.owner.replace('/', '.')); } Field field = owner.findField(fieldNode, ClassInfo.INCLUDE_PRIVATE); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index f2e79c74..baf044df 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -60,6 +60,7 @@ import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.mixin.refmap.IReferenceMapper; import org.spongepowered.asm.mixin.struct.MemberRef; import org.spongepowered.asm.mixin.struct.SourceMap.File; +import org.spongepowered.asm.mixin.throwables.ClassMetadataNotFoundException; import org.spongepowered.asm.mixin.transformer.ActivityStack.Activity; import org.spongepowered.asm.mixin.transformer.ClassInfo.Field; import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; @@ -444,7 +445,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { this.validateMethod(method); activity.next("Transform Descriptor"); this.transformDescriptor(method); - activity.next("Transform LVTs"); + activity.next("Transform LVT"); this.transformLVT(method); // Offset line numbers per the stratum @@ -532,13 +533,16 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { return; } + Activity localVarActivity = this.activities.begin("?"); for (LocalVariableNode local : method.localVariables) { if (local == null || local.desc == null) { continue; } + localVarActivity.next("var=%s", local.name); local.desc = this.transformSingleDescriptor(Type.getType(local.desc)); } + localVarActivity.end(); } /** @@ -906,6 +910,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { } private String transformSingleDescriptor(String desc, boolean isObject) { + Activity descriptorActivity = this.activities.begin("desc=%s", desc); String type = desc; while (type.startsWith("[") || type.startsWith("L")) { if (type.startsWith("[")) { @@ -915,27 +920,30 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { type = type.substring(1, type.indexOf(";")); isObject = true; } - if (!isObject) { + descriptorActivity.end(); return desc; } - String innerClassName = this.innerClasses.get(type); if (innerClassName != null) { + descriptorActivity.end(); return desc.replace(type, innerClassName); } - if (this.innerClasses.inverse().containsKey(type)) { + descriptorActivity.end(); return desc; } - ClassInfo typeInfo = ClassInfo.forName(type); - + if (typeInfo == null) { + throw new ClassMetadataNotFoundException(type.replace('/', '.')); + } if (!typeInfo.isMixin() || typeInfo.isLoadable()) { + descriptorActivity.end(); return desc; } - - return desc.replace(type, this.findRealType(typeInfo).toString()); + String realDesc = desc.replace(type, this.findRealType(typeInfo).toString()); + descriptorActivity.end(); + return realDesc; } private String transformMethodDescriptor(String desc) { @@ -1165,16 +1173,26 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { * @param targetClass Target class */ void postApply(String transformedName, ClassNode targetClass) { + this.activities.clear(); + try { + Activity activity = this.activities.begin("Validating Injector Groups"); this.injectorGroups.validateAll(); + activity.next("Plugin Post-Application"); + this.mixin.postApply(transformedName, targetClass); + activity.end(); } catch (InjectionValidationException ex) { InjectorGroupInfo group = ex.getGroup(); throw new InjectionError( String.format("Critical injection failure: Callback group %s in %s failed injection check: %s", group, this.mixin, ex.getMessage())); + } catch (InvalidMixinException ex) { + ex.prepend(this.activities); + throw ex; + } catch (Exception ex) { + throw new InvalidMixinException(this, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst transforming the mixin class:", ex, + this.activities); } - - this.mixin.postApply(transformedName, targetClass); } /** @@ -1206,20 +1224,38 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { * injectors */ void prepareInjections() { - this.injectors.clear(); + this.activities.clear(); - for (MethodNode method : this.mergedMethods) { - InjectionInfo injectInfo = InjectionInfo.parse(this, method); - if (injectInfo == null) { - continue; + try { + this.injectors.clear(); + + Activity prepareActivity = this.activities.begin("?"); + for (MethodNode method : this.mergedMethods) { + prepareActivity.next("%s%s", method.name, method.desc); + Activity methodActivity = this.activities.begin("Parse"); + InjectionInfo injectInfo = InjectionInfo.parse(this, method); + if (injectInfo == null) { + continue; + } + + methodActivity.next("Validate"); + if (injectInfo.isValid()) { + methodActivity.next("Prepare"); + injectInfo.prepare(); + this.injectors.add(injectInfo); + } + + methodActivity.next("Undecorate"); + method.visibleAnnotations.remove(injectInfo.getAnnotation()); + methodActivity.end(); } - - if (injectInfo.isValid()) { - injectInfo.prepare(); - this.injectors.add(injectInfo); - } - - method.visibleAnnotations.remove(injectInfo.getAnnotation()); + prepareActivity.end(); + } catch (InvalidMixinException ex) { + ex.prepend(this.activities); + throw ex; + } catch (Exception ex) { + throw new InvalidMixinException(this, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst transforming the mixin class:", ex, + this.activities); } } @@ -1227,15 +1263,32 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { * Apply injectors discovered in the {@link #prepareInjections()} pass */ void applyInjections() { - for (InjectionInfo injectInfo : this.injectors) { - injectInfo.inject(); - } + this.activities.clear(); - for (InjectionInfo injectInfo : this.injectors) { - injectInfo.postInject(); + try { + Activity applyActivity = this.activities.begin("Inject"); + Activity injectActivity = this.activities.begin("?"); + for (InjectionInfo injectInfo : this.injectors) { + injectActivity.next(injectInfo.toString()); + injectInfo.inject(); + } + + applyActivity.next("PostInject"); + Activity postInjectActivity = this.activities.begin("?"); + for (InjectionInfo injectInfo : this.injectors) { + postInjectActivity.next(injectInfo.toString()); + injectInfo.postInject(); + } + + applyActivity.end(); + this.injectors.clear(); + } catch (InvalidMixinException ex) { + ex.prepend(this.activities); + throw ex; + } catch (Exception ex) { + throw new InvalidMixinException(this, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst transforming the mixin class:", ex, + this.activities); } - - this.injectors.clear(); } /** @@ -1243,16 +1296,32 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { * method bodies */ List generateAccessors() { - for (AccessorInfo accessor : this.accessors) { - accessor.locate(); - } - + this.activities.clear(); List methods = new ArrayList(); - for (AccessorInfo accessor : this.accessors) { - MethodNode generated = accessor.generate(); - this.getTarget().addMixinMethod(generated); - methods.add(generated); + try { + Activity accessorActivity = this.activities.begin("Locate"); + Activity locateActivity = this.activities.begin("?"); + for (AccessorInfo accessor : this.accessors) { + locateActivity.next(accessor.toString()); + accessor.locate(); + } + + accessorActivity.next("Generate"); + Activity generateActivity = this.activities.begin("?"); + for (AccessorInfo accessor : this.accessors) { + generateActivity.next(accessor.toString()); + MethodNode generated = accessor.generate(); + this.getTarget().addMixinMethod(generated); + methods.add(generated); + } + accessorActivity.end(); + } catch (InvalidMixinException ex) { + ex.prepend(this.activities); + throw ex; + } catch (Exception ex) { + throw new InvalidMixinException(this, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst transforming the mixin class:", ex, + this.activities); } return methods; diff --git a/src/main/java/org/spongepowered/asm/util/asm/MethodNodeEx.java b/src/main/java/org/spongepowered/asm/util/asm/MethodNodeEx.java index f8869159..78a247f0 100644 --- a/src/main/java/org/spongepowered/asm/util/asm/MethodNodeEx.java +++ b/src/main/java/org/spongepowered/asm/util/asm/MethodNodeEx.java @@ -59,4 +59,8 @@ public class MethodNodeEx extends MethodNode { return this.owner; } + public static String getName(MethodNode method) { + return method instanceof MethodNodeEx ? ((MethodNodeEx)method).getOriginalName() : method.name; + } + }