Skip to content

Commit

Permalink
Add get_entity_transient_id (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin authored Mar 5, 2024
1 parent fd53398 commit 6279947
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 59 deletions.
2 changes: 1 addition & 1 deletion nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInit>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInit>
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInitItems>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapArrayInitItems>
<org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapObjects>WRAP_ALWAYS</org-netbeans-modules-editor-indent.text.plain.CodeStyle.project.wrapObjects>
<netbeans.hint.jdkPlatform>JDK_16</netbeans.hint.jdkPlatform>
<netbeans.hint.jdkPlatform>JDK_17</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -187,7 +189,7 @@ public static void set(Class clazz, Object instance, String variableName, Object
* @param methodName The name of the method.
* @return The invocation result, null if void.
*/
public static Object invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
public static <T> T invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
return invokeMethod(clazz, instance, methodName, new Class[]{}, new Object[]{});
}

Expand All @@ -202,7 +204,7 @@ public static Object invokeMethod(Class clazz, Object instance, String methodNam
* @throws ReflectionException
*/
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
public static Object invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
public static <T> T invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
Class c = instance.getClass();
Class[] argTypes;
{
Expand Down Expand Up @@ -235,7 +237,7 @@ public static Object invokeMethod(Object instance, String methodName, Object...
}
}
m.setAccessible(true);
return m.invoke(instance, params);
return (T) m.invoke(instance, params);
}
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ReflectionException(ex);
Expand All @@ -258,13 +260,13 @@ public static Object invokeMethod(Object instance, String methodName, Object...
* @throws ReflectionException
*/
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
public static Object invokeMethod(Object instance, String methodName) throws ReflectionException {
public static <T> T invokeMethod(Object instance, String methodName) throws ReflectionException {
Class c = instance.getClass();
while(c != null) {
for(Method m : c.getDeclaredMethods()) {
if(methodName.equals(m.getName())) {
if(methodName.equals(m.getName()) && m.getParameterCount() == 0) {
try {
return m.invoke(instance);
return (T) m.invoke(instance);
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ReflectionException(ex);
}
Expand All @@ -288,12 +290,12 @@ public static Object invokeMethod(Object instance, String methodName) throws Ref
* @param args The arguments.
* @return The invocation result, null if void.
*/
public static Object invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
public static <T> T invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
throws ReflectionException {
try {
Method m = clazz.getDeclaredMethod(methodName, argTypes);
m.setAccessible(true);
return m.invoke(instance, args);
return (T) m.invoke(instance, args);
} catch(InvocationTargetException | NoSuchMethodException | IllegalArgumentException
| IllegalAccessException | SecurityException ex) {
throw new ReflectionException(ex);
Expand Down Expand Up @@ -462,9 +464,8 @@ public static void throwUncheckedException(Throwable t) {
*
* @param className the fully qualified name of the desired class.
* @return the {@code Class} object for the class with the specified name.
* @throws LinkageError if the linkage fails
* @throws ExceptionInInitializerError if the initialization provoked by this method fails
* @throws ReflectionException if the class cannot be located
* @throws ReflectionException wraps a LinkageError if the linkage fails, ExceptionInInitializerError if the
* initialization provoked by this method fails, or ClassNotFoundException if the class is not found.
*/
public static Class forName(String className) {
try {
Expand Down Expand Up @@ -515,12 +516,11 @@ public static Class forName(String className) {
* @param loader class loader from which the class must be loaded
* @return class object representing the desired class
*
* @throws LinkageError if the linkage fails
* @throws ExceptionInInitializerError if the initialization provoked by this method fails
* @throws ReflectionException if the class cannot be located by the specified class loader
* @throws SecurityException if a security manager is present, and the {@code loader} is {@code null}, and the
* @throws ReflectionException wraps a LinkageError if the linkage fails, ExceptionInInitializerError if the
* initialization provoked by this method fails, SecurityException if a security manager is present,
* and the {@code loader} is {@code null}, and the
* caller's class loader is not {@code null}, and the caller does not have the
* {@link RuntimePermission}{@code ("getClassLoader")}
* {@link RuntimePermission}{@code ("getClassLoader")}, or ClassNotFoundException if the class is not found.
*
* @see java.lang.Class#forName(String, boolean, ClassLoader)
* @see java.lang.ClassLoader
Expand All @@ -533,4 +533,26 @@ public static Class forName(String name, boolean initialize, ClassLoader loader)
}
}

// Exceptions are expensive, so cache this.
private static Map<String, Boolean> classExistsMap = new HashMap<>();

/**
* Checks if a class exists, according to Class.forName(). This is cached.
* @param name The class name.
* @return True if the class exists.
*/
public static boolean classExists(String name) {
if(classExistsMap.containsKey(name)) {
return classExistsMap.get(name);
}
try {
Class.forName(name);
classExistsMap.put(name, Boolean.TRUE);
return true;
} catch(ClassNotFoundException ex) {
classExistsMap.put(name, Boolean.FALSE);
return false;
}
}

}
33 changes: 18 additions & 15 deletions src/main/java/com/laytonsmith/PureUtilities/Common/StreamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
* Streams are hard sometimes. This class abstracts most of the functionality that is commonly used.
Expand Down Expand Up @@ -44,7 +43,7 @@ public static void Copy(InputStream in, OutputStream out) throws IOException {
public static String GetString(InputStream in) {
try {
return GetString(in, "UTF-8");
} catch (UnsupportedEncodingException ex) {
} catch(UnsupportedEncodingException ex) {
throw new Error(ex);
}
}
Expand Down Expand Up @@ -76,29 +75,31 @@ public static String GetString(InputStream in, String encoding) throws Unsupport
read = input.read(buffer, 0, buffer.length)) {
output.append(buffer, 0, read);
}
} catch (IOException ignore) {
} catch(IOException ignore) {
}

return output.toString();

}

/**
* Fully reads in a stream, as efficiently as possible, and returns a byte array.
* The input stream is not closed afterwards.
* Fully reads in a stream, as efficiently as possible, and returns a byte array. The input stream is not closed
* afterwards.
*
* @param in
* @return
* @throws IOException
*/
public static byte[] GetBytes(InputStream in) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
List<Byte> bytes = new ArrayList<>();
int i;
while((i = bis.read()) != -1) {
bytes.add(((byte) i));
try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
int i;
byte[] buffer = new byte[8 * 1024];
while((i = bis.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
return out.toByteArray();
}
return ArrayUtils.unbox(bytes.toArray(new Byte[bytes.size()]));
}

/**
Expand All @@ -110,7 +111,7 @@ public static byte[] GetBytes(InputStream in) throws IOException {
public static InputStream GetInputStream(String contents) {
try {
return GetInputStream(contents, "UTF-8");
} catch (UnsupportedEncodingException ex) {
} catch(UnsupportedEncodingException ex) {
throw new Error(ex);
}
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public static InputStream GetInputStream(byte[] bytes) {
public static PrintStream GetSystemOut() {
try {
return new PrintStream(System.out, true, "UTF-8");
} catch (UnsupportedEncodingException ex) {
} catch(UnsupportedEncodingException ex) {
throw new Error(ex);
}
}
Expand All @@ -160,13 +161,14 @@ public static PrintStream GetSystemOut() {
public static PrintStream GetSystemErr() {
try {
return new PrintStream(System.err, true, "UTF-8");
} catch (UnsupportedEncodingException ex) {
} catch(UnsupportedEncodingException ex) {
throw new Error(ex);
}
}

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

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

0 comments on commit 6279947

Please sign in to comment.