PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/asm/SignatureParser.java

http://testability-explorer.googlecode.com/
Java | 111 lines | 75 code | 21 blank | 15 comment | 0 complexity | c7bdd30b386bd02d3e1086120feba530 MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.asm;
 17
 18import java.util.ArrayList;
 19import java.util.List;
 20
 21import org.objectweb.asm.signature.SignatureReader;
 22import org.objectweb.asm.signature.SignatureVisitor;
 23
 24import com.google.test.metric.JavaType;
 25import com.google.test.metric.Type;
 26
 27public class SignatureParser extends NoopSignatureVisitor {
 28
 29  interface Setter {
 30    void set(Type type);
 31  }
 32
 33  public static class TypeVisitor extends NoopSignatureVisitor {
 34    private final Setter setter;
 35
 36    public TypeVisitor(Setter setter) {
 37      this.setter = setter;
 38    }
 39
 40    @Override
 41    public SignatureVisitor visitArrayType() {
 42      return new TypeVisitor(new Setter() {
 43        public void set(Type type) {
 44          setter.set(type.toArray());
 45        }
 46      });
 47    }
 48
 49    @Override
 50    public void visitBaseType(char descriptor) {
 51      setter.set(JavaType.fromDesc("" + descriptor));
 52    }
 53
 54    @Override
 55    public void visitClassType(String name) {
 56      setter.set(JavaType.fromJava(name));
 57    }
 58
 59  }
 60
 61  private final List<Type> parameters = new ArrayList<Type>();
 62  private Type returnType;
 63
 64  @Override
 65  public SignatureVisitor visitArrayType() {
 66    return new TypeVisitor(new Setter() {
 67      public void set(Type type) {
 68        parameters.add(type.toArray());
 69      }
 70    });
 71  }
 72
 73  @Override
 74  public void visitBaseType(char descriptor) {
 75    parameters.add(JavaType.fromDesc("" + descriptor));
 76  }
 77
 78  @Override
 79  public void visitClassType(String name) {
 80    parameters.add(JavaType.fromJava(name));
 81  }
 82
 83  @Override
 84  public SignatureVisitor visitParameterType() {
 85    return this;
 86  }
 87
 88  @Override
 89  public SignatureVisitor visitReturnType() {
 90    return new TypeVisitor(new Setter() {
 91      public void set(Type type) {
 92        returnType = type;
 93      }
 94    });
 95  }
 96
 97  public List<Type> getParameters() {
 98    return parameters;
 99  }
100
101  public Type getReturnType() {
102    return returnType;
103  }
104
105  public static SignatureParser parse(String signature) {
106    SignatureParser parser = new SignatureParser();
107    new SignatureReader(signature).accept(parser);
108    return parser;
109  }
110
111}