Skip to content

Commit

Permalink
Add ModifyConstant injector, version bump to 0.5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Apr 27, 2016
1 parent 107b58d commit f19f4de
Show file tree
Hide file tree
Showing 20 changed files with 778 additions and 75 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ packaging=jar
description=Mixin
url=https://www.spongepowered.org
organization=SpongePowered
buildVersion=0.5.4
buildVersion=0.5.5
buildType=SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.tools.MirrorUtils;
Expand Down Expand Up @@ -71,6 +72,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
this.processInjectors(roundEnv, ModifyArg.class);
this.processInjectors(roundEnv, Redirect.class);
this.processInjectors(roundEnv, ModifyVariable.class);
this.processInjectors(roundEnv, ModifyConstant.class);
this.postProcess(roundEnv);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.4";
public static final String VERSION = "0.5.5";

/**
* Blackboard key where the subsystem version will be stored to indicate
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/org/spongepowered/asm/mixin/injection/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.injection;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Annotation for specifying the injection point for an {@link ModifyConstant}
* injector. Leaving all values unset causes the injection point to match all
* constants with the same type as the {@link ModifyConstant} handler's return
* type.
*
* <p>To match a specific constant, specify the appropriate value for the
* appropriate argument. Specifying values of different types will cause an
* error to be raised by the injector.</p>
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Constant {

/**
* Causes this injector to match ACONST_NULL (null object) literals
*
* @return true to match <tt>null</tt>
*/
public boolean nullValue() default false;

/**
* Specify an integer constant to match, includes byte and short values
*
* @return integer value to match
*/
public int intValue() default 0;

/**
* Specify a float constant to match
*
* @return float value to match
*/
public float floatValue() default 0.0F;

/**
* Specify a long constant to match
*
* @return long value to match
*/
public long longValue() default 0L;

/**
* Specify a double constant to match
*
* @return double value to match
*/
public double doubleValue() default 0.0;

/**
* Specify a String constant to match
*
* @return string value to match
*/
public String stringValue() default "";

/**
* Specify a type literal to match
*
* @return type literal to match
*/
public Class<?> classValue() default Object.class;

/**
* Ordinal offset. Many InjectionPoints will return every opcode matching
* their criteria, specifying <em>ordinal</em> allows a particular opcode to
* be identified from the returned list. The default value of -1 does not
* alter the behaviour and returns all matching opcodes. Specifying a value
* of 0 or higher returns <em>only</em> the requested opcode (if one exists:
* for example specifying an ordinal of 4 when only 2 opcodes are matched by
* the InjectionPoint is not going to work particularly well!)
*
* @return ordinal value for supported InjectionPoint types
*/
public int ordinal() default -1;

/**
* @return true to enable verbose debug logging for this injection point
*/
public boolean log() default false;
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.injection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.spongepowered.asm.mixin.MixinEnvironment.Option;

/**
* Specifies that this mixin method should inject a constant modifier callback
* to itself in the target method(s) identified by {@link #method} at the
* specified LDC (or specialised const opcode) instruction in order to adjust
* the constant value returned.
*
* <p><tt>ModifyConstant</tt> callbacks should always take one argument of the
* appropriate constant type and return the same type. For example a
* <tt>ModifyConstant </tt> for a local of type {@link String} should have the
* signature:</p>
*
* <code>private String methodName(String variable) { ...</code>
*
* <p>The callback receives the original value of the constant, and should
* return the new value.</p>
*/

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ModifyConstant {

/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
*
* @return method to inject into
*/
public String method();

/**
* Discriminator for the constant to match (injection point), if not
* specified then all constants matching the annotated handler's return type
* are matched
*
* @return the constant discriminator
*/
public Constant constant() default @Constant;

/**
* By default, the annotation processor will attempt to locate an
* obfuscation mapping for all {@link ModifyConstant} methods since it is
* anticipated that in general the target of a {@link ModifyConstant}
* annotation will be an obfuscated method in the target class. However
* since it is possible to also apply mixins to non-obfuscated targets (or
* non- obfuscated methods in obfuscated targets, such as methods added by
* Forge) it may be necessary to suppress the compiler error which would
* otherwise be generated. Setting this value to <em>false</em> will cause
* the annotation processor to skip this annotation when attempting to build
* the obfuscation table for the mixin.
*
* @return True to instruct the annotation processor to search for
* obfuscation mappings for this annotation
*/
public boolean remap() default true;

/**
* In general, injectors are intended to "fail soft" in that a failure to
* locate the injection point in the target method is not considered an
* error condition. Another transformer may have changed the method
* structure or any number of reasons may cause an injection to fail. This
* also makes it possible to define several injections to achieve the same
* task given <em>expected</em> mutation of the target class and the
* injectors which fail are simply ignored.
*
* <p>However, this behaviour is not always desirable. For example, if your
* application depends on a particular injection succeeding you may wish to
* detect the injection failure as an error condition. This argument is thus
* provided to allow you to stipulate a <b>minimum</b> number of successful
* injections for this callback handler. If the number of injections
* specified is not achieved then an {@link InjectionError} is thrown at
* application time. Use this option with care.</p>
*
* @return Minimum required number of injected callbacks, default specified
* by the containing config
*/
public int require() default -1;

/**
* Like {@link #require()} but only enabled if the
* {@link Option#DEBUG_INJECTORS mixin.debug.countInjections} option is set
* to <tt>true</tt> and defaults to 1. Use this option during debugging to
* perform simple checking of your injectors. Causes the injector to throw
* a {@link InvalidInjectionException} if the expected number of injections
* is not realised.
*
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.util.Constants;

/**
* CallbackInfo instances are passed to callbacks in order to provide
Expand All @@ -35,9 +36,6 @@
*/
public class CallbackInfo implements Cancellable {

protected static final String STRING = "Ljava/lang/String;";
protected static final String OBJECT = "Ljava/lang/Object;";

/**
* Method name being injected into, this is useful if a single callback is
* injecting into multiple methods.
Expand Down Expand Up @@ -122,14 +120,14 @@ static String getConstructorDescriptor(Type returnType) {
}

if (returnType.getSort() == Type.OBJECT || returnType.getSort() == Type.ARRAY) {
return String.format("(%sZ%s)V", CallbackInfo.STRING, CallbackInfo.OBJECT);
return String.format("(%sZ%s)V", Constants.STRING, Constants.OBJECT);
}

return String.format("(%sZ%s)V", CallbackInfo.STRING, returnType.getDescriptor());
return String.format("(%sZ%s)V", Constants.STRING, returnType.getDescriptor());
}

static String getConstructorDescriptor() {
return String.format("(%sZ)V", CallbackInfo.STRING);
return String.format("(%sZ)V", Constants.STRING);
}

static String getIsCancelledMethodName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.asm.mixin.injection.callback;

import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.util.Constants;

/**
* For callbacks with a non-void return type, a CallbackInfoReturnable is passed
Expand Down Expand Up @@ -133,7 +134,7 @@ static String getReturnAccessor(Type returnType) {

static String getReturnDescriptor(Type returnType) {
if (returnType.getSort() == Type.OBJECT || returnType.getSort() == Type.ARRAY) {
return String.format("()%s", CallbackInfo.OBJECT);
return String.format("()%s", Constants.OBJECT);
}

return String.format("()%s", returnType.getDescriptor());
Expand Down
Loading

0 comments on commit f19f4de

Please sign in to comment.