KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > synthetic > reflection > EntryPoint


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: EntryPoint.java,v 1.7 2004/02/17 04:24:21 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.utils.synthetic.reflection;
20
21 import com.sun.org.apache.xml.internal.utils.synthetic.SynthesisException;
22
23 /*
24  * OPEN ISSUES:
25  * Reflection doesn't tell us about deprecation; if we want
26  * that info, MFC advises mousing our way into the class (ugh).
27  * Should we at least model that for synthetics?
28  */

29
30 /**
31  * API/behaviors shared between Constructors and Methods.
32  * They're mostly similar, except for what they proxy and
33  * a few specific calls (name, invoke/getInstance).
34  * @xsl.usage internal
35  */

36 abstract public class EntryPoint implements Member
37 {
38
39   /** Field realep */
40   protected Object JavaDoc realep;
41
42   /** Field declaringclass */
43   private com.sun.org.apache.xml.internal.utils.synthetic.Class declaringclass = null;
44
45   /** Field returntype */
46   protected com.sun.org.apache.xml.internal.utils.synthetic.Class returntype = null;
47
48   /** Field parameternames */
49   private String JavaDoc[] parameternames = new String JavaDoc[0];
50
51   /** Field parametertypes */
52   private com.sun.org.apache.xml.internal.utils.synthetic.Class[] parametertypes =
53     new com.sun.org.apache.xml.internal.utils.synthetic.Class[0];
54
55   /** Field exceptiontypes */
56   private com.sun.org.apache.xml.internal.utils.synthetic.Class[] exceptiontypes =
57     new com.sun.org.apache.xml.internal.utils.synthetic.Class[0];
58   ;
59
60   /** Field modifiers */
61   private int modifiers;
62
63   /** Field name */
64   protected String JavaDoc name = null; // for Methods
65

66   // For synthesis:
67

68   /** Field body */
69   private StringBuffer JavaDoc body = null;
70
71   /** Field language */
72   private String JavaDoc language = null;
73
74   // For reifying:
75

76   /** Field realE, realP */
77   Class JavaDoc[] realE, realP;
78
79   /**
80    * Insert the method's description here.
81    * <p>
82    * Creation date: (12-27-99 2:31:39 PM)
83    * @param realConstructor java.lang.reflect.Constructor
84    *
85    * @param declaringclass
86    */

87   public EntryPoint(com.sun.org.apache.xml.internal.utils.synthetic.Class declaringclass)
88   {
89     this.declaringclass = declaringclass;
90   }
91
92   /**
93    * Nonpublic constructor. Wrap this to appropriate "real" type
94    *
95    * @param ep
96    * @param declaringclass
97    *
98    * @throws IllegalArgumentException
99    */

100   protected EntryPoint(
101           Object JavaDoc ep, com.sun.org.apache.xml.internal.utils.synthetic.Class declaringclass)
102             throws IllegalArgumentException JavaDoc
103   {
104
105     realep = ep;
106     this.declaringclass = declaringclass;
107
108     if (ep instanceof java.lang.reflect.Method JavaDoc)
109     {
110       java.lang.reflect.Method JavaDoc m = (java.lang.reflect.Method JavaDoc) ep;
111
112       if (declaringclass == null)
113       {
114         declaringclass = com.sun.org.apache.xml.internal.utils.synthetic.Class.forClass(
115           m.getDeclaringClass());
116       }
117
118       name = m.getName();
119       modifiers = m.getModifiers();
120       returntype =
121         com.sun.org.apache.xml.internal.utils.synthetic.Class.forClass(m.getReturnType());
122       realP = m.getParameterTypes();
123       realE = m.getExceptionTypes();
124     }
125     else if (ep instanceof java.lang.reflect.Constructor JavaDoc)
126     {
127       java.lang.reflect.Constructor JavaDoc c = (java.lang.reflect.Constructor JavaDoc) ep;
128
129       if (declaringclass == null)
130       {
131         declaringclass = com.sun.org.apache.xml.internal.utils.synthetic.Class.forClass(
132           c.getDeclaringClass());
133       }
134
135       name = declaringclass.getShortName();
136       modifiers = c.getModifiers();
137       returntype = declaringclass;
138       realP = c.getParameterTypes();
139       realE = c.getExceptionTypes();
140     }
141     else
142       throw new IllegalArgumentException JavaDoc();
143   }
144
145   /**
146    * Nonpublic constructor. Wrap this to appropriate "real" type
147    *
148    * @param ep
149    *
150    * @throws IllegalArgumentException
151    */

152   protected EntryPoint(Object JavaDoc ep) throws IllegalArgumentException JavaDoc
153   {
154     this(ep, null);
155   }
156
157   /**
158    * Compares this against the specified
159    * object. Returns true if the objects are the same.
160    * Two EntryPoints are the same if they were
161    * declared by the same class, have the same name
162    * (or are both ctors) and have the same
163    * formal parameter types.
164    *
165    * @param obj
166    *
167    */

168   public boolean equals(Object JavaDoc obj)
169   {
170
171     EntryPoint otherep = null;
172
173     if (obj instanceof EntryPoint)
174       otherep = (EntryPoint) obj;
175     else if (obj instanceof java.lang.reflect.Constructor JavaDoc
176              || obj instanceof java.lang.reflect.Method JavaDoc)
177       otherep = (EntryPoint) obj;
178
179     return (otherep != null && ((this instanceof Constructor && otherep instanceof Constructor) || (this instanceof Method && otherep instanceof Method && this.getName().equals(
180       otherep.getName()))) && otherep.getDeclaringClass().equals(
181         declaringclass) && otherep.getParameterTypes().equals(
182         parametertypes));
183   }
184
185   /**
186    * Returns the Class object representing the class that
187    * declares the constructor represented by this
188    * Constructor object.
189    *
190    */

191   public com.sun.org.apache.xml.internal.utils.synthetic.Class getDeclaringClass()
192   {
193     return declaringclass;
194   }
195
196   /**
197    * Returns the Class object representing the class that
198    * will be returned by this EntryPoint. Needed by the Method
199    * API, but made meaningful for Constructors as well.
200    *
201    */

202   public com.sun.org.apache.xml.internal.utils.synthetic.Class getReturnType()
203   {
204     return returntype;
205   }
206
207   /**
208    * Returns an array of Class objects that represent the
209    * types of the checked exceptions thrown by the
210    * underlying constructor represented by this
211    * Constructor object. Returns an array of length 0 if
212    * the constructor throws no checked exceptions.
213    *
214    */

215   public com.sun.org.apache.xml.internal.utils.synthetic.Class[] getExceptionTypes()
216   {
217
218     if (realep != null && exceptiontypes == null)
219     {
220       exceptiontypes =
221         new com.sun.org.apache.xml.internal.utils.synthetic.Class[realE.length];
222
223       for (int i = 0; i < realE.length; ++i)
224       {
225         exceptiontypes[i] =
226           com.sun.org.apache.xml.internal.utils.synthetic.Class.forClass(realE[i]);
227       }
228
229       realE = null;
230     }
231
232     return exceptiontypes;
233   }
234
235   /**
236    * Method addExceptionType
237    *
238    *
239    * @param exception
240    *
241    * @throws SynthesisException
242    */

243   public void addExceptionType(
244           com.sun.org.apache.xml.internal.utils.synthetic.Class exception)
245             throws SynthesisException
246   {
247
248     if (realep != null)
249       throw new SynthesisException(SynthesisException.REIFIED);
250
251     com.sun.org.apache.xml.internal.utils.synthetic.Class[] e =
252       new com.sun.org.apache.xml.internal.utils.synthetic.Class[exceptiontypes.length + 1];
253
254     System.arraycopy(exceptiontypes, 0, e, 0, exceptiontypes.length);
255
256     e[exceptiontypes.length] = exception;
257     exceptiontypes = e;
258   }
259
260   /**
261    * Returns the Java language modifiers for the
262    * constructor represented by this Constructor object,
263    * as an integer. The Modifier class should be used to
264    * decode the modifiers.
265    *
266    */

267   public int getModifiers()
268   {
269     return modifiers;
270   }
271
272   /**
273    * Member method. C'tor's name is always that of the defining class.
274    * Methods have a "real" name.
275    * Creation date: (12-25-99 1:32:06 PM)
276    * @return java.lang.String
277    */

278   public java.lang.String JavaDoc getName()
279   {
280
281     if (this instanceof Constructor)
282       return declaringclass.getShortName();
283
284     return name;
285   }
286
287   /**
288    * Member method. C'tor's name is always that of the defining class.
289    * Methods have a "real" name.
290    * Creation date: (12-25-99 1:32:06 PM)
291    *
292    * @param name
293    * @return java.lang.String
294    *
295    * @throws SynthesisException
296    */

297   public void setName(String JavaDoc name) throws SynthesisException
298   {
299
300     if (realep != null)
301       throw new SynthesisException(SynthesisException.REIFIED);
302
303     this.name = name;
304   }
305
306   /**
307    * Returns an array of Class objects that represent the
308    * formal parameter types, in declaration order, of the
309    * constructor represented by this Constructor object.
310    * Returns an array of length 0 if the underlying
311    * constructor takes no parameters.
312    *
313    */

314   public com.sun.org.apache.xml.internal.utils.synthetic.Class[] getParameterTypes()
315   {
316
317     if (realep != null && parametertypes == null)
318     {
319       parametertypes =
320         new com.sun.org.apache.xml.internal.utils.synthetic.Class[realP.length];
321
322       for (int i = 0; i < realP.length; ++i)
323       {
324         parametertypes[i] =
325           com.sun.org.apache.xml.internal.utils.synthetic.Class.forClass(realP[i]);
326       }
327
328       realP = null;
329     }
330
331     return parametertypes;
332   }
333
334   /**
335    * Method getParameterNames
336    *
337    *
338    * (getParameterNames) @return
339    */

340   public String JavaDoc[] getParameterNames()
341   {
342     return parameternames;
343   }
344
345   /**
346    * Method addParameter
347    *
348    *
349    * @param type
350    * @param name
351    *
352    * @throws SynthesisException
353    */

354   public void addParameter(
355           com.sun.org.apache.xml.internal.utils.synthetic.Class type, String JavaDoc name)
356             throws SynthesisException
357   {
358
359     if (realep != null)
360       throw new SynthesisException(SynthesisException.REIFIED);
361
362     com.sun.org.apache.xml.internal.utils.synthetic.Class[] types =
363       new com.sun.org.apache.xml.internal.utils.synthetic.Class[parametertypes.length + 1];
364
365     System.arraycopy(parametertypes, 0, types, 0, parametertypes.length);
366
367     types[parametertypes.length] = type;
368     parametertypes = types;
369
370     String JavaDoc[] names = new String JavaDoc[parameternames.length + 1];
371
372     System.arraycopy(parameternames, 0, names, 0, parameternames.length);
373
374     names[parameternames.length] = name;
375     parameternames = names;
376   }
377
378   /**
379    * Returns a hashcode for this Constructor. The
380    * hashcode is the same as the hashcode for the
381    * underlying constructor's declaring class name,
382    * xor'ed (for Methods) with the method name.
383    * (Implemented in the subclasses rather than here.)
384    *
385    */

386   abstract public int hashCode();
387
388   /**
389    * Assert the Class object representing the class that
390    * declares the constructor represented by this
391    * Constructor object.
392    *
393    * @param declaringClass
394    *
395    * @throws SynthesisException
396    */

397   public void setDeclaringClass(
398           com.sun.org.apache.xml.internal.utils.synthetic.Class declaringClass)
399             throws SynthesisException
400   {
401
402     if (realep != null)
403       throw new SynthesisException(SynthesisException.REIFIED);
404
405     this.declaringclass = declaringClass;
406   }
407
408   /**
409    * Should only be accepted before a "real" entrypoint is bound.
410    * Creation date: (12-25-99 1:28:28 PM)
411    * @return int
412    * @param modifiers int
413    *
414    * @throws SynthesisException
415    */

416   public void setModifiers(int modifiers) throws SynthesisException
417   {
418
419     if (realep != null)
420       throw new SynthesisException(SynthesisException.REIFIED);
421
422     this.modifiers = modifiers;
423   }
424
425   /**
426    * Return a string describing this Constructor. The
427    * string is formatted as the constructor access
428    * modifiers, if any, followed by the fully-qualified
429    * name of the declaring class, followed by a
430    * parenthesized, comma-separated list of the
431    * constructor's formal parameter types. For example:
432    * <code>
433    * public java.util.Hashtable(int,float)
434    * </code>
435    * <p>
436    * The only possible modifiers for constructors are
437    * the access modifiers public, protected or
438    * private. Only one of these may appear, or none
439    * if the constructor has default (package) access.
440    * <p>
441    * Methods will also display their checked exceptions.
442    *
443    */

444   public String JavaDoc toString()
445   {
446
447     StringBuffer JavaDoc sb =
448       new StringBuffer JavaDoc(java.lang.reflect.Modifier.toString(getModifiers()));
449
450     if (this instanceof com.sun.org.apache.xml.internal.utils.synthetic.reflection.Method)
451       sb.append(' ').append(getReturnType()).append(
452         getDeclaringClass().getName()).append('.').append(getName());
453     else
454       sb.append(getDeclaringClass().getName());
455
456     sb.append('(');
457
458     com.sun.org.apache.xml.internal.utils.synthetic.Class[] p = getParameterTypes();
459
460     if (p != null && p.length > 0)
461     {
462       sb.append(p[0].getName());
463
464       for (int i = 1; i < p.length; ++i)
465       {
466         sb.append(',').append(p[i].getName());
467       }
468     }
469
470     sb.append(')');
471
472     if (this instanceof com.sun.org.apache.xml.internal.utils.synthetic.reflection.Method)
473     {
474       p = getExceptionTypes();
475
476       if (p != null && p.length > 0)
477       {
478         sb.append(" throws ").append(p[0].getName());
479
480         for (int i = 1; i < p.length; ++i)
481         {
482           sb.append(',').append(p[i].getName());
483         }
484       }
485     }
486
487     return sb.toString();
488   }
489
490   /**
491    * Extension: For synthesis, we need a place to hang a
492    * method body.
493    *
494    * @param language
495    * @param body
496    *
497    * @throws SynthesisException
498    */

499   public void setBody(String JavaDoc language, StringBuffer JavaDoc body)
500           throws SynthesisException
501   {
502
503     if (realep != null)
504       throw new SynthesisException(SynthesisException.REIFIED);
505
506     this.language = language;
507     this.body = body;
508   }
509
510   /**
511    * Extension: For synthesis, we need a place to hang a
512    * method body. Note that this returns a mutable object,
513    * for editing etc. Slightly sloppy first cut.
514    *
515    */

516   public StringBuffer JavaDoc getBody()
517   {
518
519     if (body == null)
520       body = new StringBuffer JavaDoc();
521
522     return body;
523   }
524
525   /**
526    * Extension: For synthesis, we need a place to hang a
527    * method body.
528    *
529    */

530   public String JavaDoc getLanguage()
531   {
532     return language;
533   }
534
535   /**
536    * Generate Java code
537    *
538    * @param basetab
539    *
540    */

541   public String JavaDoc toSource(String JavaDoc basetab)
542   {
543
544     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
545
546     sb.append(basetab).append(
547       java.lang.reflect.Modifier.toString(getModifiers()));
548
549     if (this instanceof com.sun.org.apache.xml.internal.utils.synthetic.reflection.Method)
550     {
551       if (returntype != null)
552         sb.append(" ").append(getReturnType().getJavaName());
553       else
554         sb.append(" void");
555     }
556
557     sb.append(" ").append(getName()).append("(");
558
559     com.sun.org.apache.xml.internal.utils.synthetic.Class[] types = getParameterTypes();
560
561     if (types != null & types.length > 0)
562     {
563       sb.append(types[0].getJavaName());
564
565       if (parameternames != null)
566         sb.append(' ').append(parameternames[0]);
567
568       for (int i = 1; i < types.length; ++i)
569       {
570         sb.append(',').append(types[i].getJavaName());
571
572         if (parameternames != null)
573           sb.append(' ').append(parameternames[i]);
574       }
575     }
576
577     sb.append(')');
578
579     types = getExceptionTypes();
580
581     if (types != null & types.length > 0)
582     {
583       sb.append(" throws ").append(types[0].getJavaName());
584
585       for (int i = 1; i < types.length; ++i)
586       {
587         sb.append(',').append(types[i].getJavaName());
588       }
589     }
590
591     if (body == null)
592       sb.append("; // No method body available\n");
593     else
594     {
595       sb.append("\n" + basetab + "{\n");
596
597       if (language == null || "java".equals(language))
598       {
599         sb.append(basetab + "// ***** Should prettyprint this code...\n");
600         sb.append(basetab + body + "\n");
601       }
602       else
603       {
604         sb.append(basetab + "// ***** Generate BSF invocation!?\n");
605       }
606
607       sb.append(basetab + "}\n");
608     }
609
610     return sb.toString();
611   }
612 }
613
Popular Tags