Ejemplo n.º 1
0
  public void test_getType() throws Exception {
    Assert.assertEquals(Type.VOID_TYPE, Type.getType(ASMUtils.getDesc(void.class)));

    Assert.assertEquals(Type.BOOLEAN_TYPE, Type.getType(ASMUtils.getDesc(boolean.class)));

    Assert.assertEquals(Type.CHAR_TYPE, Type.getType(ASMUtils.getDesc(char.class)));

    Assert.assertEquals(Type.BYTE_TYPE, Type.getType(ASMUtils.getDesc(byte.class)));

    Assert.assertEquals(Type.SHORT_TYPE, Type.getType(ASMUtils.getDesc(short.class)));

    Assert.assertEquals(Type.INT_TYPE, Type.getType(ASMUtils.getDesc(int.class)));

    Assert.assertEquals(Type.LONG_TYPE, Type.getType(ASMUtils.getDesc(long.class)));

    Assert.assertEquals(Type.FLOAT_TYPE, Type.getType(ASMUtils.getDesc(float.class)));

    Assert.assertEquals(Type.DOUBLE_TYPE, Type.getType(ASMUtils.getDesc(double.class)));

    Assert.assertEquals("[D", Type.getType(ASMUtils.getDesc(double[].class)).getInternalName());
    Assert.assertEquals("[[D", Type.getType(ASMUtils.getDesc(double[][].class)).getInternalName());
    Assert.assertEquals(
        "[Ljava/lang/Double;", Type.getType(ASMUtils.getDesc(Double[].class)).getInternalName());
  }
Ejemplo n.º 2
0
/**
 * circular references detect
 *
 * @author wenshao<*****@*****.**>
 */
public class SerializeConfig extends IdentityHashMap<Type, ObjectSerializer> {

  private static final SerializeConfig globalInstance = new SerializeConfig();

  private boolean asm = !ASMUtils.isAndroid();;

  private final ASMSerializerFactory asmFactory = new ASMSerializerFactory();

  public final ObjectSerializer createASMSerializer(Class<?> clazz) throws Exception {
    return asmFactory.createJavaBeanSerializer(clazz);
  }

  public ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
    if (!Modifier.isPublic(clazz.getModifiers())) {
      return new JavaBeanSerializer(clazz);
    }

    boolean asm = this.asm;

    if (asm && ASMClassLoader.isExternalClass(clazz)
        || clazz == Serializable.class
        || clazz == Object.class) {
      asm = false;
    }
    if (asm) {
      try {
        return createASMSerializer(clazz);
      } catch (Throwable e) {
        throw new JSONException("create asm serilizer error, class " + clazz, e);
      }
    }

    return new JavaBeanSerializer(clazz);
  }

  public boolean isAsmEnable() {
    return asm;
  }

  public void setAsmEnable(boolean asmEnable) {
    this.asm = asmEnable;
  }

  public static final SerializeConfig getGlobalInstance() {
    return globalInstance;
  }

  public SerializeConfig() {
    this(DEFAULT_TABLE_SIZE);
  }

  public SerializeConfig(int tableSize) {
    super(tableSize);

    put(Boolean.class, BooleanSerializer.instance);
    put(Character.class, CharacterSerializer.instance);
    put(Byte.class, ByteSerializer.instance);
    put(Short.class, ShortSerializer.instance);
    put(Integer.class, IntegerSerializer.instance);
    put(Long.class, LongSerializer.instance);
    put(Float.class, FloatSerializer.instance);
    put(Double.class, DoubleSerializer.instance);
    put(BigDecimal.class, BigDecimalSerializer.instance);
    put(BigInteger.class, BigIntegerSerializer.instance);
    put(String.class, StringSerializer.instance);
    put(byte[].class, ByteArraySerializer.instance);
    put(short[].class, ShortArraySerializer.instance);
    put(int[].class, IntArraySerializer.instance);
    put(long[].class, LongArraySerializer.instance);
    put(float[].class, FloatArraySerializer.instance);
    put(double[].class, DoubleArraySerializer.instance);
    put(boolean[].class, BooleanArraySerializer.instance);
    put(char[].class, CharArraySerializer.instance);
    put(Object[].class, ObjectArraySerializer.instance);
    put(Class.class, ClassSerializer.instance);

    put(SimpleDateFormat.class, DateFormatSerializer.instance);
    put(Locale.class, LocaleSerializer.instance);
    put(TimeZone.class, TimeZoneSerializer.instance);
    put(UUID.class, UUIDSerializer.instance);
    put(InetAddress.class, InetAddressSerializer.instance);
    put(Inet4Address.class, InetAddressSerializer.instance);
    put(Inet6Address.class, InetAddressSerializer.instance);
    put(InetSocketAddress.class, InetSocketAddressSerializer.instance);
    put(File.class, FileSerializer.instance);
    put(URI.class, URISerializer.instance);
    put(URL.class, URLSerializer.instance);
    put(Appendable.class, AppendableSerializer.instance);
    put(StringBuffer.class, AppendableSerializer.instance);
    put(StringBuilder.class, AppendableSerializer.instance);
    put(Pattern.class, PatternSerializer.instance);
    put(Charset.class, CharsetSerializer.instance);

    // atomic
    put(AtomicBoolean.class, AtomicBooleanSerializer.instance);
    put(AtomicInteger.class, AtomicIntegerSerializer.instance);
    put(AtomicLong.class, AtomicLongSerializer.instance);
    put(AtomicReference.class, AtomicReferenceSerializer.instance);
    put(AtomicIntegerArray.class, AtomicIntegerArraySerializer.instance);
    put(AtomicLongArray.class, AtomicLongArraySerializer.instance);
  }
}