package org.k13n.swtstubber.codegen;

import org.objectweb.asm.Type;

public class CodeAssist {

  public static String getJavaType(String type) {
    return getJavaType(Type.getType(type));
  }

  public static String getJavaType(Type type) {
    switch (type.getSort()) {
      case Type.BOOLEAN: return "boolean";
      case Type.BYTE:    return "byte";
      case Type.CHAR:    return "char";
      case Type.DOUBLE:  return "double";
      case Type.FLOAT:   return "float";
      case Type.INT:     return "int";
      case Type.LONG:    return "long";
      case Type.SHORT:   return "short";
      case Type.VOID:    return "void";
      case Type.ARRAY:   return getArrayJavaType(type);
      case Type.OBJECT:  return getObjectJavaType(type);
    }
    throw new AssertionError("impossible");
  }

  private static String getArrayJavaType(Type type) {
    String descriptor = type.getDescriptor();
    int level = 0;
    while (descriptor.charAt(level) == '[')
      level++;
    String realType = getJavaType(descriptor.substring(level));
    for (int i = 0; i < level; i++)
      realType = realType + "[]";
    return realType;
  }

  private static String getObjectJavaType(Type type) {
    String descriptor = type.getDescriptor();
    descriptor = descriptor.substring(1, descriptor.length() - 1);
    return descriptor.replace('/', '.');
  }

}

@@ -1353,10 +1353,675 @@
 );%0A  %7D%0A%0A
+  public static String neutralValue(String type) %7B%0A    return neutralValue(Type.getType(type));%0A  %7D%0A%0A  public static String neutralValue(Type type) %7B%0A    switch (type.getSort()) %7B%0A      case Type.BOOLEAN: return %22false%22;%0A      case Type.BYTE:    return %220%22;%0A      case Type.CHAR:    return %22'%5Cu0000'%22;%0A      case Type.DOUBLE:  return %220.0d%22;%0A      case Type.FLOAT:   return %220.0f%22;%0A      case Type.INT:     return %220%22;%0A      case Type.LONG:    return %220L%22;%0A      case Type.SHORT:   return %220%22;%0A      case Type.VOID:    return %22%22;%0A      case Type.ARRAY:   return %22null%22;%0A      case Type.OBJECT:  return %22null%22;%0A    %7D%0A    throw new AssertionError(%22impossible%22);%0A  %7D%0A%0A
 %7D%0A

Add a method to CodeAssist to get the neutral value of a type