io.datakernel.util.ReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-eventloop Show documentation
Show all versions of datakernel-eventloop Show documentation
Efficient non-blocking network and file I/O, for building Node.js-like client/server applications with
high performance requirements. It is similar to Event Loop in Node.js.
Although Eventloop runs in a single thread, multiple event loops can be
launched at the same time allowing for efficient CPU usage.
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* 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 io.datakernel.util;
import io.datakernel.annotation.Nullable;
import io.datakernel.jmx.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import static io.datakernel.util.CollectionUtils.first;
import static io.datakernel.util.Preconditions.checkArgument;
public final class ReflectionUtils {
private ReflectionUtils() {
}
public static boolean isJmxStats(Class> clazz) {
return JmxStats.class.isAssignableFrom(clazz);
}
public static boolean isJmxRefreshableStats(Class> clazz) {
return JmxRefreshableStats.class.isAssignableFrom(clazz);
}
public static boolean isPrimitiveType(Class> clazz) {
return boolean.class.isAssignableFrom(clazz)
|| byte.class.isAssignableFrom(clazz)
|| short.class.isAssignableFrom(clazz)
|| char.class.isAssignableFrom(clazz)
|| int.class.isAssignableFrom(clazz)
|| long.class.isAssignableFrom(clazz)
|| float.class.isAssignableFrom(clazz)
|| double.class.isAssignableFrom(clazz);
}
public static boolean isPrimitiveTypeWrapper(Class> clazz) {
return Boolean.class.isAssignableFrom(clazz)
|| Byte.class.isAssignableFrom(clazz)
|| Short.class.isAssignableFrom(clazz)
|| Character.class.isAssignableFrom(clazz)
|| Integer.class.isAssignableFrom(clazz)
|| Long.class.isAssignableFrom(clazz)
|| Float.class.isAssignableFrom(clazz)
|| Double.class.isAssignableFrom(clazz);
}
public static boolean isString(Class> clazz) {
return String.class.isAssignableFrom(clazz);
}
public static boolean isThrowable(Class> clazz) {
return Throwable.class.isAssignableFrom(clazz);
}
public static boolean isGetter(Method method) {
boolean returnsBoolean = method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class;
boolean isIsGetter = method.getName().length() > 2 && method.getName().startsWith("is") && returnsBoolean;
boolean doesntReturnVoid = method.getReturnType() != void.class;
boolean isGetGetter = method.getName().length() >= 3 && method.getName().startsWith("get") && doesntReturnVoid;
return isIsGetter || isGetGetter;
}
public static String extractFieldNameFromGetter(Method getter) {
checkArgument(isGetter(getter));
if (getter.getName().startsWith("get")) {
if (getter.getName().length() == 3) {
return "";
} else {
String getterName = getter.getName();
String firstLetter = getterName.substring(3, 4);
String restOfName = getterName.substring(4);
return firstLetter.toLowerCase() + restOfName;
}
} else if (getter.getName().startsWith("is")) {
String getterName = getter.getName();
String firstLetter = getterName.substring(2, 3);
String restOfName = getterName.substring(3);
return firstLetter.toLowerCase() + restOfName;
} else {
throw new RuntimeException();
}
}
public static boolean isSetter(Method method) {
boolean hasSingleParameter = method.getParameterTypes().length == 1;
return method.getName().length() > 3 && method.getName().startsWith("set")
&& method.getReturnType() == void.class && hasSingleParameter;
}
public static String extractFieldNameFromSetter(Method setter) {
checkArgument(isSetter(setter));
String setterName = setter.getName();
String firstLetter = setterName.substring(3, 4);
String restOfName = setterName.substring(4);
return firstLetter.toLowerCase() + restOfName;
}
public static boolean classHasPublicNoArgConstructor(Class> clazz) {
for (Constructor> constructor : clazz.getConstructors()) {
if (constructor.getParameterTypes().length == 0) {
return true;
}
}
return false;
}
public static boolean classHasPublicStaticFactoryMethod(Class> clazz, String methodName) {
Method createMethod;
try {
createMethod = clazz.getDeclaredMethod(methodName);
} catch (NoSuchMethodException e) {
return false;
}
return Modifier.isStatic(createMethod.getModifiers()) &&
Modifier.isPublic(createMethod.getModifiers()) &&
createMethod.getReturnType().equals(clazz);
}
private static void visitFields(Object instance, Function