KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > rtti > ConstructorItem


1 /*
2   Copyright (C) 2001-2004 Renaud Pawlak, Laurent Martelli
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 License
15   along with this program; 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.jac.core.rtti;
19
20
21 import java.lang.NoSuchMethodException JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.util.WrappedThrowableException;
26
27 /**
28  * This class defines a meta item that corresponds to the
29  * <code>java.lang.reflect.Constructor</code> meta element.<p>
30  *
31  * @author Laurent Martelli
32  * @author Renaud Pawlak
33  */

34
35 public class ConstructorItem extends AbstractMethodItem {
36     static Logger logger = Logger.getLogger("rtti.method");
37
38     /**
39      * Transforms a constructor items array into a constructors array
40      * containing the <code>java.lang.reflect</code> constructors
41      * wrapped by the constructor items.<p>
42      *
43      * @param constructorItems the Constructor items
44      * @return the actual Constructors in <code>java.lang.reflect</code> */

45
46     public static Constructor JavaDoc[] toConstructors( ConstructorItem[] constructorItems ) {
47         Constructor JavaDoc[] res = new Constructor JavaDoc[constructorItems.length];
48         for ( int i = 0; i < constructorItems.length; i++ ) {
49             if ( constructorItems[i] == null ) {
50                 res[i] = null;
51             } else {
52                 res[i] = constructorItems[i].getActualConstructor();
53             }
54         }
55         return res;
56     }
57
58    
59     /**
60      * Default contructor to create a new constructor item object.<p>
61      *
62      * @param delegate the <code>java.lang.reflect.Constructor</code> actual
63      * meta item */

64
65     public ConstructorItem(Constructor JavaDoc delegate, ClassItem parent)
66         throws InvalidDelegateException
67     {
68         super(delegate,parent);
69         try {
70             delegate.getDeclaringClass().getDeclaredMethod(
71                 "_org_"+NamingConventions.getShortClassName(delegate.getDeclaringClass()),
72                 delegate.getParameterTypes());
73         } catch(NoSuchMethodException JavaDoc e) {
74             //Log.warning("No _org_ method found for "+this);
75
}
76     }
77
78     /**
79      * Get the constructor represented by this constructor item.<p>
80      *
81      * @return the actual constructor
82      */

83
84     public Constructor JavaDoc getActualConstructor() {
85         return (Constructor JavaDoc) delegate;
86     }
87
88     public String JavaDoc getName() {
89         return NamingConventions.getShortConstructorName(this);
90     }
91
92     public Class JavaDoc getType() {
93         return getParent().getType();
94     }
95
96     /**
97      * Creates a new instance of the class item that is parent of this
98      * constructor item.
99      *
100      * @param params the parameters needed by this constructor (see the
101      * types)
102      * @see #getParameterTypes() */

103
104     public Object JavaDoc newInstance(Object JavaDoc [] params)
105         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
106     {
107         return getActualConstructor().newInstance(params);
108     }
109
110     /**
111      * A nice way to construct a new instance when the constructor does
112      * not take any arguements (it throws an exception if it is not the
113      * case).
114      *
115      * @see #getParameterTypes() */

116
117     public Object JavaDoc newInstance()
118         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
119     {
120         if (getClassItem().isAbstract())
121             throw new InstantiationException JavaDoc("Cannot instantiate abstract class "+getClassItem());
122         return getType().newInstance();
123     }
124
125     public Object JavaDoc invoke(Object JavaDoc substance, Object JavaDoc[] params) {
126         try {
127             return newInstance(params);
128         } catch (Exception JavaDoc e) {
129             logger.info("Failed to invoke "+this,e);
130             throw new WrappedThrowableException(e);
131         }
132     }
133
134     public Class JavaDoc[] getParameterTypes() {
135         return ((Constructor JavaDoc)delegate).getParameterTypes();
136     }
137
138     public String JavaDoc toString() {
139         if (delegate!=null)
140             return getFullName();
141         else
142             return "???";
143     }
144
145     public String JavaDoc getFullName() {
146         String JavaDoc ret = NamingConventions.getShortConstructorName(this) + "(";
147         Class JavaDoc[] pts = getParameterTypes();
148         for ( int i = 0; i < pts.length; i++ ) {
149             ret = ret + NamingConventions.getStandardClassName(pts[i]);
150             if ( i < pts.length - 1 ) ret = ret + ",";
151         }
152         ret = ret + ")";
153         return ret;
154     }
155
156     public final boolean isAdder() {
157         return false;
158     }
159     public final CollectionItem[] getAddedCollections() {
160         return CollectionItem.emptyArray;
161     }
162     public final CollectionItem[] getRemovedCollections() {
163         return CollectionItem.emptyArray;
164     }
165
166     public final boolean isSetter() { return false; }
167     public final boolean isRemover() { return false; }
168     public final boolean isAccessor() { return false; }
169     public final boolean isWriter() { return false; }
170     public final boolean isGetter() { return false; }
171
172     public final boolean isFieldGetter() { return false; }
173     public final boolean isFieldSetter() { return false; }
174
175     public final boolean isReferenceGetter() { return false; }
176     public final boolean isReferenceSetter() { return false; }
177     public final boolean isReferenceAccessor() { return false; }
178
179     public final boolean isCollectionGetter() { return false; }
180     public final boolean isCollectionSetter() { return false; }
181     public final boolean isCollectionAccessor() { return false; }
182
183     public final FieldItem getSetField() {
184         return null;
185     }
186    
187 }// ConstructorItem
188
Popular Tags