Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StackMapAdder.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* *******************************************************************
  2. * Copyright (c) 2008 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.*;
  17. /**
  18. * 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
  19. * process it. The writer is wired into the world so that types can be resolved and getCommonSuperClass() can be implemented without
  20. * class loading using the context class loader.
  21. *
  22. * It is important that the constant pool is preserved here and asm does not try to remove unused entries. That is because some
  23. * entries are refered to from classfile attributes. Asm cannot see into these attributes so does not realise the constant pool
  24. * entries are in use. In order to ensure the copying of cp occurs, we use the variant super constructor call in AspectJConnectClassWriter
  25. * that passes in the classreader. However, ordinarily that change causes a further optimization: that if a classreader sees
  26. * a methodvisitor that has been created by a ClassWriter then it just copies the data across without changing it (and so it
  27. * fails to attach the stackmapattribute). In order to avoid this further optimization we use our own minimal MethodVisitor.
  28. *
  29. * @author Andy Clement
  30. */
  31. public class StackMapAdder {
  32. public static byte[] addStackMaps(World world, byte[] data) {
  33. try {
  34. ClassReader cr = new ClassReader(data);
  35. ClassWriter cw = new AspectJConnectClassWriter(cr, world);
  36. ClassVisitor cv = new AspectJClassVisitor(cw);
  37. cr.accept(cv, 0);
  38. return cw.toByteArray();
  39. } catch (Throwable t) {
  40. System.err.println("AspectJ Internal Error: unable to add stackmap attributes. " + t.getMessage());
  41. AsmDetector.isAsmAround = false;
  42. return data;
  43. }
  44. }
  45. private static class AspectJClassVisitor extends ClassVisitor {
  46. public AspectJClassVisitor(ClassVisitor classwriter) {
  47. super(Opcodes.ASM5, classwriter);
  48. }
  49. @Override
  50. public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
  51. MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
  52. return new AJMethodVisitor(mv);
  53. }
  54. // Minimal pass through MethodVisitor just so that the ClassReader doesn't see one that has been directly
  55. // created by a ClassWriter (see top level class comment)
  56. static class AJMethodVisitor extends MethodVisitor {
  57. public AJMethodVisitor(MethodVisitor mv) {
  58. super(Opcodes.ASM5,mv);
  59. }
  60. }
  61. }
  62. private static class AspectJConnectClassWriter extends ClassWriter {
  63. private final World world;
  64. public AspectJConnectClassWriter(ClassReader cr, World w) {
  65. super(cr, ClassWriter.COMPUTE_FRAMES); // passing in cr is necessary so cpool isnt modified (see 2.2.4 of asm doc)
  66. this.world = w;
  67. }
  68. // Implementation of getCommonSuperClass() that avoids Class.forName()
  69. protected String getCommonSuperClass(final String type1, final String type2) {
  70. ResolvedType resolvedType1 = world.resolve(UnresolvedType.forName(type1.replace('/', '.')));
  71. ResolvedType resolvedType2 = world.resolve(UnresolvedType.forName(type2.replace('/', '.')));
  72. if (resolvedType1.isAssignableFrom(resolvedType2)) {
  73. return type1;
  74. }
  75. if (resolvedType2.isAssignableFrom(resolvedType1)) {
  76. return type2;
  77. }
  78. if (resolvedType1.isInterface() || resolvedType2.isInterface()) {
  79. return "java/lang/Object";
  80. } else {
  81. do {
  82. resolvedType1 = resolvedType1.getSuperclass();
  83. } while (!resolvedType1.isAssignableFrom(resolvedType2));
  84. return resolvedType1.getRawName().replace('.', '/');
  85. }
  86. }
  87. }
  88. }