001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen;
024:
025: import java.lang.reflect.*;
026: import java.io.File;
027: import java.io.Writer;
028: import com.mchange.v1.lang.ClassUtils;
029:
030: public final class CodegenUtils {
031: public static String getModifierString(int modifiers) {
032: StringBuffer sb = new StringBuffer(32);
033: if (Modifier.isPublic(modifiers))
034: sb.append("public ");
035: if (Modifier.isProtected(modifiers))
036: sb.append("protected ");
037: if (Modifier.isPrivate(modifiers))
038: sb.append("private ");
039: if (Modifier.isAbstract(modifiers))
040: sb.append("abstract ");
041: if (Modifier.isStatic(modifiers))
042: sb.append("static ");
043: if (Modifier.isFinal(modifiers))
044: sb.append("final ");
045: if (Modifier.isSynchronized(modifiers))
046: sb.append("synchronized ");
047: if (Modifier.isTransient(modifiers))
048: sb.append("transient ");
049: if (Modifier.isVolatile(modifiers))
050: sb.append("volatile ");
051: if (Modifier.isStrict(modifiers))
052: sb.append("strictfp ");
053: if (Modifier.isNative(modifiers))
054: sb.append("native ");
055: if (Modifier.isInterface(modifiers)) //????
056: sb.append("interface ");
057: return sb.toString().trim();
058: }
059:
060: public static Class unarrayClass(Class cl) {
061: Class out = cl;
062: while (out.isArray())
063: out = out.getComponentType();
064: return out;
065: }
066:
067: public static boolean inSamePackage(String cn1, String cn2) {
068: int pkgdot = cn1.lastIndexOf('.');
069: int pkgdot2 = cn2.lastIndexOf('.');
070:
071: //always return true of one class is a primitive or unpackages
072: if (pkgdot < 0 || pkgdot2 < 0)
073: return true;
074: if (cn1.substring(0, pkgdot).equals(cn1.substring(0, pkgdot))) {
075: if (cn2.indexOf('.') >= 0)
076: return false;
077: else
078: return true;
079: } else
080: return false;
081: }
082:
083: /**
084: * @return fully qualified class name last element
085: */
086: public static String fqcnLastElement(String fqcn) {
087: return ClassUtils.fqcnLastElement(fqcn);
088: }
089:
090: public static String methodSignature(Method m) {
091: return methodSignature(m, null);
092: }
093:
094: public static String methodSignature(Method m, String[] argNames) {
095: return methodSignature(Modifier.PUBLIC, m, argNames);
096: }
097:
098: public static String methodSignature(int modifiers, Method m,
099: String[] argNames) {
100: StringBuffer sb = new StringBuffer(256);
101: sb.append(getModifierString(modifiers));
102: sb.append(' ');
103: sb.append(ClassUtils.simpleClassName(m.getReturnType()));
104: sb.append(' ');
105: sb.append(m.getName());
106: sb.append('(');
107: Class[] cls = m.getParameterTypes();
108: for (int i = 0, len = cls.length; i < len; ++i) {
109: if (i != 0)
110: sb.append(", ");
111: sb.append(ClassUtils.simpleClassName(cls[i]));
112: sb.append(' ');
113: sb.append(argNames == null ? String
114: .valueOf((char) ('a' + i)) : argNames[i]);
115: }
116: sb.append(')');
117: Class[] excClasses = m.getExceptionTypes();
118: if (excClasses.length > 0) {
119: sb.append(" throws ");
120: for (int i = 0, len = excClasses.length; i < len; ++i) {
121: if (i != 0)
122: sb.append(", ");
123: sb.append(ClassUtils.simpleClassName(excClasses[i]));
124: }
125: }
126: return sb.toString();
127: }
128:
129: public static String methodCall(Method m) {
130: return methodCall(m, null);
131: }
132:
133: public static String methodCall(Method m, String[] argNames) {
134: StringBuffer sb = new StringBuffer(256);
135: sb.append(m.getName());
136: sb.append('(');
137: Class[] cls = m.getParameterTypes();
138: for (int i = 0, len = cls.length; i < len; ++i) {
139: if (i != 0)
140: sb.append(", ");
141: sb.append(argNames == null ? generatedArgumentName(i)
142: : argNames[i]);
143: }
144: sb.append(')');
145: return sb.toString();
146: }
147:
148: public static String generatedArgumentName(int index) {
149: return String.valueOf((char) ('a' + index));
150: }
151:
152: public static String simpleClassName(Class cl) {
153: return ClassUtils.simpleClassName(cl);
154: }
155:
156: public static IndentedWriter toIndentedWriter(Writer w) {
157: return (w instanceof IndentedWriter ? (IndentedWriter) w
158: : new IndentedWriter(w));
159: }
160:
161: public static String packageNameToFileSystemDirPath(
162: String packageName) {
163: StringBuffer sb = new StringBuffer(packageName);
164: for (int i = 0, len = sb.length(); i < len; ++i)
165: if (sb.charAt(i) == '.')
166: sb.setCharAt(i, File.separatorChar);
167: sb.append(File.separatorChar);
168: return sb.toString();
169: }
170:
171: private CodegenUtils() {
172: }
173: }
|