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.

BcelAttributes.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import org.aspectj.apache.bcel.classfile.Attribute;
  16. import org.aspectj.apache.bcel.classfile.Unknown;
  17. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  18. import org.aspectj.bridge.IMessageHandler;
  19. import org.aspectj.weaver.AjAttribute;
  20. import org.aspectj.weaver.BCException;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  23. // this is a class o' static methods for reading attributes. It's pretty much a bridge from
  24. // bcel to AjAttribute.
  25. class BcelAttributes {
  26. public static List readAjAttributes(String classname,Attribute[] as, ISourceContext context,IMessageHandler msgHandler) {
  27. List l = new ArrayList();
  28. AjAttribute.WeaverVersionInfo version = new WeaverVersionInfo();
  29. for (int i = as.length - 1; i >= 0; i--) {
  30. Attribute a = as[i];
  31. if (a instanceof Unknown) {
  32. Unknown u = (Unknown) a;
  33. String name = u.getName();
  34. if (name.startsWith(AjAttribute.AttributePrefix)) {
  35. AjAttribute attr = AjAttribute.read(version,name,u.getBytes(),context,msgHandler);
  36. if (attr!=null && attr instanceof AjAttribute.WeaverVersionInfo) {
  37. version = (AjAttribute.WeaverVersionInfo)attr;
  38. // Do a version check, this weaver can't process versions
  39. // from a future AspectJ (where the major number has changed)
  40. if (version.getMajorVersion() > WeaverVersionInfo.getCurrentWeaverMajorVersion()) {
  41. throw new BCException("Unable to continue, this version of AspectJ supports classes built with weaver version "+
  42. WeaverVersionInfo.toCurrentVersionString()+" but the class "+classname+" is version "+version.toString());
  43. }
  44. }
  45. if (attr!=null) l.add(attr);
  46. }
  47. }
  48. }
  49. return l;
  50. }
  51. public static Attribute bcelAttribute(AjAttribute a, ConstantPoolGen pool) {
  52. int nameIndex = pool.addUtf8(a.getNameString());
  53. byte[] bytes = a.getBytes();
  54. int length = bytes.length;
  55. return new Unknown(nameIndex, length, bytes, pool.getConstantPool());
  56. }
  57. }