KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > asm > associations > Advice


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25
26 package org.aspectj.asm.associations;
27
28 import java.util.*;
29 import org.aspectj.compiler.base.*;
30 import org.aspectj.compiler.base.ast.*;
31 import org.aspectj.compiler.base.cst.*;
32 import org.aspectj.compiler.crosscuts.*;
33 import org.aspectj.compiler.crosscuts.ast.*;
34 import org.aspectj.asm.*;
35 import org.aspectj.asm.internal.*;
36
37 /**
38  * @author Mik Kersten
39  */

40 public class Advice implements Association {
41     
42     public static final String JavaDoc NAME = "Advice";
43     public static final Relation METHOD_RELATION = new Relation("affects methods", "method affected by", NAME, true, false);
44     public static final Relation METHOD_CALL_SITE_RELATION = new Relation("affects method call sites", "method call site affected by", NAME, true, false);
45     public static final Relation CONSTRUCTOR_RELATION = new Relation("affects constructors", "constructor affected by", NAME, true, false);
46     public static final Relation CONSTRUCTOR_CALL_SITE_RELATION = new Relation("affects constructions", "construction affected by", NAME, true, false);
47     public static final Relation HANDLER_RELATION = new Relation("affects handlers", "handler affected by", NAME, true, false);
48     public static final Relation INITIALIZER_RELATION = new Relation("affects initializers", "initializer affected by", NAME, true, false);
49     public static final Relation FIELD_ACCESS_RELATION = new Relation("affects field access", "field access affected by", NAME, true, false);
50     public static final Relation INTRODUCTION_RELATION = new Relation("affects introduction", "introduction affected by", NAME, true, false);
51
52     private List relations = new ArrayList();
53
54     public Advice() {
55         relations.add(METHOD_RELATION);
56         relations.add(METHOD_CALL_SITE_RELATION);
57         relations.add(CONSTRUCTOR_RELATION);
58         relations.add(CONSTRUCTOR_CALL_SITE_RELATION);
59         relations.add(HANDLER_RELATION);
60         relations.add(INITIALIZER_RELATION);
61         relations.add(FIELD_ACCESS_RELATION);
62         relations.add(INTRODUCTION_RELATION);
63     }
64
65     public List getRelations() {
66         return relations;
67     }
68
69     public List getRelationNodes(ASTObject astObject) {
70         List relations = new ArrayList();
71         List methods = new ArrayList();
72         List methodCallSites = new ArrayList();
73         List constructors = new ArrayList();
74         List constructorCallSites = new ArrayList();
75         List handlers = new ArrayList();
76         List initializers = new ArrayList();
77         List fieldAccesses = new ArrayList();
78         List introductions = new ArrayList();
79         Set forwardCorrs = StructureModelManager.correspondences.getAffects(astObject);
80         Set backCorrs = StructureModelManager.correspondences.getAffectedBy(astObject);
81
82         if (astObject instanceof AdviceDec) {
83             for (Iterator it = forwardCorrs.iterator(); it.hasNext(); ) {
84                 ASTObject node = (ASTObject)it.next();
85                 LinkNode link = StructureNodeFactory.makeLink(node, false);
86                 if (node instanceof MethodDec) {
87                     if (((MethodDec)node).isSynthetic()) {
88                         ASTObject resolvedNode = resolveSyntheticMethodToIntroduction((MethodDec)node);
89                         introductions.add(StructureNodeFactory.makeLink(resolvedNode, false));
90                     } else {
91                         methods.add(link);
92                     }
93                 } else if (node instanceof CallExpr) {
94                     methodCallSites.add(link);
95                 } else if (node instanceof ConstructorDec) {
96                     constructors.add(link);
97                 } else if (node instanceof NewInstanceExpr) {
98                     constructorCallSites.add(link);
99                 } else if (node instanceof CatchClause) {
100                     handlers.add(link);
101                 } else if (node instanceof InitializerDec) {
102                     initializers.add(link);
103                 } else if (node instanceof FieldDec) {
104                     fieldAccesses.add(link);
105                 } else if (node instanceof BasicAssignExpr || node instanceof FieldAccessExpr) {
106                     fieldAccesses.add(link);
107                 }
108             }
109             if (!methods.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_RELATION.getForwardNavigationName(), methods));
110             if (!methodCallSites.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_CALL_SITE_RELATION.getForwardNavigationName(), methodCallSites));
111             if (!constructors.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_RELATION, CONSTRUCTOR_RELATION.getForwardNavigationName(), constructors));
112             if (!constructorCallSites.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_CALL_SITE_RELATION, CONSTRUCTOR_CALL_SITE_RELATION.getForwardNavigationName(), constructorCallSites));
113             if (!handlers.isEmpty()) relations.add(new RelationNode(HANDLER_RELATION, HANDLER_RELATION.getForwardNavigationName(), handlers));
114             if (!initializers.isEmpty()) relations.add(new RelationNode(INITIALIZER_RELATION, INITIALIZER_RELATION.getForwardNavigationName(), initializers));
115             if (!fieldAccesses.isEmpty()) relations.add(new RelationNode(FIELD_ACCESS_RELATION, FIELD_ACCESS_RELATION.getForwardNavigationName(), fieldAccesses));
116             if (!introductions.isEmpty()) relations.add(new RelationNode(INTRODUCTION_RELATION, INTRODUCTION_RELATION.getForwardNavigationName(), introductions));
117         } else {
118             if (astObject instanceof IntroducedDec) {
119                 Set adviceDecs = resolveAdviceAffectingIntroduction((IntroducedDec)astObject);
120                 if (adviceDecs != null) {
121                     for (Iterator adIt = adviceDecs.iterator(); adIt.hasNext(); ) {
122                         introductions.add(StructureNodeFactory.makeLink((ASTObject)adIt.next(), false));
123                     }
124                 }
125             }
126
127             for (Iterator it = backCorrs.iterator(); it.hasNext(); ) {
128                 ASTObject node = (ASTObject)it.next();
129                 if (node instanceof AdviceDec) {
130                     if (astObject instanceof MethodDec) {
131                         methods.add(StructureNodeFactory.makeLink(node, false));
132                     } else if (astObject instanceof CallExpr) {
133                         methodCallSites.add(StructureNodeFactory.makeLink(node, false));
134                     } else if (astObject instanceof ConstructorDec) {
135                         constructors.add(StructureNodeFactory.makeLink(node, false));
136                     } else if (astObject instanceof NewInstanceExpr) {
137                         constructorCallSites.add(StructureNodeFactory.makeLink(node, false));
138                     } else if (astObject instanceof CatchClause) {
139                         handlers.add(StructureNodeFactory.makeLink(node, false));
140                     } else if (astObject instanceof InitializerDec) {
141                         initializers.add(StructureNodeFactory.makeLink(node, false));
142                     } else if (astObject instanceof FieldDec) {
143                         fieldAccesses.add(StructureNodeFactory.makeLink(node, false));
144                     } else if (astObject instanceof BasicAssignExpr
145                         || astObject instanceof FieldAccessExpr) {
146                         fieldAccesses.add(StructureNodeFactory.makeLink(node, false));
147                     }
148                 }
149             }
150             if (!methods.isEmpty()) relations.add(new RelationNode(METHOD_RELATION, METHOD_RELATION.getBackNavigationName(), methods));
151             if (!methodCallSites.isEmpty()) relations.add(new RelationNode(METHOD_CALL_SITE_RELATION, METHOD_CALL_SITE_RELATION.getBackNavigationName(), methodCallSites));
152             if (!constructors.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_RELATION, CONSTRUCTOR_RELATION.getBackNavigationName(), constructors));
153             if (!constructorCallSites.isEmpty()) relations.add(new RelationNode(CONSTRUCTOR_CALL_SITE_RELATION, CONSTRUCTOR_CALL_SITE_RELATION.getBackNavigationName(), constructorCallSites));
154             if (!handlers.isEmpty()) relations.add(new RelationNode(HANDLER_RELATION, HANDLER_RELATION.getBackNavigationName(), handlers));
155             if (!initializers.isEmpty()) relations.add(new RelationNode(INITIALIZER_RELATION, INITIALIZER_RELATION.getBackNavigationName(), initializers));
156             if (!fieldAccesses.isEmpty()) relations.add(new RelationNode(FIELD_ACCESS_RELATION, FIELD_ACCESS_RELATION.getBackNavigationName(), fieldAccesses));
157             if (!introductions.isEmpty()) relations.add(new RelationNode(INTRODUCTION_RELATION, INTRODUCTION_RELATION.getBackNavigationName(), introductions));
158         }
159         return relations;
160     }
161
162     public String JavaDoc getName() {
163         return NAME;
164     }
165
166     /**
167      * @todo HACK: this search and hacked name-match should be replace by a fix to the correspondeces db
168      */

