/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. */
  16. package com.google.test.metric.asm;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.objectweb.asm.signature.SignatureReader;
  20. import org.objectweb.asm.signature.SignatureVisitor;
  21. import com.google.test.metric.JavaType;
  22. import com.google.test.metric.Type;
  23. public class SignatureParser extends NoopSignatureVisitor {
  24. interface Setter {
  25. void set(Type type);
  26. }
  27. public static class TypeVisitor extends NoopSignatureVisitor {
  28. private final Setter setter;
  29. public TypeVisitor(Setter setter) {
  30. this.setter = setter;
  31. }
  32. @Override
  33. public SignatureVisitor visitArrayType() {
  34. return new TypeVisitor(new Setter() {
  35. public void set(Type type) {
  36. setter.set(type.toArray());
  37. }
  38. });
  39. }
  40. @Override
  41. public void visitBaseType(char descriptor) {
  42. setter.set(JavaType.fromDesc("" + descriptor));
  43. }
  44. @Override
  45. public void visitClassType(String name) {
  46. setter.set(JavaType.fromJava(name));
  47. }
  48. }
  49. private final List<Type> parameters = new ArrayList<Type>();
  50. private Type returnType;
  51. @Override
  52. public SignatureVisitor visitArrayType() {
  53. return new TypeVisitor(new Setter() {
  54. public void set(Type type) {
  55. parameters.add(type.toArray());
  56. }
  57. });
  58. }
  59. @Override
  60. public void visitBaseType(char descriptor) {
  61. parameters.add(JavaType.fromDesc("" + descriptor));
  62. }
  63. @Override
  64. public void visitClassType(String name) {
  65. parameters.add(JavaType.fromJava(name));
  66. }
  67. @Override
  68. public SignatureVisitor visitParameterType() {
  69. return this;
  70. }
  71. @Override
  72. public SignatureVisitor visitReturnType() {
  73. return new TypeVisitor(new Setter() {
  74. public void set(Type type) {
  75. returnType = type;
  76. }
  77. });
  78. }
  79. public List<Type> getParameters() {
  80. return parameters;
  81. }
  82. public Type getReturnType() {
  83. return returnType;
  84. }
  85. public static SignatureParser parse(String signature) {
  86. SignatureParser parser = new SignatureParser();
  87. new SignatureReader(signature).accept(parser);
  88. return parser;
  89. }
  90. }