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.

StackMapAdder.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* *******************************************************************
  2. * Copyright (c) 2008, 2018 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel.asm;
  13. import org.aspectj.weaver.ResolvedType;
  14. import org.aspectj.weaver.UnresolvedType;
  15. import org.aspectj.weaver.World;
  16. import aj.org.objectweb.asm.ClassReader;
  17. import aj.org.objectweb.asm.ClassVisitor;
  18. import aj.org.objectweb.asm.ClassWriter;
  19. import aj.org.objectweb.asm.MethodVisitor;
  20. import aj.org.objectweb.asm.Opcodes;
  21. /**
  22. * Uses asm to add the stack map attribute to methods in a class. The class is passed in as pure byte data and then a reader/writer
  23. * process it. The writer is wired into the world so that types can be resolved and getCommonSuperClass() can be implemented without
  24. * class loading using the context class loader.
  25. *
  26. * It is important that the constant pool is preserved here and asm does not try to remove unused entries. That is because some
  27. * entries are refered to from classfile attributes. Asm cannot see into these attributes so does not realise the constant pool
  28. * entries are in use. In order to ensure the copying of cp occurs, we use the variant super constructor call in AspectJConnectClassWriter
  29. * that passes in the classreader. However, ordinarily that change causes a further optimization: that if a classreader sees
  30. * a methodvisitor that has been created by a ClassWriter then it just copies the data across without changing it (and so it
  31. * fails to attach the stackmapattribute). In order to avoid this further optimization we use our own minimal MethodVisitor.
  32. *
  33. * @author Andy Clement
  34. */
  35. public class StackMapAdder {
  36. public static byte[] addStackMaps(World world, byte[] data) {
  37. try {
  38. ClassReader cr = new ClassReader(data);
  39. ClassWriter cw = new AspectJConnectClassWriter(cr, world);
  40. ClassVisitor cv = new AspectJClassVisitor(cw);
  41. cr.accept(cv, 0);
  42. return cw.toByteArray();
  43. } catch (Throwable t) {
  44. System.err.println("AspectJ Internal Error: unable to add stackmap attributes. " + t.getMessage());
  45. t.printStackTrace();
  46. AsmDetector.isAsmAround = false;
  47. return data;
  48. }
  49. }
  50. private static class AspectJClassVisitor extends ClassVisitor {
  51. public AspectJClassVisitor(ClassVisitor classwriter) {
  52. super(Opcodes.ASM7, classwriter);
  53. }
  54. @Override
  55. public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
  56. MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
  57. return new AJMethodVisitor(mv);
  58. }
  59. // Minimal pass through MethodVisitor just so that the ClassReader doesn't see one that has been directly
  60. // created by a ClassWriter (see top level class comment)
  61. static class AJMethodVisitor extends MethodVisitor {
  62. public AJMethodVisitor(MethodVisitor mv) {
  63. super(Opcodes.ASM7,mv);
  64. }
  65. }
  66. }
  67. private static class AspectJConnectClassWriter extends ClassWriter {
  68. private final World world;
  69. public AspectJConnectClassWriter(ClassReader cr, World w) {
  70. super(cr, ClassWriter.COMPUTE_FRAMES); // passing in cr is necessary so cpool isnt modified (see 2.2.4 of asm doc)
  71. this.world = w;
  72. }
  73. // Implementation of getCommonSuperClass() that avoids Class.forName()
  74. @Override
  75. protected String getCommonSuperClass(final String type1, final String type2) {
  76. ResolvedType resolvedType1 = world.resolve(UnresolvedType.forName(type1.replace('/', '.')));
  77. ResolvedType resolvedType2 = world.resolve(UnresolvedType.forName(type2.replace('/', '.')));
  78. if (resolvedType1.isAssignableFrom(resolvedType2)) {
  79. return type1;
  80. }
  81. if (resolvedType2.isAssignableFrom(resolvedType1)) {
  82. return type2;
  83. }
  84. if (resolvedType1.isInterface() || resolvedType2.isInterface()) {
  85. return "java/lang/Object";
  86. } else {
  87. do {
  88. resolvedType1 = resolvedType1.getSuperclass();
  89. if (resolvedType1 == null) {
  90. // This happens if some types are missing, the getSuperclass() call on
  91. // MissingResolvedTypeWithKnownSignature will return the Missing type which
  92. // in turn returns a superclass of null. By returning Object here it
  93. // should surface the cantFindType message raised in the first problematic
  94. // getSuperclass call
  95. return "java/lang/Object";
  96. }
  97. if (resolvedType1.isParameterizedOrGenericType()) {
  98. resolvedType1 = resolvedType1.getRawType();
  99. }
  100. } while (!resolvedType1.isAssignableFrom(resolvedType2));
  101. return resolvedType1.getRawName().replace('.', '/');
  102. }
  103. }
  104. }
  105. }