Skip to content

Commit 61ce677

Browse files
Fixed primitives
Made it so BetterReflection#PRIMITIVES are loaded in the static initialization of the class, removing the need to normally initialize the class at least once before being able to use them Added some primitives to the map (short, byte and void) BetterReflectionClass#getArrayClassIf(boolean), if the boolean passed in is true the class returned will be the same as #getArrayClass(), otherwise it will be the same as #getClasz()
1 parent 031de6c commit 61ce677

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

Java-BetterReflection/src/me/wavelength/betterreflection/BetterReflection.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,33 @@ public class BetterReflection {
2727

2828
private static final Map<Class<?>, BetterReflectionClass> PRIMITIVES = new HashMap<>();
2929

30+
static {
31+
PRIMITIVES.put(Short.class, new BetterReflectionClass(Short.class));
32+
PRIMITIVES.put(Byte.class, new BetterReflectionClass(Byte.class));
33+
PRIMITIVES.put(Double.class, new BetterReflectionClass(Double.class));
34+
PRIMITIVES.put(Integer.class, new BetterReflectionClass(Integer.class));
35+
PRIMITIVES.put(Float.class, new BetterReflectionClass(Float.class));
36+
PRIMITIVES.put(Boolean.class, new BetterReflectionClass(Boolean.class));
37+
PRIMITIVES.put(Long.class, new BetterReflectionClass(Long.class));
38+
PRIMITIVES.put(Void.class, new BetterReflectionClass(Void.class));
39+
40+
PRIMITIVES.put(short.class, new BetterReflectionClass(short.class));
41+
PRIMITIVES.put(byte.class, new BetterReflectionClass(byte.class));
42+
PRIMITIVES.put(double.class, new BetterReflectionClass(double.class));
43+
PRIMITIVES.put(int.class, new BetterReflectionClass(int.class));
44+
PRIMITIVES.put(float.class, new BetterReflectionClass(float.class));
45+
PRIMITIVES.put(boolean.class, new BetterReflectionClass(boolean.class));
46+
PRIMITIVES.put(long.class, new BetterReflectionClass(long.class));
47+
PRIMITIVES.put(void.class, new BetterReflectionClass(void.class));
48+
}
49+
3050
/**
3151
* @since 0.4
3252
*/
3353
public static final Pattern INTEGER_PATTERN = Pattern.compile("-?\\d+");
3454

3555
public BetterReflection() {
3656
this.betterReflectionClasses = Collections.synchronizedList(new ArrayList<>());
37-
38-
PRIMITIVES.put(Double.class, getBetterReflectionClass(Double.class));
39-
PRIMITIVES.put(Integer.class, getBetterReflectionClass(Integer.class));
40-
PRIMITIVES.put(Float.class, getBetterReflectionClass(Float.class));
41-
PRIMITIVES.put(Boolean.class, getBetterReflectionClass(Boolean.class));
42-
PRIMITIVES.put(Long.class, getBetterReflectionClass(Long.class));
43-
PRIMITIVES.put(double.class, getBetterReflectionClass(double.class));
44-
PRIMITIVES.put(int.class, getBetterReflectionClass(int.class));
45-
PRIMITIVES.put(float.class, getBetterReflectionClass(float.class));
46-
PRIMITIVES.put(boolean.class, getBetterReflectionClass(boolean.class));
47-
PRIMITIVES.put(long.class, getBetterReflectionClass(long.class));
4857
}
4958

5059
/**
@@ -101,7 +110,7 @@ public String call() throws Exception {
101110
int len = 0;
102111
while ((len = in.read(buf)) != -1)
103112
baos.write(buf, 0, len);
104-
String body = new String(baos.toByteArray(), encoding);
113+
String body = baos.toString(encoding);
105114
for (String line : body.split("\",")) {
106115
line = line.replace("\"", "");
107116
if (!line.startsWith("tag_name"))
@@ -143,7 +152,7 @@ public Boolean call() throws Exception {
143152
* @since 0.4
144153
*/
145154
public static final String getVersion() {
146-
return "0.4";
155+
return "0.5";
147156
}
148157

149158
/**

Java-BetterReflection/src/me/wavelength/betterreflection/BetterReflectionClass.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ public Class<?> getArrayClass() {
204204
return Array.newInstance(clasz, 0).getClass();
205205
}
206206

207+
/**
208+
* @param condition the condition to be met to return an array of the class
209+
* @return an array of {@link #clasz} if condition is true, {@link #clasz}
210+
* otherwise
211+
*/
212+
public Class<?> getArrayClassIf(boolean condition) {
213+
return condition ? getArrayClass() : clasz;
214+
}
215+
207216
public Object getArray(Object... content) {
208217
Object array = Array.newInstance(clasz, content.length);
209218
for (int i = 0; i < content.length; i++) {

0 commit comments

Comments
 (0)