forked from FasterXML/jackson-jr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FasterXML#25: add support for single-int Constructors (Fast…
- Loading branch information
1 parent
28ae21b
commit 380913d
Showing
9 changed files
with
191 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanConstructors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.fasterxml.jackson.jr.ob.impl; | ||
|
||
import java.lang.reflect.Constructor; | ||
|
||
/** | ||
* Container class added to encapsulate details of collection and use of | ||
* Constructors for User-defined (non-JDK) types (aka "Beans"). | ||
* | ||
* @since 2.17 | ||
*/ | ||
public class BeanConstructors | ||
{ | ||
protected final Class<?> _valueType; | ||
|
||
protected Constructor<?> _noArgsCtor; | ||
|
||
protected Constructor<?> _intCtor; | ||
protected Constructor<?> _longCtor; | ||
protected Constructor<?> _stringCtor; | ||
|
||
public BeanConstructors(Class<?> valueType) { | ||
_valueType = valueType; | ||
} | ||
|
||
public BeanConstructors addNoArgsConstructor(Constructor<?> ctor) { | ||
_noArgsCtor = ctor; | ||
return this; | ||
} | ||
|
||
public BeanConstructors addIntConstructor(Constructor<?> ctor) { | ||
_intCtor = ctor; | ||
return this; | ||
} | ||
|
||
public BeanConstructors addLongConstructor(Constructor<?> ctor) { | ||
_longCtor = ctor; | ||
return this; | ||
} | ||
|
||
public BeanConstructors addStringConstructor(Constructor<?> ctor) { | ||
_stringCtor = ctor; | ||
return this; | ||
} | ||
|
||
public void forceAccess() { | ||
if (_noArgsCtor != null) { | ||
_noArgsCtor.setAccessible(true); | ||
} | ||
if (_intCtor != null) { | ||
_intCtor.setAccessible(true); | ||
} | ||
if (_longCtor != null) { | ||
_longCtor.setAccessible(true); | ||
} | ||
if (_stringCtor != null) { | ||
_stringCtor.setAccessible(true); | ||
} | ||
} | ||
|
||
protected Object create() throws Exception { | ||
if (_noArgsCtor == null) { | ||
throw new IllegalStateException("Class "+_valueType.getName()+" does not have default constructor to use"); | ||
} | ||
return _noArgsCtor.newInstance((Object[]) null); | ||
} | ||
|
||
protected Object create(String str) throws Exception { | ||
if (_stringCtor == null) { | ||
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-String constructor to use"); | ||
} | ||
return _stringCtor.newInstance(str); | ||
} | ||
|
||
protected Object create(long l) throws Exception { | ||
// 23-Feb-2024, tatu: As per [jackson-jr#25] can have `int`-constructors too. | ||
// For now no need to try to optimize separately | ||
if (_longCtor != null) { | ||
return _longCtor.newInstance(l); | ||
} | ||
if (_intCtor != null) { | ||
// TODO: should this do bounds checks? | ||
return _intCtor.newInstance((int) l); | ||
} | ||
throw new IllegalStateException("Class "+_valueType.getName() | ||
+" does not have single-long or single-int constructor to use"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.