You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestClassAdapter.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm;
  25. import org.objectweb.asm.*;
  26. import org.objectweb.asm.commons.Remapper;
  27. import org.objectweb.asm.commons.RemappingAnnotationAdapter;
  28. import org.objectweb.asm.commons.RemappingClassAdapter;
  29. import org.objectweb.asm.commons.RemappingMethodAdapter;
  30. import java.util.Map;
  31. /**
  32. * @author Ivan Dubrov
  33. */
  34. public class TestClassAdapter extends RemappingClassAdapter {
  35. /**
  36. * This suffix is automatically removed from the method.
  37. */
  38. private final static String METHOD_SUFFIX = "___";
  39. private boolean isObject;
  40. public TestClassAdapter(ClassVisitor cv, final Map<String, String> typeMappings) {
  41. super(cv, new Remapper() {
  42. @Override
  43. public String map(String type) {
  44. return typeMappings.containsKey(type) ? typeMappings.get(type) : type;
  45. }
  46. });
  47. }
  48. @Override
  49. public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
  50. // For java/lang/Object redefinition
  51. String newName = remapper.mapType(name);
  52. if (newName.equals("java/lang/Object")) {
  53. superName = null;
  54. isObject = true;
  55. }
  56. super.visit(version, access, name, signature, superName, interfaces);
  57. }
  58. @Override
  59. public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
  60. return super.visitMethod(access, stripMethodSuffix(name), desc, signature, exceptions);
  61. }
  62. /**
  63. * Get renamed class name.
  64. *
  65. * @return
  66. */
  67. public String getClassName() {
  68. return remapper.mapType(className);
  69. }
  70. protected MethodVisitor createRemappingMethodAdapter(
  71. int access,
  72. String newDesc,
  73. MethodVisitor mv) {
  74. return new RemappingMethodAdapter(access, newDesc, mv, remapper) {
  75. @Override
  76. public void visitMethodInsn(int opcode, String owner, String name, String desc) {
  77. if (name.equals("<init>") && isObject && owner.equals("java/lang/Object")) {
  78. return;
  79. }
  80. super.visitMethodInsn(opcode, owner, stripMethodSuffix(name), desc);
  81. }
  82. };
  83. }
  84. private static String stripMethodSuffix(String name) {
  85. int pos = name.indexOf(METHOD_SUFFIX);
  86. return (pos != -1) ? name.substring(0, pos) : name;
  87. }
  88. @Override
  89. public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
  90. AnnotationVisitor av = super.visitAnnotation(remapper.mapDesc(desc), visible);
  91. return av == null ? null : new RemappingAnnotationAdapter(av, remapper) {
  92. @Override
  93. public void visitEnum(String name, String enumDesc, String value) {
  94. if (Type.getType(enumDesc).getClassName().equals(RedefinitionPolicy.class.getName())) {
  95. RedefinitionPolicy valueAsEnum = RedefinitionPolicy.valueOf(value);
  96. if (Type.getType(desc).getClassName().equals(FieldRedefinitionPolicy.class.getName())) {
  97. cv.visitAttribute(new SingleByteAttribute(FieldRedefinitionPolicy.class.getSimpleName(), (byte) valueAsEnum.ordinal()));
  98. }
  99. if (Type.getType(desc).getClassName().equals(MethodRedefinitionPolicy.class.getName())) {
  100. cv.visitAttribute(new SingleByteAttribute(MethodRedefinitionPolicy.class.getSimpleName(), (byte) valueAsEnum.ordinal()));
  101. }
  102. }
  103. super.visitEnum(name, desc, value);
  104. }
  105. };
  106. }
  107. private static class SingleByteAttribute extends Attribute {
  108. private byte value;
  109. public SingleByteAttribute(String name, byte value) {
  110. super(name);
  111. this.value = value;
  112. }
  113. @Override
  114. protected ByteVector write(ClassWriter writer, byte[] code, int len, int maxStack, int maxLocals) {
  115. return new ByteVector().putByte(value);
  116. }
  117. }
  118. }