169     private ASTObject resolveSyntheticMethodToIntroduction(MethodDec node) {
170         Set backCorrs = StructureModelManager.correspondences.getAffectedBy(node.getDeclaringType().getTypeDec());
171         Method method = node.getMethod();
172         String JavaDoc name = method.getDeclaringType().getName() + '.' + method.getName();
173         for (Iterator it = backCorrs.iterator(); it.hasNext(); ) {
174             Object JavaDoc next = it.next();
175             if (next instanceof IntroducedDec) {
176                 IntroducedDec introducedDec = (IntroducedDec)next;
177                 if (name.equals(introducedDec.toShortString())) return introducedDec;
178             }
179         }
180         return node;
181     }
182
183     /**
184      * @todo HACK: this search and hacked name-match should be replace by a fix to the correspondeces db
185      */

186     private Set resolveAdviceAffectingIntroduction(IntroducedDec node) {
187         Set forwardCorrs = StructureModelManager.correspondences.getAffects(node);
188         String JavaDoc name = node.getId();
189         for (Iterator it = forwardCorrs.iterator(); it.hasNext(); ) {
190             Object JavaDoc next = it.next();
191             if (next instanceof TypeDec) {
192                 TypeDec typeDec = (TypeDec)next;
193                 List decs = typeDec.getBody().getList();
194                 for (Iterator it2 = decs.iterator(); it2.hasNext(); ) {
195                     Dec bodyDec = (Dec)it2.next();
196                     if (bodyDec != null && !(bodyDec instanceof InitializerDec)) {
197                         if (bodyDec instanceof MethodDec && bodyDec.isSynthetic() && name.equals(bodyDec.getName())) {
198                             MethodDec methodDec = (MethodDec)bodyDec;
199                             return StructureModelManager.correspondences.getAffectedBy(methodDec);
200                         }
201                     }
202                 }
203             }
204         }
205         return null;
206     }
207 }
208
209
Popular Tags