1 22 23 24 package com.mchange.v2.codegen; 25 26 import java.lang.reflect.*; 27 import java.io.File ; 28 import java.io.Writer ; 29 import com.mchange.v1.lang.ClassUtils; 30 31 public final class CodegenUtils 32 { 33 public static String getModifierString( int modifiers ) 34 { 35 StringBuffer sb = new StringBuffer (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 ) ) sb.append("interface "); 60 return sb.toString().trim(); 61 } 62 63 public static Class unarrayClass( Class cl ) 64 { 65 Class out = cl; 66 while ( out.isArray() ) 67 out = out.getComponentType(); 68 return out; 69 } 70 71 public static boolean inSamePackage(String cn1, String cn2) 72 { 73 int pkgdot = cn1.lastIndexOf('.'); 74 int pkgdot2 = cn2.lastIndexOf('.'); 75 76 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 93 public static String fqcnLastElement(String fqcn) 94 { return ClassUtils.fqcnLastElement( fqcn ); } 95 96 public static String methodSignature( Method m ) 97 { return methodSignature( m, null ); } 98 99 public static String methodSignature( Method m, String [] argNames ) 100 { return methodSignature( Modifier.PUBLIC, m, argNames ); } 101 102 public static String methodSignature( int modifiers, Method m, String [] argNames ) 103 { 104 StringBuffer sb = new StringBuffer (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 [] 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 [] 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 methodCall( Method m ) 136 { return methodCall( m, null ); } 137 138 public static String methodCall( Method m, String [] argNames ) 139 { 140 StringBuffer sb = new StringBuffer (256); 141 sb.append( m.getName() ); 142 sb.append('('); 143 Class [] 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 generatedArgumentName( int index ) 155 { return String.valueOf((char) ('a' + index)); } 156 157 public static String simpleClassName( Class cl ) 158 { return ClassUtils.simpleClassName( cl ); } 159 160 public static IndentedWriter toIndentedWriter( Writer w ) 161 { return (w instanceof IndentedWriter ? (IndentedWriter) w : new IndentedWriter(w)); } 162 163 public static String packageNameToFileSystemDirPath(String packageName) 164 { 165 StringBuffer sb = new StringBuffer ( 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 |