
com.hazelcast.jet.impl.util.ReflectionUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.jet.impl.util;
import com.hazelcast.internal.nio.ClassLoaderUtil;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import static com.hazelcast.internal.util.ExceptionUtil.sneakyThrow;
public final class ReflectionUtils {
private ReflectionUtils() {
}
/**
* See {@link ClassLoaderUtil#loadClass(ClassLoader, String)}. Exceptions
* are sneakily thrown.
*/
public static Class loadClass(ClassLoader classLoaderHint, String name) {
try {
return ClassLoaderUtil.loadClass(classLoaderHint, name);
} catch (ClassNotFoundException e) {
throw sneakyThrow(e);
}
}
/**
* See {@link ClassLoaderUtil#newInstance(ClassLoader, String)}. Exceptions
* are sneakily thrown.
*/
public static T newInstance(ClassLoader classLoader, String name) {
try {
return ClassLoaderUtil.newInstance(classLoader, name);
} catch (Exception e) {
throw sneakyThrow(e);
}
}
/**
* Reads a value of a static field. In case of any exceptions it returns
* null.
*/
public static T readStaticFieldOrNull(String className, String fieldName) {
try {
Class> clazz = Class.forName(className);
return readStaticField(clazz, fieldName);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | SecurityException e) {
return null;
}
}
@SuppressWarnings("unchecked")
private static T readStaticField(Class> clazz, String fieldName) throws NoSuchFieldException,
IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return (T) field.get(null);
}
private static String toPath(String name) {
return name.replace('.', '/');
}
public static String toClassResourceId(String name) {
return toPath(name) + ".class";
}
@Nullable
public static byte[] getClassContent(String name, ClassLoader classLoader) throws IOException {
try (InputStream is = classLoader.getResourceAsStream(toClassResourceId(name))) {
if (is == null) {
throw new IOException("Resource not found");
} else {
return is.readAllBytes();
}
}
}
/**
* Finds the value for a static field. If the field doesn't exist, null is returned.
*
* @param className name of the class.
* @param fieldName the name of the static field.
* @param
* @return the value of the static field. If the field doesn't exist, null is returned.
*/
public static E findStaticFieldValue(String className, String fieldName) {
try {
Class> clazz = Class.forName(className);
Field field = clazz.getField(fieldName);
return (E) field.get(null);
} catch (Exception ignore) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy