Source for gnu.java.lang.reflect.MethodSignatureParser

   1: /* MethodSignatureParser.java
   2:    Copyright (C) 2005
   3:    Free Software Foundation
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: package gnu.java.lang.reflect;
  40: 
  41: import java.lang.reflect.*;
  42: import java.util.ArrayList;
  43: 
  44: public class MethodSignatureParser extends GenericSignatureParser
  45: {
  46:     private TypeVariable[] typeParameters;
  47:     private Type[] argTypes;
  48:     private Type retType;
  49:     private Type[] throwsSigs;
  50: 
  51:     public MethodSignatureParser(Method method, String signature)
  52:     {
  53:         this(method, method.getDeclaringClass().getClassLoader(), signature);
  54:     }
  55: 
  56:     public MethodSignatureParser(Constructor method, String signature)
  57:     {
  58:         this(method, method.getDeclaringClass().getClassLoader(), signature);
  59:     }
  60: 
  61:     private MethodSignatureParser(GenericDeclaration wrapper,
  62:         ClassLoader loader, String signature)
  63:     {
  64:         super(wrapper, loader, signature);
  65: 
  66:         if (peekChar() == '<')
  67:         {
  68:             typeParameters = readFormalTypeParameters();
  69:         }
  70:         else
  71:         {
  72:             typeParameters = new TypeVariable[0];
  73:         }
  74:         consume('(');
  75:         ArrayList<Type> args = new ArrayList<Type>();
  76:         while (peekChar() != ')')
  77:         {
  78:             args.add(readTypeSignature());
  79:         }
  80:         argTypes = new Type[args.size()];
  81:         args.toArray(argTypes);
  82:         consume(')');
  83:         retType = readTypeSignature();
  84:         ArrayList<Type> throwsSigs = new ArrayList<Type>();
  85:         while (peekChar() == '^')
  86:         {
  87:             consume('^');
  88:             if(peekChar() == 'T')
  89:             {
  90:                 throwsSigs.add(readTypeVariableSignature());
  91:             }
  92:             else
  93:             {
  94:                 throwsSigs.add(readClassTypeSignature());
  95:             }
  96:         }
  97:         this.throwsSigs = new Type[throwsSigs.size()];
  98:         throwsSigs.toArray(this.throwsSigs);
  99:         end();
 100:     }
 101: 
 102:     public TypeVariable[] getTypeParameters()
 103:     {
 104:         TypeImpl.resolve(typeParameters);
 105:         return typeParameters;
 106:     }
 107: 
 108:     public Type[] getGenericParameterTypes()
 109:     {
 110:         TypeImpl.resolve(argTypes);
 111:         return argTypes;
 112:     }
 113: 
 114:     public Type getGenericReturnType()
 115:     {
 116:         retType = TypeImpl.resolve(retType);
 117:         return retType;
 118:     }
 119: 
 120:     public Type[] getGenericExceptionTypes()
 121:     {
 122:         TypeImpl.resolve(throwsSigs);
 123:         return throwsSigs;
 124:     }
 125: 
 126:     private Type readTypeSignature()
 127:     {
 128:         switch (peekChar())
 129:         {
 130:             case 'T':
 131:                 return readTypeVariableSignature();
 132:             case 'L':
 133:                 return readClassTypeSignature();
 134:             case '[':
 135:                 return readArrayTypeSignature();
 136:             case 'Z':
 137:                 consume('Z');
 138:                 return boolean.class;
 139:             case 'B':
 140:                 consume('B');
 141:                 return byte.class;
 142:             case 'S':
 143:                 consume('S');
 144:                 return short.class;
 145:             case 'C':
 146:                 consume('C');
 147:                 return char.class;
 148:             case 'I':
 149:                 consume('I');
 150:                 return int.class;
 151:             case 'F':
 152:                 consume('F');
 153:                 return float.class;
 154:             case 'J':
 155:                 consume('J');
 156:                 return long.class;
 157:             case 'D':
 158:                 consume('D');
 159:                 return double.class;
 160:             case 'V':
 161:                 consume('V');
 162:                 return void.class;
 163:             default:
 164:                 throw new GenericSignatureFormatError();
 165:         }
 166:     }
 167: }