123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /* *******************************************************************
- * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Common Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * Xerox/PARC initial implementation
- * ******************************************************************/
-
-
- package org.aspectj.weaver;
-
- import java.io.*;
- import java.lang.reflect.Modifier;
- import java.util.*;
-
- import org.apache.bcel.Constants;
- import org.apache.bcel.classfile.Field;
- import org.aspectj.weaver.ast.Var;
- import org.aspectj.weaver.patterns.IScope;
- import org.aspectj.bridge.*;
- import org.aspectj.bridge.SourceLocation;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.util.*;
-
- /*
- * The superclass of anything representing a the shadow of a join point. A shadow represents
- * some bit of code, and encompasses both entry and exit from that code. All shadows have a kind
- * and a signature.
- */
-
- public abstract class Shadow {
- private final Kind kind;
- private final Member signature;
- protected List mungers = new ArrayList(1);
-
- // ----
-
- protected Shadow(Kind kind, Member signature) {
- this.kind = kind;
- this.signature = signature;
- }
-
- // ----
-
- public abstract World getIWorld();
-
- public final boolean hasTarget() {
- return !(getSignature().isStatic() || (getKind() == ConstructorCall)
- || (getKind() == PreInitialization));
- }
-
- public final TypeX getTargetType() {
- if (!hasTarget()) return ResolvedTypeX.MISSING;
- return getSignature().getDeclaringType();
- }
-
- public TypeX[] getArgTypes() {
- if (getKind() == FieldSet) return new TypeX[] { getSignature().getReturnType() };
- return getSignature().getParameterTypes();
- }
-
- public TypeX getArgType(int arg) {
- if (getKind() == FieldSet) return getSignature().getReturnType();
- return getSignature().getParameterTypes()[arg];
- }
-
- public int getArgCount() {
- if (getKind() == FieldSet) return 1;
- return getSignature()
- .getParameterTypes().length;
- }
-
- public abstract boolean hasThis();
- public abstract TypeX getThisType();
- public abstract TypeX getEnclosingType();
- public abstract Var getThisVar();
- public abstract Var getTargetVar();
- public abstract Var getArgVar(int i);
-
- public abstract Var getThisJoinPointVar();
- public abstract Var getThisJoinPointStaticPartVar();
- public abstract Var getThisEnclosingJoinPointStaticPartVar();
-
- public abstract Member getEnclosingCodeSignature();
-
-
- /** returns the kind of shadow this is, representing what happens under this shadow
- */
- public Kind getKind() {
- return kind;
- }
-
- /** returns the signature of the thing under this shadow
- */
- public Member getSignature() {
- return signature;
- }
-
-
- public TypeX getReturnType() {
- if (kind == ConstructorCall) return getSignature().getDeclaringType();
- else if (kind == FieldSet) return ResolvedTypeX.VOID;
- return getSignature().getReturnType();
- }
-
-
- /**
- * These names are the ones that will be returned by thisJoinPoint.getKind()
- * Those need to be documented somewhere
- */
- public static final Kind MethodCall = new Kind(JoinPoint.METHOD_CALL, 1, true);
- public static final Kind ConstructorCall = new Kind(JoinPoint.CONSTRUCTOR_CALL, 2, true);
- public static final Kind MethodExecution = new Kind(JoinPoint.METHOD_EXECUTION, 3, false);
- public static final Kind ConstructorExecution = new Kind(JoinPoint.CONSTRUCTOR_EXECUTION, 4, false);
- public static final Kind FieldGet = new Kind(JoinPoint.FIELD_GET, 5, true);
- public static final Kind FieldSet = new Kind(JoinPoint.FIELD_SET, 6, true);
- public static final Kind StaticInitialization = new Kind(JoinPoint.STATICINITIALIZATION, 7, false);
- public static final Kind PreInitialization = new Kind(JoinPoint.PREINTIALIZATION, 8, false);
- public static final Kind AdviceExecution = new Kind(JoinPoint.ADVICE_EXECUTION, 9, false);
- public static final Kind Initialization = new Kind(JoinPoint.INITIALIZATION, 10, false);
- public static final Kind ExceptionHandler = new Kind(JoinPoint.EXCEPTION_HANDLER, 11, true);
-
-
- /** A type-safe enum representing the kind of shadows
- */
- public static final class Kind extends TypeSafeEnum {
- private boolean argsOnStack;
-
- public Kind(String name, int key, boolean argsOnStack) {
- super(name, key);
- this.argsOnStack = argsOnStack;
- }
-
- public String toLegalJavaIdentifier() {
- return getName().replace('-', '_');
- }
-
- public boolean argsOnStack() {
- return argsOnStack;
- }
-
- // !!! this is false for handlers!
- public boolean allowsExtraction() {
- return true;
- }
-
- public String getSimpleName() {
- int dash = getName().lastIndexOf('-');
- if (dash == -1) return getName();
- else return getName().substring(dash+1);
- }
-
- public static Kind read(DataInputStream s) throws IOException {
- int key = s.readByte();
- switch(key) {
- case 1: return MethodCall;
- case 2: return ConstructorCall;
- case 3: return MethodExecution;
- case 4: return ConstructorExecution;
- case 5: return FieldGet;
- case 6: return FieldSet;
- case 7: return StaticInitialization;
- case 8: return PreInitialization;
- case 9: return AdviceExecution;
- case 10: return Initialization;
- case 11: return ExceptionHandler;
- }
- throw new BCException("unknown kind: " + key);
- }
- }
-
- public void addMunger(ShadowMunger munger) {
- this.mungers.add(munger);
- }
-
- public final void implement() {
- sortMungers();
- if (mungers == null) return;
- prepareForMungers();
- implementMungers();
- }
-
- private void sortMungers() {
- List sorted = PartialOrder.sort(mungers);
- if (sorted == null) {
- // this means that we have circular dependencies
- for (Iterator i = mungers.iterator(); i.hasNext(); ) {
- ShadowMunger m = (ShadowMunger)i.next();
- getIWorld().getMessageHandler().handleMessage(
- MessageUtil.error("circular dependency at " + this, m.getSourceLocation()));
- }
- }
- mungers = sorted;
- }
-
- /** Prepare the shadow for implementation. After this is done, the shadow
- * should be in such a position that each munger simply needs to be implemented.
- */
- protected void prepareForMungers() {
- throw new RuntimeException("Generic shadows cannot be prepared");
- }
-
- /** Actually implement the (non-empty) mungers associated with this shadow */
- private void implementMungers() {
- World world = getIWorld();
- for (Iterator iter = mungers.iterator(); iter.hasNext();) {
- ShadowMunger munger = (ShadowMunger) iter.next();
- munger.implementOn(this);
- if (world.getModel() != null) {
- System.err.println("munger: " + munger + " on " + this);
- AsmAdaptor.noteMunger(world.getModel(), this, munger);
- }
- }
- }
-
- public String makeReflectiveFactoryString() {
- return null; //XXX
- }
-
- public abstract SourceLocation getSourceLocation();
-
- // ---- utility
-
- public String toString() {
- return getKind() + "(" + getSignature() + ")"; // + getSourceLines();
- }
-
-
-
-
-
- // ---- type access methods
-
-
-
- }
|