Commit cc4ed6a5 authored by Eric Bruneton's avatar Eric Bruneton

Improve the code quality of the [...]Remapper classes.

parent 3224adf0
......@@ -32,42 +32,72 @@ import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;
/**
* An {@link AnnotationVisitor} adapter for type remapping.
* An {@link AnnotationVisitor} that remaps types with a {@link Remapper}.
*
* @author Eugene Kuleshov
*/
public class AnnotationRemapper extends AnnotationVisitor {
/** The remapper used to remap the types in the visited annotation. */
protected final Remapper remapper;
public AnnotationRemapper(final AnnotationVisitor av, final Remapper remapper) {
this(Opcodes.ASM6, av, remapper);
/**
* Constructs a new {@link AnnotationRemapper}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #AnnotationRemapper(int,AnnotationVisitor,Remapper)} version.
*
* @param annotationVisitor the annotation visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited annotation.
*/
public AnnotationRemapper(final AnnotationVisitor annotationVisitor, final Remapper remapper) {
this(Opcodes.ASM6, annotationVisitor, remapper);
}
protected AnnotationRemapper(final int api, final AnnotationVisitor av, final Remapper remapper) {
super(api, av);
/**
* Constructs a new {@link AnnotationRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be one of {@link
* org.objectweb.asm.Opcodes#ASM4}, {@link org.objectweb.asm.Opcodes#ASM5} or {@link
* org.objectweb.asm.Opcodes#ASM6}.
* @param annotationVisitor the annotation visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited annotation.
*/
protected AnnotationRemapper(
final int api, final AnnotationVisitor annotationVisitor, final Remapper remapper) {
super(api, annotationVisitor);
this.remapper = remapper;
}
@Override
public void visit(String name, Object value) {
av.visit(name, remapper.mapValue(value));
public void visit(final String name, final Object value) {
super.visit(name, remapper.mapValue(value));
}
@Override
public void visitEnum(String name, String desc, String value) {
av.visitEnum(name, remapper.mapDesc(desc), value);
public void visitEnum(final String name, final String descriptor, final String value) {
super.visitEnum(name, remapper.mapDesc(descriptor), value);
}
@Override
public AnnotationVisitor visitAnnotation(String name, String desc) {
AnnotationVisitor v = av.visitAnnotation(name, remapper.mapDesc(desc));
return v == null ? null : (v == av ? this : new AnnotationRemapper(api, v, remapper));
public AnnotationVisitor visitAnnotation(final String name, final String descriptor) {
AnnotationVisitor annotationVisitor = super.visitAnnotation(name, remapper.mapDesc(descriptor));
if (annotationVisitor == null) {
return null;
} else {
return annotationVisitor == av
? this
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
}
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor v = av.visitArray(name);
return v == null ? null : (v == av ? this : new AnnotationRemapper(api, v, remapper));
public AnnotationVisitor visitArray(final String name) {
AnnotationVisitor annotationVisitor = super.visitArray(name);
if (annotationVisitor == null) {
return null;
} else {
return annotationVisitor == av
? this
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
}
}
......@@ -40,33 +40,51 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;
/**
* A {@link ClassVisitor} for type remapping.
* A {@link ClassVisitor} that remaps types with a {@link Remapper}.
*
* @author Eugene Kuleshov
*/
public class ClassRemapper extends ClassVisitor {
/** The remapper used to remap the types in the visited class. */
protected final Remapper remapper;
/** The internal name of the visited class. */
protected String className;
public ClassRemapper(final ClassVisitor cv, final Remapper remapper) {
this(Opcodes.ASM6, cv, remapper);
/**
* Constructs a new {@link ClassRemapper}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #ClassRemapper(int,ClassVisitor,Remapper)} version.
*
* @param classVisitor the class visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited class.
*/
public ClassRemapper(final ClassVisitor classVisitor, final Remapper remapper) {
this(Opcodes.ASM6, classVisitor, remapper);
}
protected ClassRemapper(final int api, final ClassVisitor cv, final Remapper remapper) {
super(api, cv);
/**
* Constructs a new {@link ClassRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be one of {@link
* org.objectweb.asm.Opcodes#ASM4}, {@link org.objectweb.asm.Opcodes#ASM5} or {@link
* org.objectweb.asm.Opcodes#ASM6}.
* @param classVisitor the class visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited class.
*/
protected ClassRemapper(final int api, final ClassVisitor classVisitor, final Remapper remapper) {
super(api, classVisitor);
this.remapper = remapper;
}
@Override
public void visit(
int version,
int access,
String name,
String signature,
String superName,
String[] interfaces) {
final int version,
final int access,
final String name,
final String signature,
final String superName,
final String[] interfaces) {
this.className = name;
super.visit(
version,
......@@ -78,67 +96,76 @@ public class ClassRemapper extends ClassVisitor {
}
@Override
public ModuleVisitor visitModule(String name, int flags, String version) {
ModuleVisitor mv = super.visitModule(remapper.mapModuleName(name), flags, version);
return mv == null ? null : createModuleRemapper(mv);
public ModuleVisitor visitModule(final String name, final int flags, final String version) {
ModuleVisitor moduleVisitor = super.visitModule(remapper.mapModuleName(name), flags, version);
return moduleVisitor == null ? null : createModuleRemapper(moduleVisitor);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(remapper.mapDesc(desc), visible);
return av == null ? null : createAnnotationRemapper(av);
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitAnnotation(remapper.mapDesc(descriptor), visible);
return annotationVisitor == null ? null : createAnnotationRemapper(annotationVisitor);
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
AnnotationVisitor av =
super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible);
return av == null ? null : createAnnotationRemapper(av);
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(descriptor), visible);
return annotationVisitor == null ? null : createAnnotationRemapper(annotationVisitor);
}
@Override
public void visitAttribute(Attribute attr) {
if (attr instanceof ModuleHashesAttribute) {
ModuleHashesAttribute hashesAttr = new ModuleHashesAttribute();
List<String> modules = hashesAttr.modules;
for (int i = 0; i < modules.size(); i++) {
public void visitAttribute(final Attribute attribute) {
if (attribute instanceof ModuleHashesAttribute) {
ModuleHashesAttribute moduleHashesAttribute = new ModuleHashesAttribute();
List<String> modules = moduleHashesAttribute.modules;
for (int i = 0; i < modules.size(); ++i) {
modules.set(i, remapper.mapModuleName(modules.get(i)));
}
}
super.visitAttribute(attr);
super.visitAttribute(attribute);
}
@Override
public FieldVisitor visitField(
int access, String name, String desc, String signature, Object value) {
FieldVisitor fv =
final int access,
final String name,
final String descriptor,
final String signature,
final Object value) {
FieldVisitor fieldVisitor =
super.visitField(
access,
remapper.mapFieldName(className, name, desc),
remapper.mapDesc(desc),
remapper.mapFieldName(className, name, descriptor),
remapper.mapDesc(descriptor),
remapper.mapSignature(signature, true),
remapper.mapValue(value));
return fv == null ? null : createFieldRemapper(fv);
return fieldVisitor == null ? null : createFieldRemapper(fieldVisitor);
}
@Override
public MethodVisitor visitMethod(
int access, String name, String desc, String signature, String[] exceptions) {
String newDesc = remapper.mapMethodDesc(desc);
MethodVisitor mv =
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
String remappedDescriptor = remapper.mapMethodDesc(descriptor);
MethodVisitor methodVisitor =
super.visitMethod(
access,
remapper.mapMethodName(className, name, desc),
newDesc,
remapper.mapMethodName(className, name, descriptor),
remappedDescriptor,
remapper.mapSignature(signature, false),
exceptions == null ? null : remapper.mapTypes(exceptions));
return mv == null ? null : createMethodRemapper(mv);
return methodVisitor == null ? null : createMethodRemapper(methodVisitor);
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
// TODO should innerName be changed?
public void visitInnerClass(
final String name, final String outerName, final String innerName, final int access) {
super.visitInnerClass(
remapper.mapType(name),
outerName == null ? null : remapper.mapType(outerName),
......@@ -147,26 +174,54 @@ public class ClassRemapper extends ClassVisitor {
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
public void visitOuterClass(final String owner, final String name, final String descriptor) {
super.visitOuterClass(
remapper.mapType(owner),
name == null ? null : remapper.mapMethodName(owner, name, desc),
desc == null ? null : remapper.mapMethodDesc(desc));
name == null ? null : remapper.mapMethodName(owner, name, descriptor),
descriptor == null ? null : remapper.mapMethodDesc(descriptor));
}
protected FieldVisitor createFieldRemapper(FieldVisitor fv) {
return new FieldRemapper(api, fv, remapper);
/**
* Constructs a new remapper for fields. The default implementation of this method returns a new
* {@link FieldRemapper}.
*
* @param fieldVisitor the FieldVisitor the remapper must delegate to.
* @return the newly created remapper.
*/
protected FieldVisitor createFieldRemapper(final FieldVisitor fieldVisitor) {
return new FieldRemapper(api, fieldVisitor, remapper);
}
protected MethodVisitor createMethodRemapper(MethodVisitor mv) {
return new MethodRemapper(api, mv, remapper);
/**
* Constructs a new remapper for methods. The default implementation of this method returns a new
* {@link MethodRemapper}.
*
* @param methodVisitor the MethodVisitor the remapper must delegate to.
* @return the newly created remapper.
*/
protected MethodVisitor createMethodRemapper(final MethodVisitor methodVisitor) {
return new MethodRemapper(api, methodVisitor, remapper);
}
protected AnnotationVisitor createAnnotationRemapper(AnnotationVisitor av) {
return new AnnotationRemapper(api, av, remapper);
/**
* Constructs a new remapper for annotations. The default implementation of this method returns a
* new {@link AnnotationRemapper}.
*
* @param annotationVisitor the AnnotationVisitor the remapper must delegate to.
* @return the newly created remapper.
*/
protected AnnotationVisitor createAnnotationRemapper(final AnnotationVisitor annotationVisitor) {
return new AnnotationRemapper(api, annotationVisitor, remapper);
}
protected ModuleVisitor createModuleRemapper(ModuleVisitor mv) {
return new ModuleRemapper(api, mv, remapper);
/**
* Constructs a new remapper for modules. The default implementation of this method returns a new
* {@link ModuleRemapper}.
*
* @param moduleVisitor the ModuleVisitor the remapper must delegate to.
* @return the newly created remapper.
*/
protected ModuleVisitor createModuleRemapper(final ModuleVisitor moduleVisitor) {
return new ModuleRemapper(api, moduleVisitor, remapper);
}
}
......@@ -34,34 +34,56 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;
/**
* A {@link FieldVisitor} adapter for type remapping.
* A {@link FieldVisitor} that remaps types with a {@link Remapper}.
*
* @author Eugene Kuleshov
*/
public class FieldRemapper extends FieldVisitor {
private final Remapper remapper;
/** The remapper used to remap the types in the visited field. */
protected final Remapper remapper;
public FieldRemapper(final FieldVisitor fv, final Remapper remapper) {
this(Opcodes.ASM6, fv, remapper);
/**
* Constructs a new {@link FieldRemapper}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #FieldRemapper(int,FieldVisitor,Remapper)} version.
*
* @param fieldVisitor the field visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited field.
*/
public FieldRemapper(final FieldVisitor fieldVisitor, final Remapper remapper) {
this(Opcodes.ASM6, fieldVisitor, remapper);
}
protected FieldRemapper(final int api, final FieldVisitor fv, final Remapper remapper) {
super(api, fv);
/**
* Constructs a new {@link FieldRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be one of {@link
* org.objectweb.asm.Opcodes#ASM4}, {@link org.objectweb.asm.Opcodes#ASM5} or {@link
* org.objectweb.asm.Opcodes#ASM6}.
* @param fieldVisitor the field visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited field.
*/
protected FieldRemapper(final int api, final FieldVisitor fieldVisitor, final Remapper remapper) {
super(api, fieldVisitor);
this.remapper = remapper;
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = fv.visitAnnotation(remapper.mapDesc(desc), visible);
return av == null ? null : new AnnotationRemapper(api, av, remapper);
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitAnnotation(remapper.mapDesc(descriptor), visible);
return annotationVisitor == null
? null
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
AnnotationVisitor av =
super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible);
return av == null ? null : new AnnotationRemapper(api, av, remapper);
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(descriptor), visible);
return annotationVisitor == null
? null
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
}
......@@ -32,72 +32,91 @@ import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;
/**
* A {@link ModuleVisitor} adapter for type remapping.
* A {@link ModuleVisitor} that remaps types with a {@link Remapper}.
*
* @author Remi Forax
*/
public class ModuleRemapper extends ModuleVisitor {
private final Remapper remapper;
public ModuleRemapper(final ModuleVisitor mv, final Remapper remapper) {
this(Opcodes.ASM6, mv, remapper);
/** The remapper used to remap the types in the visited module. */
protected final Remapper remapper;
/**
* Constructs a new {@link ModuleRemapper}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #ModuleRemapper(int,ModuleVisitor,Remapper)} version.
*
* @param moduleVisitor the module visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited module.
*/
public ModuleRemapper(final ModuleVisitor moduleVisitor, final Remapper remapper) {
this(Opcodes.ASM6, moduleVisitor, remapper);
}
protected ModuleRemapper(final int api, final ModuleVisitor mv, final Remapper remapper) {
super(api, mv);
/**
* Constructs a new {@link ModuleRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be one of {@link
* org.objectweb.asm.Opcodes#ASM4}, {@link org.objectweb.asm.Opcodes#ASM5} or {@link
* org.objectweb.asm.Opcodes#ASM6}.
* @param moduleVisitor the module visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited module.
*/
protected ModuleRemapper(
final int api, final ModuleVisitor moduleVisitor, final Remapper remapper) {
super(api, moduleVisitor);
this.remapper = remapper;
}
@Override
public void visitMainClass(String mainClass) {
public void visitMainClass(final String mainClass) {
super.visitMainClass(remapper.mapType(mainClass));
}
@Override
public void visitPackage(String packaze) {
public void visitPackage(final String packaze) {
super.visitPackage(remapper.mapPackageName(packaze));
}
@Override
public void visitRequire(String module, int access, String version) {
public void visitRequire(final String module, final int access, final String version) {
super.visitRequire(remapper.mapModuleName(module), access, version);
}
@Override
public void visitExport(String packaze, int access, String... modules) {
String[] newModules = null;
public void visitExport(final String packaze, final int access, final String... modules) {
String[] remappedModules = null;
if (modules != null) {
newModules = new String[modules.length];
for (int i = 0; i < modules.length; i++) {
newModules[i] = remapper.mapModuleName(modules[i]);
remappedModules = new String[modules.length];
for (int i = 0; i < modules.length; ++i) {
remappedModules[i] = remapper.mapModuleName(modules[i]);
}
}
super.visitExport(remapper.mapPackageName(packaze), access, newModules);
super.visitExport(remapper.mapPackageName(packaze), access, remappedModules);
}
@Override
public void visitOpen(String packaze, int access, String... modules) {
String[] newModules = null;
public void visitOpen(final String packaze, final int access, final String... modules) {
String[] remappedModules = null;
if (modules != null) {
newModules = new String[modules.length];
for (int i = 0; i < modules.length; i++) {
newModules[i] = remapper.mapModuleName(modules[i]);
remappedModules = new String[modules.length];
for (int i = 0; i < modules.length; ++i) {
remappedModules[i] = remapper.mapModuleName(modules[i]);
}
}
super.visitOpen(remapper.mapPackageName(packaze), access, newModules);
super.visitOpen(remapper.mapPackageName(packaze), access, remappedModules);
}
@Override
public void visitUse(String service) {
public void visitUse(final String service) {
super.visitUse(remapper.mapType(service));
}
@Override
public void visitProvide(String service, String... providers) {
String[] newProviders = new String[providers.length];
for (int i = 0; i < providers.length; i++) {
newProviders[i] = remapper.mapType(providers[i]);
public void visitProvide(final String service, final String... providers) {
String[] remappedProviders = new String[providers.length];
for (int i = 0; i < providers.length; ++i) {
remappedProviders[i] = remapper.mapType(providers[i]);
}
super.visitProvide(remapper.mapType(service), newProviders);
super.visitProvide(remapper.mapType(service), remappedProviders);
}
}
......@@ -28,131 +28,148 @@
package org.objectweb.asm.commons;
import java.util.Stack;
import java.util.ArrayList;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.signature.SignatureVisitor;
/**
* A {@link SignatureVisitor} adapter for type mapping.
* A {@link SignatureVisitor} that remaps types with a {@link Remapper}.
*
* @author Eugene Kuleshov
*/
public class SignatureRemapper extends SignatureVisitor {
private final SignatureVisitor v;
private final SignatureVisitor signatureVisitor;
private final Remapper remapper;
private Stack<String> classNames = new Stack<String>();
public SignatureRemapper(final SignatureVisitor v, final Remapper remapper) {
this(Opcodes.ASM6, v, remapper);
}
protected SignatureRemapper(final int api, final SignatureVisitor v, final Remapper remapper) {
private ArrayList<String> classNames = new ArrayList<String>();
/**
* Constructs a new {@link SignatureRemapper}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #SignatureRemapper(int,SignatureVisitor,Remapper)} version.
*
* @param signatureVisitor the signature visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited signature.
*/
public SignatureRemapper(final SignatureVisitor signatureVisitor, final Remapper remapper) {
this(Opcodes.ASM6, signatureVisitor, remapper);
}
/**
* Constructs a new {@link SignatureRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be one of {@link
* org.objectweb.asm.Opcodes#ASM4}, {@link org.objectweb.asm.Opcodes#ASM5} or {@link
* org.objectweb.asm.Opcodes#ASM6}.
* @param signatureVisitor the signature visitor this remapper must deleted to.
* @param remapper the remapper to use to remap the types in the visited signature.
*/
protected SignatureRemapper(
final int api, final SignatureVisitor signatureVisitor, final Remapper remapper) {
super(api);
this.v = v;
this.signatureVisitor = signatureVisitor;
this.remapper = remapper;
}
@Override
public void visitClassType(String name) {
classNames.push(name);
v.visitClassType(remapper.mapType(name));
public void visitClassType(final String name) {
classNames.add(name);
signatureVisitor.visitClassType(remapper.mapType(name));
}
@Override
public void visitInnerClassType(String name) {
String outerClassName = classNames.pop();
public void visitInnerClassType(final String name) {
String outerClassName = classNames.remove(classNames.size() - 1);
String className = outerClassName + '$' + name;
classNames.push(className);
classNames.add(className);
String remappedOuter = remapper.mapType(outerClassName) + '$';
String remappedName = remapper.mapType(className);
int index =
remappedName.startsWith(remappedOuter)
? remappedOuter.length()
: remappedName.lastIndexOf('$') + 1;
v.visitInnerClassType(remappedName.substring(index));
signatureVisitor.visitInnerClassType(remappedName.substring(index));
}
@Override
public void visitFormalTypeParameter(String name) {
v.visitFormalTypeParameter(name);
public void visitFormalTypeParameter(final String name) {
signatureVisitor.visitFormalTypeParameter(name);
}
@Override
public void visitTypeVariable(String name) {
v.visitTypeVariable(name);
public void visitTypeVariable(final String name) {
signatureVisitor.visitTypeVariable(name);
}
@Override
public SignatureVisitor visitArrayType() {
v.visitArrayType();
signatureVisitor.visitArrayType();
return this;
}
@Override
public void visitBaseType(char descriptor) {
v.visitBaseType(descriptor);
public void visitBaseType(final char descriptor) {
signatureVisitor.visitBaseType(descriptor);
}
@Override
public SignatureVisitor visitClassBound() {
v.visitClassBound();
signatureVisitor.visitClassBound();
return this;
}
@Override
public SignatureVisitor visitExceptionType() {
v.visitExceptionType();
signatureVisitor.visitExceptionType();
return this;
}
@Override
public SignatureVisitor visitInterface() {
v.visitInterface();
signatureVisitor.visitInterface();
return this;
}
@Override
public SignatureVisitor visitInterfaceBound() {
v.visitInterfaceBound();
signatureVisitor.visitInterfaceBound();
return this;
}
@Override
public SignatureVisitor visitParameterType() {
v.visitParameterType();
signatureVisitor.visitParameterType();
return this;
}
@Override