Skip to content

Commit 6279947

Browse files
authored
Add get_entity_transient_id (#1377)
1 parent fd53398 commit 6279947

9 files changed

Lines changed: 160 additions & 59 deletions

File tree

nb-configuration.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInit>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInit>
3838
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInitItems>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInitItems>
3939
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapObjects>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapObjects>
40-
<netbeans.hint.jdkPlatform>JDK_16</netbeans.hint.jdkPlatform>
40+
<netbeans.hint.jdkPlatform>JDK_17</netbeans.hint.jdkPlatform>
4141
</properties>
4242
</project-shared-configuration>

src/main/java/com/laytonsmith/PureUtilities/Common/ReflectionUtils.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import java.lang.reflect.Method;
88
import java.lang.reflect.Modifier;
99
import java.util.ArrayList;
10+
import java.util.HashMap;
1011
import java.util.HashSet;
1112
import java.util.List;
13+
import java.util.Map;
1214
import java.util.Set;
1315

1416
/**
@@ -187,7 +189,7 @@ public static void set(Class clazz, Object instance, String variableName, Object
187189
* @param methodName The name of the method.
188190
* @return The invocation result, null if void.
189191
*/
190-
public static Object invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
192+
public static <T> T invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
191193
return invokeMethod(clazz, instance, methodName, new Class[]{}, new Object[]{});
192194
}
193195

