KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > CodegenUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen;
25
26 import java.lang.reflect.*;
27 import java.io.File JavaDoc;
28 import java.io.Writer JavaDoc;
29 import com.mchange.v1.lang.ClassUtils;
30
31 public final class CodegenUtils
32 {
33     public static String JavaDoc getModifierString( int modifiers )
34     {
35     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32);
36     if ( Modifier.isPublic( modifiers ) )
37         sb.append("public ");
38     if ( Modifier.isProtected( modifiers ) )
39         sb.append("protected ");
40     if ( Modifier.isPrivate( modifiers ) )
41         sb.append("private ");
42     if ( Modifier.isAbstract( modifiers ) )
43         sb.append("abstract ");
44     if ( Modifier.isStatic( modifiers ) )
45         sb.append("static ");
46     if ( Modifier.isFinal( modifiers ) )
47         sb.append("final ");
48     if ( Modifier.isSynchronized( modifiers ) )
49         sb.append("synchronized ");
50     if ( Modifier.isTransient( modifiers ) )
51         sb.append("transient ");
52     if ( Modifier.isVolatile( modifiers ) )
53         sb.append("volatile ");
54     if ( Modifier.isStrict( modifiers ) )
55         sb.append("strictfp ");
56     if ( Modifier.isNative( modifiers ) )
57         sb.append("native ");
58     if ( Modifier.isInterface( modifiers ) ) //????
59
sb.append("interface ");
60     return sb.toString().trim();
61     }
62
63     public static Class JavaDoc unarrayClass( Class JavaDoc cl )
64     {
65     Class JavaDoc out = cl;
66     while ( out.isArray() )
67         out = out.getComponentType();
68     return out;
69     }
70
71     public static boolean inSamePackage(String JavaDoc cn1, String JavaDoc cn2)
72     {
73        int pkgdot = cn1.lastIndexOf('.');
74        int pkgdot2 = cn2.lastIndexOf('.');
75
76        //always return true of one class is a primitive or unpackages
77
if (pkgdot < 0 || pkgdot2 < 0)
78            return true;
79        if ( cn1.substring(0, pkgdot).equals(cn1.substring(0, pkgdot)) )
80        {
81           if (cn2.indexOf('.') >= 0)
82             return false;
83           else
84             return true;
85        }
86        else
87          return false;
88     }
89
90     /**
91      * @return fully qualified class name last element
92      */

93     public static String JavaDoc fqcnLastElement(String JavaDoc fqcn)
94     { return ClassUtils.fqcnLastElement( fqcn ); }
95
96     public static String JavaDoc methodSignature( Method m )
97     { return methodSignature( m, null ); }
98
99     public static String JavaDoc methodSignature( Method m, String JavaDoc[] argNames )
100     { return methodSignature( Modifier.PUBLIC, m, argNames ); }
101
102     public static String JavaDoc methodSignature( int modifiers, Method m, String JavaDoc[] argNames )
103     {
104     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(256);
105         sb.append(getModifierString(modifiers));
106     sb.append(' ');
107     sb.append( ClassUtils.simpleClassName( m.getReturnType() ) );
108     sb.append(' ');
109     sb.append( m.getName() );
110     sb.append('(');
111         Class JavaDoc[] cls = m.getParameterTypes();
112         for(int i = 0, len = cls.length; i < len; ++i)
113         {
114            if (i != 0)
115              sb.append(", ");
116            sb.append( ClassUtils.simpleClassName( cls[i] ) );
117        sb.append(' ');
118            sb.append( argNames == null ? String.valueOf((char) ('a' + i)) : argNames[i] );
119         }
120         sb.append(')');
121     Class JavaDoc[] excClasses = m.getExceptionTypes();
122     if (excClasses.length > 0)
123         {
124            sb.append(" throws ");
125            for (int i = 0, len = excClasses.length; i < len; ++i)
126            {
127              if (i != 0)
128                sb.append(", ");
129              sb.append( ClassUtils.simpleClassName( excClasses[i] ) );
130            }
131         }
132         return sb.toString();
133     }
134
135     public static String JavaDoc methodCall( Method m )
136     { return methodCall( m, null ); }
137
138     public static String JavaDoc methodCall( Method m, String JavaDoc[] argNames )
139     {
140        StringBuffer JavaDoc sb = new StringBuffer JavaDoc(256);
141        sb.append( m.getName() );
142        sb.append('(');
143         Class JavaDoc[] cls = m.getParameterTypes();
144         for(int i = 0, len = cls.length; i < len; ++i)
145         {
146            if (i != 0)
147              sb.append(", ");
148            sb.append( argNames == null ? generatedArgumentName( i ) : argNames[i] );
149         }
150         sb.append(')');
151     return sb.toString();
152     }
153
154     public static String JavaDoc generatedArgumentName( int index )
155     { return String.valueOf((char) ('a' + index)); }
156
157     public static String JavaDoc simpleClassName( Class JavaDoc cl )
158     { return ClassUtils.simpleClassName( cl ); }
159
160     public static IndentedWriter toIndentedWriter( Writer JavaDoc w )
161     { return (w instanceof IndentedWriter ? (IndentedWriter) w : new IndentedWriter(w)); }
162
163     public static String JavaDoc packageNameToFileSystemDirPath(String JavaDoc packageName)
164     {
165     StringBuffer JavaDoc sb = new StringBuffer JavaDoc( packageName );
166     for (int i = 0, len = sb.length(); i < len; ++i)
167         if ( sb.charAt(i) == '.' )
168         sb.setCharAt(i, File.separatorChar);
169     sb.append( File.separatorChar );
170     return sb.toString();
171     }
172
173     private CodegenUtils()
174     {}
175 }
176
Popular Tags