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.

BcelCflowCounterFieldAdder.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * (Andy Clement)
  11. *******************************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import org.aspectj.apache.bcel.Constants;
  14. import org.aspectj.apache.bcel.generic.FieldGen;
  15. import org.aspectj.apache.bcel.generic.InstructionFactory;
  16. import org.aspectj.apache.bcel.generic.InstructionList;
  17. import org.aspectj.apache.bcel.generic.ObjectType;
  18. import org.aspectj.apache.bcel.generic.Type;
  19. import org.aspectj.weaver.Member;
  20. import org.aspectj.weaver.NameMangler;
  21. import org.aspectj.weaver.ResolvedMember;
  22. import org.aspectj.weaver.ResolvedType;
  23. /**
  24. * This type munger will modify a given class (see the munge() method) to include
  25. * a field representing a CflowCounter object.
  26. */
  27. public class BcelCflowCounterFieldAdder extends BcelTypeMunger {
  28. private ResolvedMember cflowCounterField;
  29. public BcelCflowCounterFieldAdder(ResolvedMember cflowCounterField) {
  30. super(null,(ResolvedType)cflowCounterField.getDeclaringType());
  31. this.cflowCounterField = cflowCounterField;
  32. }
  33. public boolean munge(BcelClassWeaver weaver) {
  34. LazyClassGen gen = weaver.getLazyClassGen();
  35. // Only munge one type!
  36. if (!gen.getType().equals(cflowCounterField.getDeclaringType())) return false;
  37. // Create the field declaration.
  38. // Something like: "public static final CflowCounter ajc$cflowCounter$0;"
  39. FieldGen f = new FieldGen(cflowCounterField.getModifiers(),
  40. BcelWorld.makeBcelType(cflowCounterField.getReturnType()),
  41. cflowCounterField.getName(),
  42. gen.getConstantPool());
  43. gen.addField(f,getSourceLocation());
  44. // Modify the ajc$preClinit() method to initialize it.
  45. // Something like: "ajc$cflowCounter$0 = new CflowCounter();"
  46. LazyMethodGen clinit = gen.getAjcPreClinit(); //StaticInitializer();
  47. InstructionList setup = new InstructionList();
  48. InstructionFactory fact = gen.getFactory();
  49. setup.append(fact.createNew(new ObjectType(NameMangler.CFLOW_COUNTER_TYPE)));
  50. setup.append(InstructionFactory.createDup(1));
  51. setup.append(fact.createInvoke(
  52. NameMangler.CFLOW_COUNTER_TYPE,
  53. "<init>",
  54. Type.VOID,
  55. new Type[0],
  56. Constants.INVOKESPECIAL));
  57. setup.append(Utility.createSet(fact, cflowCounterField));
  58. clinit.getBody().insert(setup);
  59. return true;
  60. }
  61. public ResolvedMember getMatchingSyntheticMember(Member member) {
  62. return null;
  63. }
  64. public ResolvedMember getSignature() {
  65. return cflowCounterField;
  66. }
  67. public boolean matches(ResolvedType onType) {
  68. return onType.equals(cflowCounterField.getDeclaringType());
  69. }
  70. public boolean existsToSupportShadowMunging() {
  71. return true;
  72. }
  73. public String toString() {
  74. return "(BcelTypeMunger: CflowField "+cflowCounterField.getDeclaringType().getName()+" "+cflowCounterField.getName()+")";
  75. }
  76. }