-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from vaa25/1.4.1
1.4.1 java.beans.ConstructorProperties and @ExcelConstructor
- Loading branch information
Showing
21 changed files
with
608 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.poiji.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Marks constructor what should be used by Poiji. | ||
* | ||
* Created by vaa25 on 16.03.24. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.CONSTRUCTOR) | ||
@Documented | ||
public @interface ExcelConstructor { | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.poiji.util; | ||
|
||
import com.poiji.annotation.ExcelConstructor; | ||
import com.poiji.exception.PoijiInstantiationException; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class ConstructorSelector { | ||
|
||
private static final Map<Class<?>, Constructor<?>> fieldDefaultsMapping = new ConcurrentHashMap<>(); | ||
|
||
public static Constructor<?> selectConstructor(Class<?> type) { | ||
return fieldDefaultsMapping.computeIfAbsent(type, ignored -> setAccessible(defineConstructor(type))); | ||
} | ||
|
||
private static Constructor<?> setAccessible(Constructor<?> constructor) { | ||
if (!constructor.isAccessible()) { | ||
constructor.setAccessible(true); | ||
} | ||
return constructor; | ||
} | ||
|
||
private static Constructor<?> defineConstructor(Class<?> type) { | ||
final Constructor<?>[] constructors = type.getDeclaredConstructors(); | ||
if (constructors.length > 1) { | ||
final Constructor<?> constructor = getMarkedConstructor(type, constructors); | ||
if (constructor == null) { | ||
final String annotation = ExcelConstructor.class.getSimpleName(); | ||
final String message = String.format("Several constructors were found in %s. Mark one of it with @%s please.", type.getName(), annotation); | ||
throw new PoijiInstantiationException(message, null); | ||
} | ||
return constructor; | ||
} | ||
return constructors[0]; | ||
} | ||
|
||
private static Constructor<?> getMarkedConstructor(Class<?> type, Constructor<?>[] constructors) { | ||
Constructor<?> result = null; | ||
for (Constructor<?> constructor : constructors) { | ||
if (constructor.isAnnotationPresent(ExcelConstructor.class)) { | ||
if (result != null) { | ||
final String annotation = ExcelConstructor.class.getSimpleName(); | ||
final String message = String.format("Several constructors are marked with @%s in %s. Mark only one of it please.", annotation, type.getName()); | ||
throw new PoijiInstantiationException(message, null); | ||
} | ||
result = constructor; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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.