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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.ClassVisitor;
  26. import org.objectweb.asm.MethodVisitor;
  27. import org.objectweb.asm.commons.Remapper;
  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. {
  75. return new RemappingMethodAdapter(access, newDesc, mv, remapper) {
  76. @Override
  77. public void visitMethodInsn(int opcode, String owner, String name, String desc) {
  78. if (name.equals("<init>") && isObject && owner.equals("java/lang/Object")) {
  79. return;
  80. }
  81. super.visitMethodInsn(opcode, owner, stripMethodSuffix(name), desc);
  82. }
  83. };
  84. }
  85. private static String stripMethodSuffix(String name) {
  86. int pos = name.indexOf(METHOD_SUFFIX);
  87. return (pos != -1) ? name.substring(0, pos) : name;
  88. }
  89. }