Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Bytecode Analysis Framework
   * Copyright (C) 2004, University of Maryland
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 package org.robovm.compiler.util;
 
A simple class to parse method signatures that include generic information.

Modified from edu.umd.cs.findbugs.ba.SignatureParser

RoboVM note: Copied and slightly modified from findbugs

Author(s):
Nat Ayewah
 
 public class GenericSignatureParser {
     private class ParameterSignatureIterator implements Iterator<String> {
         private int index = 1;
 
         public boolean hasNext() {
             return  < .length() && .charAt() != ')' && .charAt() != '^';
         }
 
         public String next() {
             if (!hasNext())
                 throw new NoSuchElementException();
             StringBuilder result = new StringBuilder();
             boolean done;
             do {
                 done = true;
                 char ch = .charAt();
                 switch (ch) {
                 case 'B':
                 case 'C':
                 case 'D':
                 case 'F':
                 case 'I':
                 case 'J':
                 case 'S':
                 case 'Z':
                 case '*'// wildcard
                     result.append(.charAt());
                     ++;
                     break;
 
                 case 'L':
                 case 'T':
                     String tmp = "";
                     int startsemi = ;
                     int leftCount = 0;
                     int i = startsemi + 1;
                     loop: while (true) {
                         char c = .charAt(i);
                         switch (c) {
                         case ';':
                             if (leftCount == 0)
                                 break loop;
                             break;
                         case '<':
                             leftCount++;
                             break;
                         case '>':
                             leftCount--;
                             break;
                         }
                         i++;
 
                     }
                     String foo = .substring(startsemii + 1);
                     result.append(foo);
                      = i + 1;
                     break;
 
                 case '[':
                 case '+':
                 case '-':
                     result.append(.charAt());
                     ++;
                     done = false;
                     break;
                case ')':
                case '^':
                    throw new NoSuchElementException("Should have already thrown NoSuchElementException");
                case 'V':
                default:
                    throw new IllegalStateException("Invalid method signature: '" +  + "' : "
                            + .substring() + " " + result);
                }
            } while (!done);
            return result.toString();
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
    private final String signature;

    
Parses a generic method signature of the form: (argument_signature)return_type_signature

Parameters:
signature the method signature to be parsed
    public GenericSignatureParser(String signature) {
        // XXX not currently handling Type parameters for class, interface or
        // method definitions
        int s = signature.indexOf('(');
        String sig = signature;
        if (s > 0)
            sig = sig.substring(s);
        else if (s < 0 || sig.indexOf(':') >= 0 || sig.startsWith("(V)"))
            throw new IllegalArgumentException("Bad method signature: " + signature);
        this. = sig;
    }

    
Get an Iterator over signatures of the method parameters.

Returns:
Iterator which returns the parameter type signatures in order
        return new ParameterSignatureIterator();
    }
    public String getParameterSignature(int index) {
        Iterator<Stringit = parameterSignatureIterator();
        while (index > 0) {
            it.next();
            index--;
        }
        return it.next();
    }
    
    
Get the method return type signature.

Returns:
the method return type signature
    public String getReturnTypeSignature() {
        int endOfParams = .lastIndexOf(')');
        if (endOfParams < 0)
            throw new IllegalArgumentException("Bad method signature: " + );
        return .substring(endOfParams + 1);
    }

    
Get the number of parameters in the signature.

Returns:
the number of parameters
    public int getNumParameters() {
        int count = 0;
        for (Iterator<Stringi = parameterSignatureIterator(); i.hasNext();) {
            i.next();
            ++count;
        }
        return count;
    }
New to GrepCode? Check out our FAQ X