KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > mivisitor > InheritanceVisitor


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * 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 library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.generation.mivisitor;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.objectweb.speedo.api.SpeedoException;
24 import org.objectweb.speedo.api.SpeedoProperties;
25 import org.objectweb.speedo.metadata.SpeedoClass;
26 import org.objectweb.speedo.metadata.SpeedoExtension;
27 import org.objectweb.util.monolog.api.BasicLevel;
28
29 /**
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class InheritanceVisitor
34     extends AbstractMetaInfoVisitor
35     implements SpeedoProperties {
36
37     private final static String JavaDoc DEFAULT_INHERITANCE_MAPPING
38         = INHERITANCE_FILTERED_MAPPING;
39     
40     protected String JavaDoc getLoggerName() {
41         return super.getLoggerName() + ".inheritance";
42     }
43
44     public void visitClass(SpeedoClass sc) throws SpeedoException {
45         if (sc.superClassName == null || sc.superClassName.length() == 0) {
46             sc.superClassName = null;
47             super.visitClass(sc);
48             return;
49         }
50         //Always use the fully qualified name
51
SpeedoClass supersc = sc.getSpeedoClassFromContext(sc.superClassName);
52         sc.superClassName = supersc.getFQName();
53         if (debug) {
54             logger.log(BasicLevel.DEBUG, "class " + sc.getFQName()
55                 + " extends " + sc.superClassName);
56         }
57         SpeedoExtension se = sc.getExtensionByKey(INHERITANCE_MAPPING);
58         if (se == null) {
59             se = new SpeedoExtension(VENDOR_NAME,
60                     INHERITANCE_MAPPING,
61                     DEFAULT_INHERITANCE_MAPPING, sc);
62             sc.addExtension(se);
63         }
64         //Assign identifier information to all familly member
65
List JavaDoc family = new ArrayList JavaDoc();
66         getFamily(sc, family);
67         SpeedoClass ancestor = (SpeedoClass) family.get(0);
68         List JavaDoc extensions = findExtensions(ancestor, new String JavaDoc[] {
69             ID, SQL_SEQ_ALLOCATOR, SQL_SEQ_CACHE, SQL_SEQ_INC, SQL_SEQ_NAME,
70             SQL_SEQ_START});
71         boolean detachable = ancestor.isDetachable;
72         for(int i=1; i<family.size(); i++) {
73             SpeedoClass anAncestor = (SpeedoClass) family.get(i);
74             //assign extensions from ancestor to subclasses
75
anAncestor.identityType = ancestor.identityType;
76             anAncestor.objectidClass = ancestor.objectidClass;
77             assignExtension(extensions, anAncestor);
78             //assign detachable attribute from ancestor to subclasses
79
detachable |= anAncestor.isDetachable;
80             if (!anAncestor.isDetachable && detachable) {
81                 if (debug) {
82                     logger.log(BasicLevel.DEBUG, "\tThe class " + anAncestor.name
83                             + " becomes detachable by inheritance.");
84                 }
85                 anAncestor.isDetachable = true;
86             }
87             //assign datastoreSequence from ancestor to subclasses
88
anAncestor.datastoreSequence = ancestor.datastoreSequence;
89         }
90         super.visitClass(sc);
91     }
92
93     private List JavaDoc findExtensions(SpeedoClass sc, String JavaDoc[] keynames) {
94         ArrayList JavaDoc res = new ArrayList JavaDoc();
95         SpeedoExtension se;
96         for(int i=0; i<keynames.length; i++) {
97             se = sc.getExtensionByKey(keynames[i]);
98             if (se != null) {
99                 res.add(se);
100             }
101         }
102         return res;
103     }
104
105     private void assignExtension(List JavaDoc extensions, SpeedoClass sc) {
106         for(int i=extensions.size()-1; i>=0; i--) {
107             SpeedoExtension se = (SpeedoExtension) extensions.get(i);
108             sc.addExtension(
109                 new SpeedoExtension(se.vendorName, se.key, se.value, sc));
110         }
111     }
112
113     private void getFamily(SpeedoClass sc, List JavaDoc result) {
114         result.add(0, sc);
115         if (sc.superClassName == null || sc.superClassName.length() == 0) {
116             return;
117         }
118         SpeedoClass supersc = sc.getSpeedoClassFromContext(sc.superClassName);
119         getFamily(supersc, result);
120     }
121
122 }
123
Popular Tags