+ * 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.connect;
+
+import org.spongepowered.asm.mixin.Mixins;
+
+/**
+ * A connector class is a bootstrap agent for a mixin consumer. It should call
+ * back against {@link Mixins} to register its resources much like previous
+ * "CoreMod" bootstraps.
+ */
+public interface IMixinConnector {
+
+ /**
+ * Connect to Mixin
+ */
+ public abstract void connect();
+
+}
diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java
index 88e3295b..fba779c0 100644
--- a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java
+++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java
@@ -27,7 +27,7 @@ package org.spongepowered.asm.mixin.extensibility;
import java.util.List;
import java.util.Set;
-import org.spongepowered.asm.lib.tree.ClassNode;
+import org.objectweb.asm.tree.ClassNode;
/**
* A companion plugin for a mixin configuration object. Objects implementing
@@ -121,4 +121,5 @@ public interface IMixinConfigPlugin {
* @param mixinInfo Information about this mixin
*/
public abstract void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo);
+
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java
index a5612820..4e946ccf 100644
--- a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java
+++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java
@@ -26,7 +26,7 @@ package org.spongepowered.asm.mixin.extensibility;
import java.util.List;
-import org.spongepowered.asm.lib.tree.ClassNode;
+import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
/**
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/Accessor.java b/src/main/java/org/spongepowered/asm/mixin/gen/Accessor.java
index 6fa3ee2a..3dfebc08 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/Accessor.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/Accessor.java
@@ -36,6 +36,10 @@ import org.spongepowered.asm.mixin.Shadow;
* return the field type and take no arguments) or a setter (must take an
* argument of the field type and return void). The annotated method must be
* abstract.
+ *
+ *
The target field for an accessor can be specified as part of the decorated
+ * method name (eg. setFoo or getFoo to provide accessors for
+ * a target field foo) or specified in the annotation value.
*
* Accessors provide a simple way of gaining access to internal class members
* in a target class without needing to resort to access transformers, and
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGenerator.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGenerator.java
index fd78c074..6ee65bbd 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGenerator.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGenerator.java
@@ -26,9 +26,12 @@ package org.spongepowered.asm.mixin.gen;
import java.util.ArrayList;
-import org.spongepowered.asm.lib.Opcodes;
-import org.spongepowered.asm.lib.tree.AnnotationNode;
-import org.spongepowered.asm.lib.tree.MethodNode;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.AnnotationNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
+import org.spongepowered.asm.mixin.refmap.IMixinContext;
+import org.spongepowered.asm.util.Bytecode;
/**
* Base class for accessor generators
@@ -39,9 +42,23 @@ public abstract class AccessorGenerator {
* Accessor info which describes the accessor
*/
protected final AccessorInfo info;
-
- public AccessorGenerator(AccessorInfo info) {
+
+ /**
+ * True for static field, false for instance field
+ */
+ protected final boolean targetIsStatic;
+
+ public AccessorGenerator(AccessorInfo info, boolean isStatic) {
this.info = info;
+ this.targetIsStatic = isStatic;
+ }
+
+ protected void checkModifiers() {
+ if (this.info.isStatic() && !this.targetIsStatic) {
+ IMixinContext context = this.info.getContext();
+ throw new InvalidInjectionException(context, String.format("%s is invalid. Accessor method is%s static but the target is not.",
+ this.info, this.info.isStatic() ? "" : " not"));
+ }
}
/**
@@ -53,8 +70,8 @@ public abstract class AccessorGenerator {
*/
protected final MethodNode createMethod(int maxLocals, int maxStack) {
MethodNode method = this.info.getMethod();
- MethodNode accessor = new MethodNode(Opcodes.ASM5, (method.access & ~Opcodes.ACC_ABSTRACT) | Opcodes.ACC_SYNTHETIC, method.name, method.desc,
- null, null);
+ MethodNode accessor = new MethodNode(Bytecode.ASM_API_VERSION, (method.access & ~Opcodes.ACC_ABSTRACT) | Opcodes.ACC_SYNTHETIC, method.name,
+ method.desc, null, null);
accessor.visibleAnnotations = new ArrayList();
accessor.visibleAnnotations.add(this.info.getAnnotation());
accessor.maxLocals = maxLocals;
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorField.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorField.java
index 15c38a2a..41be7d26 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorField.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorField.java
@@ -24,9 +24,9 @@
*/
package org.spongepowered.asm.mixin.gen;
-import org.spongepowered.asm.lib.Opcodes;
-import org.spongepowered.asm.lib.Type;
-import org.spongepowered.asm.lib.tree.FieldNode;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.tree.FieldNode;
+import org.spongepowered.asm.util.Bytecode;
/**
* Accessor generator for field accessors
@@ -43,16 +43,11 @@ public abstract class AccessorGeneratorField extends AccessorGenerator {
*/
protected final Type targetType;
- /**
- * True for instance field, false for static field
- */
- protected final boolean isInstanceField;
-
public AccessorGeneratorField(AccessorInfo info) {
- super(info);
+ super(info, Bytecode.isStatic(info.getTargetField()));
this.targetField = info.getTargetField();
this.targetType = info.getTargetFieldType();
- this.isInstanceField = (this.targetField.access & Opcodes.ACC_STATIC) == 0;
+ this.checkModifiers();
}
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldGetter.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldGetter.java
index da48ae67..b34a592e 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldGetter.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldGetter.java
@@ -24,11 +24,11 @@
*/
package org.spongepowered.asm.mixin.gen;
-import org.spongepowered.asm.lib.Opcodes;
-import org.spongepowered.asm.lib.tree.FieldInsnNode;
-import org.spongepowered.asm.lib.tree.InsnNode;
-import org.spongepowered.asm.lib.tree.MethodNode;
-import org.spongepowered.asm.lib.tree.VarInsnNode;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.FieldInsnNode;
+import org.objectweb.asm.tree.InsnNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.objectweb.asm.tree.VarInsnNode;
/**
* Generator for instance field getters
@@ -45,10 +45,10 @@ public class AccessorGeneratorFieldGetter extends AccessorGeneratorField {
@Override
public MethodNode generate() {
MethodNode method = this.createMethod(this.targetType.getSize(), this.targetType.getSize());
- if (this.isInstanceField) {
+ if (!this.targetIsStatic) {
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
- int opcode = this.isInstanceField ? Opcodes.GETFIELD : Opcodes.GETSTATIC;
+ int opcode = this.targetIsStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD;
method.instructions.add(new FieldInsnNode(opcode, this.info.getClassNode().name, this.targetField.name, this.targetField.desc));
method.instructions.add(new InsnNode(this.targetType.getOpcode(Opcodes.IRETURN)));
return method;
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldSetter.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldSetter.java
index 012457f8..9ba3cce5 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldSetter.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorFieldSetter.java
@@ -24,11 +24,11 @@
*/
package org.spongepowered.asm.mixin.gen;
-import org.spongepowered.asm.lib.Opcodes;
-import org.spongepowered.asm.lib.tree.FieldInsnNode;
-import org.spongepowered.asm.lib.tree.InsnNode;
-import org.spongepowered.asm.lib.tree.MethodNode;
-import org.spongepowered.asm.lib.tree.VarInsnNode;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.FieldInsnNode;
+import org.objectweb.asm.tree.InsnNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.objectweb.asm.tree.VarInsnNode;
/**
* Generator for field setters
@@ -44,15 +44,15 @@ public class AccessorGeneratorFieldSetter extends AccessorGeneratorField {
*/
@Override
public MethodNode generate() {
- int stackSpace = this.isInstanceField ? 1 : 0;
+ int stackSpace = this.targetIsStatic ? 0 : 1; // Stack space for "this"
int maxLocals = stackSpace + this.targetType.getSize();
int maxStack = stackSpace + this.targetType.getSize();
MethodNode method = this.createMethod(maxLocals, maxStack);
- if (this.isInstanceField) {
+ if (!this.targetIsStatic) {
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
method.instructions.add(new VarInsnNode(this.targetType.getOpcode(Opcodes.ILOAD), stackSpace));
- int opcode = this.isInstanceField ? Opcodes.PUTFIELD : Opcodes.PUTSTATIC;
+ int opcode = this.targetIsStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD;
method.instructions.add(new FieldInsnNode(opcode, this.info.getClassNode().name, this.targetField.name, this.targetField.desc));
method.instructions.add(new InsnNode(Opcodes.RETURN));
return method;
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorMethodProxy.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorMethodProxy.java
index 6a6927e1..9617f29b 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorMethodProxy.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorMethodProxy.java
@@ -24,12 +24,12 @@
*/
package org.spongepowered.asm.mixin.gen;
-import org.spongepowered.asm.lib.Opcodes;
-import org.spongepowered.asm.lib.Type;
-import org.spongepowered.asm.lib.tree.InsnNode;
-import org.spongepowered.asm.lib.tree.MethodInsnNode;
-import org.spongepowered.asm.lib.tree.MethodNode;
-import org.spongepowered.asm.lib.tree.VarInsnNode;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.tree.InsnNode;
+import org.objectweb.asm.tree.MethodInsnNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.objectweb.asm.tree.VarInsnNode;
import org.spongepowered.asm.util.Bytecode;
/**
@@ -40,41 +40,43 @@ public class AccessorGeneratorMethodProxy extends AccessorGenerator {
/**
* The target field, identified by the accessor info
*/
- private final MethodNode targetMethod;
+ protected final MethodNode targetMethod;
/**
* Accessor method argument types (raw, from method)
*/
- private final Type[] argTypes;
+ protected final Type[] argTypes;
/**
* Accessor method return type (raw, from method)
*/
- private final Type returnType;
-
- /**
- * True for instance method, false for static method
- */
- private final boolean isInstanceMethod;
+ protected final Type returnType;
public AccessorGeneratorMethodProxy(AccessorInfo info) {
- super(info);
+ super(info, Bytecode.isStatic(info.getTargetMethod()));
+ this.targetMethod = info.getTargetMethod();
+ this.argTypes = info.getArgTypes();
+ this.returnType = info.getReturnType();
+ this.checkModifiers();
+ }
+
+ protected AccessorGeneratorMethodProxy(AccessorInfo info, boolean isStatic) {
+ super(info, isStatic);
this.targetMethod = info.getTargetMethod();
this.argTypes = info.getArgTypes();
this.returnType = info.getReturnType();
- this.isInstanceMethod = !Bytecode.hasFlag(this.targetMethod, Opcodes.ACC_STATIC);
}
@Override
public MethodNode generate() {
- int size = Bytecode.getArgsSize(this.argTypes) + this.returnType.getSize() + (this.isInstanceMethod ? 1 : 0);
+ int size = Bytecode.getArgsSize(this.argTypes) + this.returnType.getSize() + (this.targetIsStatic ? 0 : 1);
MethodNode method = this.createMethod(size, size);
- if (this.isInstanceMethod) {
+ if (!this.targetIsStatic) {
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
- Bytecode.loadArgs(this.argTypes, method.instructions, this.isInstanceMethod ? 1 : 0);
+ Bytecode.loadArgs(this.argTypes, method.instructions, this.targetIsStatic ? 0 : 1);
boolean isPrivate = Bytecode.hasFlag(this.targetMethod, Opcodes.ACC_PRIVATE);
- int opcode = this.isInstanceMethod ? (isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL) : Opcodes.INVOKESTATIC;
+ int opcode = this.targetIsStatic ? (isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL) : Opcodes.INVOKESTATIC;
method.instructions.add(new MethodInsnNode(opcode, this.info.getClassNode().name, this.targetMethod.name, this.targetMethod.desc, false));
method.instructions.add(new InsnNode(this.returnType.getOpcode(Opcodes.IRETURN)));
return method;
diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorObjectFactory.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorObjectFactory.java
new file mode 100644
index 00000000..1c928134
--- /dev/null
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorObjectFactory.java
@@ -0,0 +1,67 @@
+/*
+ * 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.gen;
+
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.InsnNode;
+import org.objectweb.asm.tree.MethodInsnNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.objectweb.asm.tree.TypeInsnNode;
+import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
+import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.Constants;
+
+/**
+ * Generator for factory methods (constructor invokers)
+ */
+public class AccessorGeneratorObjectFactory extends AccessorGeneratorMethodProxy {
+
+ public AccessorGeneratorObjectFactory(AccessorInfo info) {
+ super(info, true);
+ if (!info.isStatic()) {
+ throw new InvalidInjectionException(info.getContext(), String.format("%s is invalid. Factory method must be static.", this.info));
+ }
+ }
+
+ @Override
+ public MethodNode generate() {
+ int returnSize = this.returnType.getSize();
+ // size includes return size x2 since we immediately DUP the value to invoke the ctor on it
+ int size = Bytecode.getArgsSize(this.argTypes) + (returnSize * 2);
+ MethodNode method = this.createMethod(size, size);
+
+ String className = this.info.getClassNode().name;
+ method.instructions.add(new TypeInsnNode(Opcodes.NEW, className));
+ method.instructions.add(new InsnNode(returnSize == 1 ? Opcodes.DUP : Opcodes.DUP2));
+ Bytecode.loadArgs(this.argTypes, method.instructions, 0);
+ method.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, className, Constants.CTOR, this.targetMethod.desc, false));
+ method.instructions.add(new InsnNode(Opcodes.ARETURN));
+
+ assert 1 == 3;
+
+ return method;
+ }
+
+}
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 5261c32b..f15aa07d 100644
--- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java
+++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java
@@ -26,24 +26,31 @@ package org.spongepowered.asm.mixin.gen;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
-import org.spongepowered.asm.lib.Type;
-import org.spongepowered.asm.lib.tree.FieldNode;
-import org.spongepowered.asm.lib.tree.MethodNode;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.tree.FieldNode;
+import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException;
+import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
+import org.spongepowered.asm.mixin.injection.selectors.TargetSelector;
+import org.spongepowered.asm.mixin.injection.selectors.TargetSelector.Result;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.mixin.struct.SpecialMethodInfo;
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
-import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Annotations;
+import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.asm.ElementNode;
+import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
@@ -86,6 +93,16 @@ public class AccessorInfo extends SpecialMethodInfo {
AccessorGenerator getGenerator(AccessorInfo info) {
return new AccessorGeneratorMethodProxy(info);
}
+ },
+
+ /**
+ * An invoker (proxy) method
+ */
+ OBJECT_FACTORY(ImmutableSet.of("new", "create")) {
+ @Override
+ AccessorGenerator getGenerator(AccessorInfo info) {
+ return new AccessorGeneratorObjectFactory(info);
+ }
};
private final Set expectedPrefixes;
@@ -112,17 +129,111 @@ public class AccessorInfo extends SpecialMethodInfo {
* @return string representation of expected prefixes for this accessor
* type
*/
- public String getExpectedPrefixes() {
- return this.expectedPrefixes.toString();
+ public Set getExpectedPrefixes() {
+ return Collections.unmodifiableSet(this.expectedPrefixes);
}
abstract AccessorGenerator getGenerator(AccessorInfo info);
+
}
/**
- * Pattern for matching accessor names (for inflector)
+ * Accessor Name struct
*/
- protected static final Pattern PATTERN_ACCESSOR = Pattern.compile("^(get|set|is|invoke|call)(([A-Z])(.*?))(_\\$md.*)?$");
+ public static final class AccessorName {
+
+ /**
+ * Pattern for matching accessor names (for inflector)
+ */
+ private static final Pattern PATTERN = Pattern.compile("^(" + AccessorName.getPrefixList() + ")(([A-Z])(.*?))(_\\$md.*)?$");
+
+ /**
+ * Name of the accessor method
+ */
+ public final String methodName;
+
+ /**
+ * Accessor prefix
+ */
+ public final String prefix;
+
+ /**
+ * Accessor name part
+ */
+ public final String name;
+
+ private AccessorName(String methodName, String prefix, String name) {
+ this.methodName = methodName;
+ this.prefix = prefix;
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString();
+ }
+
+ /**
+ * Get an accessor name from the supplied string. If the string matches
+ * the accessor name regex, split the string into the relevant parts
+ *
+ * @param methodName Name of the accessor method
+ * @return Parsed AccessorName struct or null if the name is not a valid
+ * accessor name
+ */
+ public static AccessorName of(String methodName) {
+ return AccessorName.of(methodName, true);
+ }
+
+ /**
+ * Get an accessor name from the supplied string. If the string matches
+ * the accessor name regex, split the string into the relevant parts
+ *
+ * @param methodName Name of the accessor method
+ * @param toMemberCase True if the first character of the name should be
+ * conditionally converted to lowercase. If the name is all
+ * uppercase (eg. if the NAME_IS_A_CONSTANT) the first character
+ * will not be lowercased, regardless of the state of this argument
+ * @return Parsed AccessorName struct or null if the name is not a valid
+ * accessor name
+ */
+ public static AccessorName of(String methodName, boolean toMemberCase) {
+ Matcher nameMatcher = AccessorName.PATTERN.matcher(methodName);
+ if (nameMatcher.matches()) {
+ String prefix = nameMatcher.group(1);
+ String namePart = nameMatcher.group(2);
+ String firstChar = nameMatcher.group(3);
+ String remainder = nameMatcher.group(4);
+ boolean nameIsUpperCase = AccessorName.isUpperCase(Locale.ROOT, namePart);
+ // If the entire name is upper case, do not lowercase the first char
+ String name = String.format("%s%s", AccessorName.toLowerCaseIf(Locale.ROOT, firstChar, toMemberCase && !nameIsUpperCase), remainder);
+ return new AccessorName(methodName, prefix, name);
+ }
+ return null;
+ }
+
+ private static boolean isUpperCase(Locale locale, String string) {
+ return string.toUpperCase(locale).equals(string);
+ }
+
+ private static String toLowerCaseIf(Locale locale, String string, boolean condition) {
+ return condition ? string.toLowerCase(locale) : string;
+ }
+
+ private static String getPrefixList() {
+ List prefixes = new ArrayList();
+ for (AccessorType type : AccessorType.values()) {
+ prefixes.addAll(type.getExpectedPrefixes());
+ }
+ return Joiner.on('|').join(prefixes);
+ }
+
+ }
+
+ /**
+ * Annotation class
+ */
+ protected final Class extends Annotation> annotationClass;
/**
* Accessor method argument types (raw, from method)
@@ -134,6 +245,16 @@ public class AccessorInfo extends SpecialMethodInfo {
*/
protected final Type returnType;
+ /**
+ * Accessor method staticness
+ */
+ protected final boolean isStatic;
+
+ /**
+ * Name specified in the attached annotation, can be null
+ */
+ protected final String specifiedName;
+
/**
* Type of accessor to generate, computed based on the signature of the
* target method.
@@ -149,7 +270,7 @@ public class AccessorInfo extends SpecialMethodInfo {
* Computed information about the target field or method, name and
* descriptor
*/
- protected final MemberInfo target;
+ protected final ITargetSelector target;
/**
* For accessors, stores the discovered target field
@@ -167,8 +288,11 @@ public class AccessorInfo extends SpecialMethodInfo {
protected AccessorInfo(MixinTargetContext mixin, MethodNode method, Class extends Annotation> annotationClass) {
super(mixin, method, Annotations.getVisible(method, annotationClass));
+ this.annotationClass = annotationClass;
this.argTypes = Type.getArgumentTypes(method.desc);
this.returnType = Type.getReturnType(method.desc);
+ this.isStatic = Bytecode.isStatic(method);
+ this.specifiedName = Annotations.getValue(this.annotation);
this.type = this.initType();
this.targetFieldType = this.initTargetFieldType();
this.target = this.initTarget();
@@ -200,22 +324,22 @@ public class AccessorInfo extends SpecialMethodInfo {
}
}
- protected MemberInfo initTarget() {
- MemberInfo target = new MemberInfo(this.getTargetName(), null, this.targetFieldType.getDescriptor());
+ protected ITargetSelector initTarget() {
+ MemberInfo target = new MemberInfo(this.getTargetName(this.specifiedName), null, this.targetFieldType.getDescriptor());
this.annotation.visit("target", target.toString());
return target;
}
- protected String getTargetName() {
- String name = Annotations.getValue(this.annotation);
+ protected String getTargetName(String name) {
if (Strings.isNullOrEmpty(name)) {
String inflectedTarget = this.inflectTarget();
if (inflectedTarget == null) {
- throw new InvalidAccessorException(this.mixin, "Failed to inflect target name for " + this + ", supported prefixes: [get, set, is]");
+ throw new InvalidAccessorException(this.mixin, String.format("Failed to inflect target name for %s, supported prefixes: %s",
+ this, this.type.getExpectedPrefixes()));
}
return inflectedTarget;
}
- return MemberInfo.parse(name, this.mixin).name;
+ return TargetSelector.parseName(name, this.mixin);
}
/**
@@ -235,31 +359,45 @@ public class AccessorInfo extends SpecialMethodInfo {
* named getFoo to be inflected to a target named foo for
* example.
*
- * @param accessorName Name of the accessor method
- * @param accessorType Type of accessor being processed, this is calculated
+ * @param name Name of the accessor method
+ * @param type Type of accessor being processed, this is calculated
* from the method signature (void methods being setters,
* methods with return types being getters)
- * @param accessorDescription description of the accessor to include in
+ * @param description description of the accessor to include in
* error messages
* @param context Mixin context
* @param verbose Emit warnings when accessor prefix doesn't match type
* @return inflected target member name or null if name cannot be
* inflected
*/
- public static String inflectTarget(String accessorName, AccessorType accessorType, String accessorDescription,
- IMixinContext context, boolean verbose) {
- Matcher nameMatcher = AccessorInfo.PATTERN_ACCESSOR.matcher(accessorName);
- if (nameMatcher.matches()) {
- String prefix = nameMatcher.group(1);
- String firstChar = nameMatcher.group(3);
- String remainder = nameMatcher.group(4);
- // If the entire name is upper case, do not lowercase the first char
- String name = String.format("%s%s", AccessorInfo.toLowerCase(firstChar, !AccessorInfo.isUpperCase(remainder)), remainder);
- if (!accessorType.isExpectedPrefix(prefix) && verbose) {
- LogManager.getLogger("mixin").warn("Unexpected prefix for {}, found [{}] expecting {}", accessorDescription, prefix,
- accessorType.getExpectedPrefixes());
+ public static String inflectTarget(String name, AccessorType type, String description, IMixinContext context, boolean verbose) {
+ return AccessorInfo.inflectTarget(AccessorName.of(name), type, description, context, verbose);
+ }
+
+ /**
+ * Uses the name of an accessor method and the accessor type to try and
+ * inflect the name of the target field or method. This allows a method
+ * named getFoo to be inflected to a target named foo for
+ * example.
+ *
+ * @param name Name of the accessor method
+ * @param type Type of accessor being processed, this is calculated
+ * from the method signature (void methods being setters,
+ * methods with return types being getters)
+ * @param description description of the accessor to include in
+ * error messages
+ * @param context Mixin context
+ * @param verbose Emit warnings when accessor prefix doesn't match type
+ * @return inflected target member name or null if name cannot be
+ * inflected
+ */
+ public static String inflectTarget(AccessorName name, AccessorType type, String description, IMixinContext context, boolean verbose) {
+ if (name != null) {
+ if (!type.isExpectedPrefix(name.prefix) && verbose) {
+ LogManager.getLogger("mixin").warn("Unexpected prefix for {}, found [{}] expecting {}", description, name.prefix,
+ type.getExpectedPrefixes());
}
- return MemberInfo.parse(name, context).name;
+ return TargetSelector.parseName(name.name, context);
}
return null;
}
@@ -267,7 +405,7 @@ public class AccessorInfo extends SpecialMethodInfo {
/**
* Get the inflected/specified target member for this accessor
*/
- public final MemberInfo getTarget() {
+ public final ITargetSelector getTarget() {
return this.target;
}
@@ -305,10 +443,18 @@ public class AccessorInfo extends SpecialMethodInfo {
public final Type[] getArgTypes() {
return this.argTypes;
}
+
+ /**
+ * Get whether the accessor itself is static
+ */
+ public boolean isStatic() {
+ return this.isStatic;
+ }
@Override
public String toString() {
- return String.format("%s->@%s[%s]::%s%s", this.mixin.toString(), Bytecode.getSimpleName(this.annotation), this.type.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);
}
@@ -330,12 +476,12 @@ public class AccessorInfo extends SpecialMethodInfo {
*/
public MethodNode generate() {
MethodNode generatedAccessor = this.type.getGenerator(this).generate();
- Bytecode.mergeAnnotations(this.method, generatedAccessor);
+ Annotations.merge(this.method, generatedAccessor);
return generatedAccessor;
}
private FieldNode findTargetField() {
- return this.findTarget(this.classNode.fields);
+ return this.findTarget(ElementNode.fieldList(this.classNode));
}
/**
@@ -346,49 +492,14 @@ public class AccessorInfo extends SpecialMethodInfo {
* @param node type
* @return best match
*/
- protected TNode findTarget(List nodes) {
- TNode exactMatch = null;
- List