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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 a field representing a CflowCounter object.
  25. */
  26. public class BcelCflowCounterFieldAdder extends BcelTypeMunger {
  27. private ResolvedMember cflowCounterField;
  28. public BcelCflowCounterFieldAdder(ResolvedMember cflowCounterField) {
  29. super(null, (ResolvedType) cflowCounterField.getDeclaringType());
  30. this.cflowCounterField = cflowCounterField;
  31. }
  32. public boolean munge(BcelClassWeaver weaver) {
  33. LazyClassGen gen = weaver.getLazyClassGen();
  34. // Only munge one type!
  35. if (!gen.getType().equals(cflowCounterField.getDeclaringType()))
  36. return false;
  37. // Create the field declaration.
  38. // Something like: "public static final CflowCounter ajc$cflowCounter$0;"
  39. FieldGen f = new FieldGen(cflowCounterField.getModifiers(), BcelWorld.makeBcelType(cflowCounterField.getReturnType()),
  40. cflowCounterField.getName(), gen.getConstantPool());
  41. gen.addField(f, getSourceLocation());
  42. // Modify the ajc$preClinit() method to initialize it.
  43. // Something like: "ajc$cflowCounter$0 = new CflowCounter();"
  44. LazyMethodGen clinit = gen.getAjcPreClinit(); // StaticInitializer();
  45. InstructionList setup = new InstructionList();
  46. InstructionFactory fact = gen.getFactory();
  47. setup.append(fact.createNew(new ObjectType(NameMangler.CFLOW_COUNTER_TYPE)));
  48. setup.append(InstructionFactory.createDup(1));
  49. setup.append(fact.createInvoke(NameMangler.CFLOW_COUNTER_TYPE, "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
  50. setup.append(Utility.createSet(fact, cflowCounterField));
  51. clinit.getBody().insert(setup);
  52. return true;
  53. }
  54. public ResolvedMember getMatchingSyntheticMember(Member member) {
  55. return null;
  56. }
  57. public ResolvedMember getSignature() {
  58. return cflowCounterField;
  59. }
  60. public boolean matches(ResolvedType onType) {
  61. return onType.equals(cflowCounterField.getDeclaringType());
  62. }
  63. public boolean existsToSupportShadowMunging() {
  64. return true;
  65. }
  66. public String toString() {
  67. return "(BcelTypeMunger: CflowField " + cflowCounterField.getDeclaringType().getName() + " " + cflowCounterField.getName()
  68. + ")";
  69. }
  70. }