PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 142 lines | 117 code | 10 blank | 15 comment | 47 complexity | beea48baa6ca17944d5bf420fdf610aa MD5 | raw file
  1/*
  2 * Copyright 2008 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;
 17
 18import java.util.ArrayList;
 19import java.util.List;
 20
 21public class JavaType extends Type {
 22
 23  public static final Type VOID = new Type("void", "V");
 24  public static final Type BYTE = new Type("byte", "B");
 25  public static final Type SHORT = new Type("short", "S");
 26  public static final Type INT = new Type("int", "I");
 27  public static final Type BOOLEAN = new Type("boolean", "Z");
 28  public static final Type CHAR = new Type("char", "C");
 29  public static final Type LONG = new Type("long", "J");
 30  public static final Type DOUBLE = new Type("double", "D");
 31  public static final Type FLOAT = new Type("float", "F");
 32  public static final Type OBJECT = fromClass(Object.class);
 33
 34  private JavaType(String name, String code) {
 35    super(name, code);
 36  }
 37
 38  public static boolean isDoubleSlot(Type type) {
 39    return type == DOUBLE || type == LONG;
 40  }
 41
 42  public static Type fromClass(Class<?> clazz) {
 43    if (clazz == Byte.class) {
 44      return BYTE;
 45    } else if (clazz == Short.class) {
 46      return SHORT;
 47    } else if (clazz == Integer.class) {
 48      return INT;
 49    } else if (clazz == Boolean.class) {
 50      return BOOLEAN;
 51    } else if (clazz == Character.class) {
 52      return CHAR;
 53    } else if (clazz == Long.class) {
 54      return LONG;
 55    } else if (clazz == Double.class) {
 56      return DOUBLE;
 57    } else if (clazz == Float.class) {
 58      return FLOAT;
 59    } else if (clazz.isArray()) {
 60      return fromClass(clazz.getComponentType()).toArray();
 61    } else {
 62      return fromJava(clazz.getName());
 63    }
 64  }
 65
 66  public static Type fromJava(String clazz) {
 67    if (clazz.contains(";")) {
 68      throw new IllegalArgumentException();
 69    }
 70    clazz = clazz.replace('/', '.');
 71    String jvm = "L" + clazz.replace('.', '/') + ";";
 72    return new Type(0, clazz, jvm);
 73  }
 74
 75  public static Type fromDesc(String desc) {
 76    char ch = desc.charAt(0);
 77    switch (ch) {
 78    case 'V':
 79      return VOID;
 80    case 'B':
 81      return BYTE;
 82    case 'S':
 83      return SHORT;
 84    case 'Z':
 85      return BOOLEAN;
 86    case 'C':
 87      return CHAR;
 88    case 'I':
 89      return INT;
 90    case 'J':
 91      return LONG;
 92    case 'F':
 93      return FLOAT;
 94    case 'D':
 95      return DOUBLE;
 96    case '[':
 97      return fromDesc(desc.substring(1)).toArray();
 98    case 'L':
 99      String javaClass = desc.substring(1, desc.length() - 1);
100      javaClass = javaClass.replace('/', '.').replace('$', '.');
101      return new Type(0, javaClass, desc);
102    default:
103      throw new IllegalArgumentException(desc);
104    }
105  }
106
107  public static Type fromDescReturn(String desc) {
108    return fromDesc(desc.split("\\(.*\\)")[1]);
109  }
110
111  public static List<Type> fromDescParameters(String desc) {
112    ArrayList<Type> list = new ArrayList<Type>();
113    int start = 1;
114    int end = 1;
115    while (end < desc.length()) {
116      char ch = desc.charAt(end);
117      if (ch == ')') {
118        if (start != end) {
119          list.add(fromDesc(desc.substring(start, end)));
120        }
121        return list;
122      } else if (ch == 'L') {
123        while (desc.charAt(end) != ';') {
124          end++;
125          if (end == desc.length()) {
126            throw new IllegalArgumentException("Did you forget closing ';'");
127          }
128        }
129        end++;
130        list.add(fromDesc(desc.substring(start, end)));
131        start = end;
132      } else if (ch == '[') {
133        end++;
134      } else {
135        end++;
136        list.add(fromDesc(desc.substring(start, end)));
137        start = end;
138      }
139    }
140    throw new IllegalArgumentException("Did you forget closing ')'");
141  }
142}