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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.ClassReader;
  17. import aj.org.objectweb.asm.ClassWriter;
  18. /**
  19. * 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
  20. * process it. The writer is wired into the world so that types can be resolved and getCommonSuperClass() can be implemented without
  21. * class loading using the context class loader.
  22. *
  23. * @author Andy Clement
  24. */
  25. public class StackMapAdder {
  26. public static byte[] addStackMaps(World world, byte[] data) {
  27. try {
  28. ClassReader cr = new ClassReader(data);
  29. ClassWriter cw = new AspectJConnectClassWriter(world);
  30. cr.accept(cw, 0);
  31. return cw.toByteArray();
  32. } catch (Throwable t) {
  33. System.err.println("AspectJ Internal Error: unable to add stackmap attributes. " + t.getMessage());
  34. AsmDetector.isAsmAround = false;
  35. return data;
  36. }
  37. }
  38. private static class AspectJConnectClassWriter extends ClassWriter {
  39. private final World world;
  40. public AspectJConnectClassWriter(World w) {
  41. super(ClassWriter.COMPUTE_FRAMES);
  42. this.world = w;
  43. }
  44. // Implementation of getCommonSuperClass() that avoids Class.forName()
  45. protected String getCommonSuperClass(final String type1, final String type2) {
  46. ResolvedType resolvedType1 = world.resolve(UnresolvedType.forName(type1.replace('/', '.')));
  47. ResolvedType resolvedType2 = world.resolve(UnresolvedType.forName(type2.replace('/', '.')));
  48. if (resolvedType1.isAssignableFrom(resolvedType2)) {
  49. return type1;
  50. }
  51. if (resolvedType2.isAssignableFrom(resolvedType1)) {
  52. return type2;
  53. }
  54. if (resolvedType1.isInterface() || resolvedType2.isInterface()) {
  55. return "java/lang/Object";
  56. } else {
  57. do {
  58. resolvedType1 = resolvedType1.getSuperclass();
  59. } while (!resolvedType1.isAssignableFrom(resolvedType2));
  60. return resolvedType1.getName().replace('.', '/');
  61. }
  62. }
  63. }
  64. }