@@ -202,7 +204,7 @@ public static Object invokeMethod(Class clazz, Object instance, String methodNam
202204
* @throws ReflectionException
203205
*/
204206
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
205-
public static Object invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
207+
public static <T> T invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
206208
Class c = instance.getClass();
207209
Class[] argTypes;
208210
{
@@ -235,7 +237,7 @@ public static Object invokeMethod(Object instance, String methodName, Object...
235237
}
236238
}
237239
m.setAccessible(true);
238-
return m.invoke(instance, params);
240+
return (T) m.invoke(instance, params);
239241
}
240242
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
241243
throw new ReflectionException(ex);
@@ -258,13 +260,13 @@ public static Object invokeMethod(Object instance, String methodName, Object...
258260
* @throws ReflectionException
259261
*/
260262
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
261-
public static Object invokeMethod(Object instance, String methodName) throws ReflectionException {
263+
public static <T> T invokeMethod(Object instance, String methodName) throws ReflectionException {
262264
Class c = instance.getClass();
263265
while(c != null) {
264266
for(Method m : c.getDeclaredMethods()) {
265-
if(methodName.equals(m.getName())) {
267+
if(methodName.equals(m.getName()) && m.getParameterCount() == 0) {
266268
try {
267-
return m.invoke(instance);
269+
return (T) m.invoke(instance);
268270
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
269271
throw new ReflectionException(ex);
270272
}
@@ -288,12 +290,12 @@ public static Object invokeMethod(Object instance, String methodName) throws Ref
288290
* @param args The arguments.
289291
* @return The invocation result, null if void.
290292
*/
291-
public static Object invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
293+
public static <T> T invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
292294
throws ReflectionException {
293295
try {
294296
Method m = clazz.getDeclaredMethod(methodName, argTypes);
295297
m.setAccessible(true);
296-
return m.invoke(instance, args);
298+
return (T) m.invoke(instance, args);
297299
} catch(InvocationTargetException | NoSuchMethodException | IllegalArgumentException
298300
| IllegalAccessException | SecurityException ex) {
299301
throw new ReflectionException(ex);
@@ -462,9 +464,8 @@ public static void throwUncheckedException(Throwable t) {
462464
*
463465
* @param className the fully qualified name of the desired class.
464466
* @return the {@code Class} object for the class with the specified name.
465-
* @throws LinkageError if the linkage fails
466-
* @throws ExceptionInInitializerError if the initialization provoked by this method fails
467-
* @throws ReflectionException if the class cannot be located
467+
* @throws ReflectionException wraps a LinkageError if the linkage fails, ExceptionInInitializerError if the
468+
* initialization provoked by this method fails, or ClassNotFoundException if the class is not found.
468469
*/
469470
public static Class forName(String className) {
470471
try {
@@ -515,12 +516,11 @@ public static Class forName(String className) {
515516
* @param loader class loader from which the class must be loaded
516517
* @return class object representing the desired class
517518
*
518-
* @throws LinkageError if the linkage fails
519-
* @throws ExceptionInInitializerError if the initialization provoked by this method fails
520-
* @throws ReflectionException if the class cannot be located by the specified class loader
521-
* @throws SecurityException if a security manager is present, and the {@code loader} is {@code null}, and the
519+
* @throws ReflectionException wraps a LinkageError if the linkage fails, ExceptionInInitializerError if the
520+
* initialization provoked by this method fails, SecurityException if a security manager is present,
521+
* and the {@code loader} is {@code null}, and the
522522
* caller's class loader is not {@code null}, and the caller does not have the
523-
* {@link RuntimePermission}{@code ("getClassLoader")}
523+
* {@link RuntimePermission}{@code ("getClassLoader")}, or ClassNotFoundException if the class is not found.
524524
*
525525
* @see java.lang.Class#forName(String, boolean, ClassLoader)
526526
* @see java.lang.ClassLoader
@@ -533,4 +533,26 @@ public static Class forName(String name, boolean initialize, ClassLoader loader)
533533
}
534534
}
535535

536+
// Exceptions are expensive, so cache this.
537+
private static Map<String, Boolean> classExistsMap = new HashMap<>();
538+
539+
/**
540+
* Checks if a class exists, according to Class.forName(). This is cached.
541+
* @param name The class name.
542+
* @return True if the class exists.
543+
*/
544+
public static boolean classExists(String name) {
545+
if(classExistsMap.containsKey(name)) {
546+
return classExistsMap.get(name);
547+
}
548+
try {
549+
Class.forName(name);
550+
classExistsMap.put(name, Boolean.TRUE);
551+
return true;
552+
} catch(ClassNotFoundException ex) {
553+
classExistsMap.put(name, Boolean.FALSE);
554+
return false;
555+
}
556+
}
557+
536558
}

src/main/java/com/laytonsmith/PureUtilities/Common/StreamUtils.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import java.io.BufferedInputStream;
44
import java.io.ByteArrayInputStream;
5+
import java.io.ByteArrayOutputStream;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import java.io.InputStreamReader;
89
import java.io.OutputStream;
910
import java.io.PrintStream;
1011
import java.io.UnsupportedEncodingException;
11-
import java.util.ArrayList;
12-
import java.util.List;
1312

1413
/**
1514
* Streams are hard sometimes. This class abstracts most of the functionality that is commonly used.
@@ -44,7 +43,7 @@ public static void Copy(InputStream in, OutputStream out) throws IOException {
4443
public static String GetString(InputStream in) {
4544
try {
4645
return GetString(in, "UTF-8");
47-
} catch (UnsupportedEncodingException ex) {
46+
} catch(UnsupportedEncodingException ex) {
4847
throw new Error(ex);
4948
}
5049
}
@@ -76,29 +75,31 @@ public static String GetString(InputStream in, String encoding) throws Unsupport
7675
read = input.read(buffer, 0, buffer.length)) {
7776
output.append(buffer, 0, read);
7877
}
79-
} catch (IOException ignore) {
78+
} catch(IOException ignore) {
8079
}
8180

8281
return output.toString();
8382

8483
}
8584

8685
/**
87-
* Fully reads in a stream, as efficiently as possible, and returns a byte array.
88-
* The input stream is not closed afterwards.
86+
* Fully reads in a stream, as efficiently as possible, and returns a byte array. The input stream is not closed
87+
* afterwards.
8988
*
9089
* @param in
9190
* @return
9291
* @throws IOException
9392
*/
9493
public static byte[] GetBytes(InputStream in) throws IOException {
9594
BufferedInputStream bis = new BufferedInputStream(in);
96-
List<Byte> bytes = new ArrayList<>();
97-
int i;
98-
while((i = bis.read()) != -1) {
99-
bytes.add(((byte) i));
95+
try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
96+
int i;
97+
byte[] buffer = new byte[8 * 1024];
98+
while((i = bis.read(buffer)) != -1) {
99+
out.write(buffer, 0, i);
100+
}
101+
return out.toByteArray();
100102
}
101-
return ArrayUtils.unbox(bytes.toArray(new Byte[bytes.size()]));
102103
}
103104

104105
/**
@@ -110,7 +111,7 @@ public static byte[] GetBytes(InputStream in) throws IOException {
110111
public static InputStream GetInputStream(String contents) {
111112
try {
112113
return GetInputStream(contents, "UTF-8");
113-
} catch (UnsupportedEncodingException ex) {
114+
} catch(UnsupportedEncodingException ex) {
114115
throw new Error(ex);
115116
}
116117
}
@@ -146,7 +147,7 @@ public static InputStream GetInputStream(byte[] bytes) {
146147
public static PrintStream GetSystemOut() {
147148
try {
148149
return new PrintStream(System.out, true, "UTF-8");
149-
} catch (UnsupportedEncodingException ex) {
150+
} catch(UnsupportedEncodingException ex) {
150151
throw new Error(ex);
151152
}
152153
}
@@ -160,13 +161,14 @@ public static PrintStream GetSystemOut() {
160161
public static PrintStream GetSystemErr() {
161162
try {
162163
return new PrintStream(System.err, true, "UTF-8");
163-
} catch (UnsupportedEncodingException ex) {
164+
} catch(UnsupportedEncodingException ex) {
164165
throw new Error(ex);
165166
}
166167
}
167168

168169
/**
169170
* Gets a resource string with the specified encoding, relative to the class that is calling this method.
171+
*
170172
* @param name The name of the resource. The name should follow the same naming conventions used by
171173
* {@link Class#getResource(java.lang.String)}.
172174
* @param encoding The encoding to use on the resource.
@@ -185,6 +187,7 @@ public static final String GetResource(String name, String encoding) throws Unsu
185187

186188
/**
187189
* Gets a resource as a UTF-8 encoded string, relative to the class that is calling this method.
190+
*
188191
* @param name The name of the resource. The name should follow the same naming conventions used by
189192
* {@link Class#getResource(java.lang.String)}.
190193
* @return A string depiction of the specified resource.
@@ -193,7 +196,7 @@ public static final String GetResource(String name, String encoding) throws Unsu
193196
public static final String GetResource(String name) throws IllegalArgumentException {
194197
try {
195198
return GetResource(name, "UTF-8");
196-
} catch (UnsupportedEncodingException ex) {
199+
} catch(UnsupportedEncodingException ex) {
197200
throw new Error(ex);
198201
}
199202
}

0 commit comments

Comments
 (0)