KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > Interaction


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.lang.reflect.AccessibleObject JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import org.aopalliance.intercept.ConstructorInvocation;
26 import org.aopalliance.intercept.Interceptor;
27 import org.aopalliance.intercept.Invocation;
28 import org.aopalliance.intercept.MethodInvocation;
29 import org.apache.log4j.Logger;
30 import org.objectweb.jac.core.rtti.AbstractMethodItem;
31 import org.objectweb.jac.core.rtti.ClassItem;
32 import org.objectweb.jac.core.rtti.ClassRepository;
33 import org.objectweb.jac.core.rtti.ConstructorItem;
34 import org.objectweb.jac.core.rtti.MethodItem;
35 import org.objectweb.jac.util.ExtArrays;
36 import org.objectweb.jac.util.Strings;
37
38
39 public class Interaction implements MethodInvocation, ConstructorInvocation {
40     static final Logger logger = Logger.getLogger("interaction");
41
42     public final Wrappee wrappee;
43     public final AbstractMethodItem method;
44     public final Object JavaDoc[] args;
45     public int rank;
46     public String JavaDoc cur_AC;
47     public Interceptor[] wrappingChain;
48
49     public Interaction(WrappingChain wrappingChain, Wrappee wrappee,
50                        AbstractMethodItem method, Object JavaDoc[] args)
51     {
52         logger.debug("new Interaction(wrappee="+wrappee+", method="+method+Strings.hash(method)+
53                      ", wrappingChain="+wrappingChain+
54                      (wrappingChain!=null ? Strings.hash(wrappingChain) : "")+
55                      ")");
56         this.wrappingChain =
57             wrappingChain==null ? ExtArrays.emptyInterceptorArray : wrappingChain.chain;
58         this.wrappee = wrappee;
59         this.method = method;
60         this.args = args;
61         this.rank = 0;
62     }
63
64     public final Object JavaDoc proceed() {
65         rank += 1;
66         return Wrapping.nextWrapper(this);
67     }
68
69     public final Object JavaDoc invoke(Object JavaDoc substance) {
70         return method.invoke(substance,args);
71     }
72
73     public final Class JavaDoc getActualClass() {
74         if (wrappee!=null)
75             return wrappee.getClass();
76         else
77             return method.getClassItem().getActualClass();
78     }
79
80     ClassItem cli;
81     public final ClassItem getClassItem() {
82         if (cli==null) {
83             if (wrappee!=null) {
84                 cli = ClassRepository.get().getClass(wrappee);
85             } else {
86                 cli = method.getClassItem();
87             }
88         }
89         return cli;
90     }
91
92     public String JavaDoc toString() {
93         return wrappee+"."+method+(args!=null?("("+Arrays.asList(args)+")"):"")+" rank="+rank;
94     }
95
96
97     // AOP Alliance interfaces implementations
98
public Constructor JavaDoc getConstructor() {
99         return ((ConstructorItem)method).getActualConstructor();
100     }
101     
102
103     public Method JavaDoc getMethod() {
104         return ((MethodItem)method).getActualMethod();
105     }
106
107     // TODO implement these methods?
108
public Object JavaDoc getArgument(int index) {
109         return args[index];
110     }
111
112     public void setArgument(int index, Object JavaDoc argument) {
113         args[index]=argument;
114     }
115
116     public int getArgumentCount() {
117         return args.length;
118     }
119
120     public Object JavaDoc[] getArguments() {
121         return args;
122     }
123
124     public Object JavaDoc getThis() {
125         return wrappee;
126     }
127
128     public AccessibleObject JavaDoc getStaticPart() {
129         if(method instanceof ConstructorItem) {
130             return getConstructor();
131         } else {
132             return getMethod();
133         }
134     }
135 }
136
Popular Tags