Signature.java

Go to the documentation of this file.
00001 
00004 package jvn;
00005 
00006 import java.lang.annotation.Annotation;
00007 import java.lang.reflect.AccessibleObject;
00008 import java.lang.reflect.Constructor;
00009 import java.lang.reflect.Method;
00010 import java.lang.reflect.Modifier;
00011 import java.lang.reflect.Type;
00012 import java.util.regex.Matcher;
00013 import java.util.regex.Pattern;
00014 
00015 
00026 public class Signature {
00027 
00028         String annotations;
00029         String modifiers;
00030         String name;
00031         String ucfirstName;
00032         String parameters;
00033         //String parameterTypes;
00034         String parameterNames;
00035         String exceptions;
00036         String returnType;
00037         Integer type;
00038         
00039         public Signature() {
00040         }
00041         
00047         public void setValues(AccessibleObject c) {
00048                 
00049                 Type [] parms = null;
00050                 Type [] excepts = null;
00051                 Annotation [] annots = null;
00052                 Annotation [][] parmAnnots = null;
00053                 String [] strings = null;
00054                 
00055                 String dummy = null;
00056                 String dummy2 = null;
00057                 
00058                 
00059                 if (c instanceof Constructor) {
00060                         type = 1; // Constructor;
00061                         parms = ((Constructor)c).getGenericParameterTypes();
00062                         excepts = ((Constructor)c).getGenericExceptionTypes();
00063                         annots = ((Constructor)c).getAnnotations();
00064                         parmAnnots = ((Constructor)c).getParameterAnnotations();
00065                         modifiers = Modifier.toString(((Constructor)c).getModifiers());
00066                         name = ((Constructor)c).getName();
00067                         returnType = "";
00068                 }
00069                 else if (c instanceof Method) {
00070                         Class<?> classReturnType = ((Method)c).getReturnType();
00071                         
00072                         type = 2;
00073                         parms = ((Method)c).getGenericParameterTypes();
00074                         excepts = ((Method)c).getGenericExceptionTypes();
00075                         annots = ((Method)c).getAnnotations();
00076                         parmAnnots = ((Method)c).getParameterAnnotations();
00077                         modifiers = Modifier.toString(((Method)c).getModifiers());
00078                         name = ((Method)c).getName();
00079                         returnType = classReturnType.getName();
00080                 }
00081                 else {
00082                         System.err.println("Wrong object type passed to Signature.setValues !");
00083                         System.exit(1);
00084                 }
00085                 
00086                 
00087                 annotations = new String(""); 
00088                 if (annots != null) {
00089                         for (Annotation a : annots) {
00090                                 String annot = a.toString();
00091                                 annotations = annotations + annot + " ";
00092                                 if (annot.equals("@jvn.JvnRead()")) {
00093                                         type = 3; // @JvnRead method
00094                                 }
00095                                 else if (annot.equals("@jvn.JvnWrite()")) {
00096                                         type = 4; // @JvnWrite method
00097                                 }
00098                         }
00099                 }
00100                 if (annotations.equals("")) {
00101                         annotations = null;
00102                 }
00103                 
00104                 
00105                 if (modifiers.equals("")) {
00106                         modifiers = null;
00107                 }
00108                 
00109 
00110                 if (returnType.equals("")) {
00111                         returnType = null;
00112                 }
00113 
00114         
00115                 if (name.equals("")) {
00116                         System.err.println("Constructor/method name is empty ?!");
00117                         System.exit(1);
00118                 }
00119                 
00120                 // Class name is the last part of the whole class name. Straightforward.
00121                 strings = name.split("\\.");
00122                 name = strings[strings.length - 1];
00123                 // Put the first letter in uppercase. Not so straightforward...
00124                 dummy = name.substring(1);
00125                 dummy2 = name.substring(0,1).toUpperCase();
00126                 ucfirstName = dummy2.concat(dummy);
00127 
00128                 
00129                 // TODO FIXME: handle "public Foo(java.lang.Serializable o)"
00130                 // Don't have an clue on how to solve the problem for now.
00131                 parameters = new String("");
00132                 parameterNames = new String("");
00133                 //parameterTypes = new String("");
00134                 
00135                 if (parms != null) {
00136                         for (int i = 0; i < parms.length; i++) {
00137                                 Annotation [] aArray = (parmAnnots != null)?parmAnnots[i]:null;
00138                                 
00139                                 // Don't add "," if it's the last parameter.
00140                                 String tempParmName = "param" + i + ((i == parms.length - 1)?"":", ");
00141                                 String tempParmType = parms[i].toString();
00142                                 String tempParmAnnot = new String("");
00143                                 
00144                                 if (aArray != null) {
00145                                         for (Annotation a : aArray) {
00146                                                 tempParmAnnot = tempParmAnnot.concat(a.toString()).concat(" ");
00147                                         }
00148                                 }
00149         
00150                                 // Strip the "class " in front of tempParmType;
00151                                 tempParmType = stripTrailingClass(tempParmType);
00152         
00153                                 parameters = parameters.concat(tempParmAnnot).concat(tempParmType).concat(" ").concat(tempParmName);
00154                                 parameterNames = parameterNames.concat(tempParmName);
00155                         }
00156                 }
00157                 
00158                 
00159                 exceptions = new String("");
00160                 if (excepts != null) {
00161                         for (Type e : excepts) {
00162                                 String s = e.toString();
00163                                 s = stripTrailingClass(s);
00164                                 
00165                                 exceptions = exceptions + s + ", ";
00166                         }
00167                 }
00168                 // Add JvnException to list of exceptions.
00169                 exceptions = exceptions + "jvn.JvnException";
00170                 // Shouldn't happen...
00171                 /*if (exceptions.equals("")) {
00172                         exceptions = null;
00173                 }*/
00174                 
00175         }
00176 
00177         // Method extracted by ultra-powerful Eclipse -> Refactor -> Extract Method !
00178         // It even found and correctly replaced a similar block of code.
00185         private String stripTrailingClass(String s) {
00186                 String result = s;
00187                 Pattern p = Pattern.compile("^(class\\s)(([A-Za-z][A-Za-z0-9.]*)+)$");
00188                 Matcher matcher = p.matcher(s);
00189                 if (matcher.matches()) {
00190                         result = matcher.group(2);
00191                 }
00192                 else {
00193                         // Do nothing.
00194                 }
00195                 return result;
00196         }
00197 
00202         public String getAnnotations() {
00203                 //System.out.println("OUAH" + annotations);
00204                 return annotations;
00205         }
00206 
00211         public String getExceptions() {
00212                 return exceptions;
00213         }
00214 
00219         public String getModifiers() {
00220                 return modifiers;
00221         }
00222 
00227         public String getName() {
00228                 return name;
00229         }
00230 
00235         public String getParameters() {
00236                 return parameters;
00237         }
00238         
00243         public int getType() {
00244                 return type;
00245         }
00246 
00251         public String getParameterNames() {
00252                 return parameterNames;
00253         }
00254 
00259         /*public String getParameterTypes() {
00260                 return parameterTypes;
00261         }*/
00262 
00267         public String getReturnType() {
00268                 return returnType;
00269         }
00270 
00275         public String getUcfirstName() {
00276                 return ucfirstName;
00277         }
00278 
00279 }

Generated on Wed Jan 2 10:15:54 2008 for Javanaise by  doxygen 1.5.4