Add support for automatically rewriting array types in the rewriter

This adds a new rewriteUnwrappedType to the TypeRewriter class, which
gets called with the unwrapped type for array types, with the potentially
modified return value automatically being re-array-ified.
This commit is contained in:
Ben Gruver 2020-02-02 20:34:29 -08:00
parent 5a900e22e0
commit 78e92b0524
2 changed files with 106 additions and 0 deletions

View File

@ -35,6 +35,29 @@ import javax.annotation.Nonnull;
public class TypeRewriter implements Rewriter<String> {
@Nonnull @Override public String rewrite(@Nonnull String value) {
if (value.length() > 0 && value.charAt(0) == '[') {
int dimensions = value.lastIndexOf("[") + 1;
String arraySpecifiers = value.substring(0, dimensions);
String unwrappedType = value.substring(dimensions);
String rewrittenType = rewriteUnwrappedType(unwrappedType);
return arraySpecifiers + rewrittenType;
} else {
return rewriteUnwrappedType(value);
}
}
/**
* This is called by the default rewrite implementation with the unwrapped type.
*
* <p>For array types, the unwrapped type is the type with the array specifiers removed. And there is no difference
* for non-array types.
*
* @param value The unwrapped type
* @return The modified version of the unwrapped type. This will be re-array-ified if the original wrapped type was
* an array.
*/
@Nonnull public String rewriteUnwrappedType(@Nonnull String value) {
return value;
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2020, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.rewriter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.AnnotationVisibility;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.iface.*;
import org.jf.dexlib2.immutable.*;
import org.junit.Assert;
import org.junit.Test;
import javax.annotation.Nonnull;
public class RewriteArrayTypeTest {
@Test
public void testRewriteArrayTypeTest() {
ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null,
Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)),
Lists.<Field>newArrayList(
new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null, null)
),
Lists.<Method>newArrayList(
new ImmutableMethod("Lcls1", "method1",
Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("[[[Lcls1;", null, null)), "V",
AccessFlags.PUBLIC.getValue(), null, null, null)));
ImmutableDexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(class1));
DexRewriter rewriter = new DexRewriter(new RewriterModule() {
@Nonnull @Override public Rewriter<String> getTypeRewriter(@Nonnull Rewriters rewriters) {
return new TypeRewriter() {
@Nonnull @Override public String rewriteUnwrappedType(@Nonnull String value) {
if (value.equals("Lcls1;")) {
return "Lcls2;";
}
return value;
}
};
}
});
DexFile rewrittenDexFile = rewriter.rewriteDexFile(dexFile);
ClassDef rewrittenClassDef = Lists.newArrayList(rewrittenDexFile.getClasses()).get(0);
Method rewrittenMethodDef = Lists.newArrayList(rewrittenClassDef.getMethods()).get(0);
Assert.assertEquals(rewrittenClassDef.getType(), "Lcls2;");
Assert.assertEquals(rewrittenMethodDef.getParameterTypes().get(0), "[[[Lcls2;");
}
}