public static <T> Constructor<T> findConstructor(Class<T> cls, boolean canFixAccess) throws IllegalArgumentException { try { Constructor<T> ctor = cls.getDeclaredConstructor(); if (canFixAccess) { checkAndFixAccess(ctor); } else { // Has to be public... if (!Modifier.isPublic(ctor.getModifiers())) { throw new IllegalArgumentException("Default constructor for "+cls.getName()+" is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type"); } } return ctor; } catch (NoSuchMethodException e) { ; } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to find default constructor of class "+cls.getName()+", problem: "+e.getMessage()); } return null; }
/** * Method that can be called to try to create an instantiate of * specified type. Instantiation is done using default no-argument * constructor. * * @param canFixAccess Whether it is possible to try to change access * rights of the default constructor (in case it is not publicly * accessible) or not. * * @throws IllegalArgumentException If instantiation fails for any reason; * except for cases where constructor throws an unchecked exception * (which will be passed as is) */ public static <T> T createInstance(Class<T> cls, boolean canFixAccess) throws IllegalArgumentException { Constructor<T> ctor = findConstructor(cls, canFixAccess); if (ctor == null) { throw new IllegalArgumentException("Class "+cls.getName()+" has no default (no arg) constructor"); } try { return ctor.newInstance(); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to instantiate class "+cls.getName()+", problem: "+e.getMessage()); return null; } }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // couple of accepted types... Object value; if (_inputType == null) { value = jp.getText(); } else if (_inputType == Integer.class) { value = Integer.valueOf(jp.getValueAsInt()); } else if (_inputType == Long.class) { value = Long.valueOf(jp.getValueAsLong()); } else { throw ctxt.mappingException(_enumClass); } try { return _factory.invoke(_enumClass, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
@Override public void deserializeAndSet(JsonParser jp, DeserializationContext ctxt, Object bean) throws IOException, JsonProcessingException { // mostly copied from super class impl: JsonToken t = jp.getCurrentToken(); Object value; if (t == JsonToken.VALUE_NULL) { value = (_nullProvider == null) ? null : _nullProvider.nullValue(ctxt); } else if (_valueTypeDeserializer != null) { value = _valueDeserializer.deserializeWithType(jp, ctxt, _valueTypeDeserializer); } else { // the usual case try { value = _creator.newInstance(bean); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to instantiate class "+_creator.getDeclaringClass().getName()+", problem: "+e.getMessage()); value = null; } _valueDeserializer.deserialize(jp, ctxt, value); } set(bean, value); }
@Override public Object _parse(String key, DeserializationContext ctxt) throws JsonMappingException { if (_factory != null) { try { return _factory.call1(key); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } } Enum<?> e = _resolver.findEnum(key); if (e == null) { throw ctxt.weirdKeyException(_keyClass, key, "not one of values for Enum class"); } return e; } }
public static <T> Constructor<T> findConstructor(Class<T> cls, boolean canFixAccess) throws IllegalArgumentException { try { Constructor<T> ctor = cls.getDeclaredConstructor(); if (canFixAccess) { checkAndFixAccess(ctor); } else { // Has to be public... if (!Modifier.isPublic(ctor.getModifiers())) { throw new IllegalArgumentException("Default constructor for "+cls.getName()+" is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type"); } } return ctor; } catch (NoSuchMethodException e) { ; } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to find default constructor of class "+cls.getName()+", problem: "+e.getMessage()); } return null; }
/** * Method that can be called to try to create an instantiate of * specified type. Instantiation is done using default no-argument * constructor. * * @param canFixAccess Whether it is possible to try to change access * rights of the default constructor (in case it is not publicly * accessible) or not. * * @throws IllegalArgumentException If instantiation fails for any reason; * except for cases where constructor throws an unchecked exception * (which will be passed as is) */ public static <T> T createInstance(Class<T> cls, boolean canFixAccess) throws IllegalArgumentException { Constructor<T> ctor = findConstructor(cls, canFixAccess); if (ctor == null) { throw new IllegalArgumentException("Class "+cls.getName()+" has no default (no arg) constructor"); } try { return ctor.newInstance(); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to instantiate class "+cls.getName()+", problem: "+e.getMessage()); return null; } }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // couple of accepted types... Object value; if (_inputType == null) { value = jp.getText(); } else if (_inputType == Integer.class) { value = Integer.valueOf(jp.getValueAsInt()); } else if (_inputType == Long.class) { value = Long.valueOf(jp.getValueAsLong()); } else { throw ctxt.mappingException(_enumClass); } try { return _factory.invoke(_enumClass, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
@Override public void deserializeAndSet(JsonParser jp, DeserializationContext ctxt, Object bean) throws IOException, JsonProcessingException { // mostly copied from super class impl: JsonToken t = jp.getCurrentToken(); Object value; if (t == JsonToken.VALUE_NULL) { value = (_nullProvider == null) ? null : _nullProvider.nullValue(ctxt); } else if (_valueTypeDeserializer != null) { value = _valueDeserializer.deserializeWithType(jp, ctxt, _valueTypeDeserializer); } else { // the usual case try { value = _creator.newInstance(bean); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e, "Failed to instantiate class "+_creator.getDeclaringClass().getName()+", problem: "+e.getMessage()); value = null; } _valueDeserializer.deserialize(jp, ctxt, value); } set(bean, value); }
@Override public Object _parse(String key, DeserializationContext ctxt) throws JsonMappingException { if (_factory != null) { try { return _factory.call1(key); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } } Enum<?> e = _resolver.findEnum(key); if (e == null) { throw ctxt.weirdKeyException(_keyClass, key, "not one of values for Enum class"); } return e; } }
/** * Method that is called to instantiate Object of type this deserializer * produces, using the default (no-argument) constructor. * * @return Instance of type this deserializer handles * * @since 1.7 */ protected Object constructDefaultInstance() { try { return _defaultConstructor.newInstance(); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); return null; // never gets here } }
public Object construct(String value) { try { if (_ctor != null) { return _ctor.newInstance(value); } if (_factoryMethod != null) { return _factoryMethod.invoke(_valueClass, value); } } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
public Object construct(String value) { try { if (_ctor != null) { return _ctor.newInstance(value); } if (_factoryMethod != null) { return _factoryMethod.invoke(_valueClass, value); } } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object value = _deserializer.deserialize(jp, ctxt); try { if (_ctor != null) { return _ctor.newInstance(value); } // static method, 'obj' can be null return _factoryMethod.invoke(null, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); return null; } } }
public Object construct(int value) { // First: "native" int methods work best: try { if (_intCtor != null) { return _intCtor.newInstance(value); } if (_intFactoryMethod != null) { return _intFactoryMethod.invoke(_valueClass, Integer.valueOf(value)); } } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } // but if not, can do widening conversion return construct((long) value); }
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object value = _deserializer.deserialize(jp, ctxt); try { if (_ctor != null) { return _ctor.newInstance(value); } // static method, 'obj' can be null return _factoryMethod.invoke(null, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); return null; } } }
public Object construct(int value) { // First: "native" int methods work best: try { if (_intCtor != null) { return _intCtor.newInstance(value); } if (_intFactoryMethod != null) { return _intFactoryMethod.invoke(_valueClass, Integer.valueOf(value)); } } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } // but if not, can do widening conversion return construct((long) value); }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken curr = jp.getCurrentToken(); // Usually should just get string value: if (curr != JsonToken.VALUE_STRING) { throw ctxt.mappingException(_enumClass); } String value = jp.getText(); try { return _factory.invoke(_enumClass, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken curr = jp.getCurrentToken(); // Usually should just get string value: if (curr != JsonToken.VALUE_STRING) { throw ctxt.mappingException(_enumClass); } String value = jp.getText(); try { return _factory.invoke(_enumClass, value); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } return null; } }
@Override public Object _parse(String key, DeserializationContext ctxt) throws JsonMappingException { if (_factory != null) { try { return _factory.call1(key); } catch (Exception e) { ClassUtil.unwrapAndThrowAsIAE(e); } } Enum<?> e = _resolver.findEnum(key); if (e == null) { throw ctxt.weirdKeyException(_keyClass, key, "not one of values for Enum class"); } return e; } }