com.caoccao.javet.utils.JavetReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javet-linux-arm64 Show documentation
Show all versions of javet-linux-arm64 Show documentation
Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
/*
* Copyright (c) 2021-2024. caoccao.com Sam Cao
*
* 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.caoccao.javet.utils;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* The type Javet reflection utils.
*
* @since 0.9.7
*/
public final class JavetReflectionUtils {
private static final String METHOD_NAME_WRITE_REPLACE = "writeReplace";
private JavetReflectionUtils() {
}
/**
* Gets method name from lambda.
*
* Usage:
*
* Suppose there is a test() method.
*
* public String test() { ... }
*
* Let's convert the lambda of test() to string which represents the method name.
*
* String methodName = JavetReflectionUtils.getMethodNameFromLambda((Supplier & Serializable) this::test);
*
*
* @param lambda the lambda
* @return the method name
* @since 0.9.13
*/
public static String getMethodNameFromLambda(Serializable lambda) {
Objects.requireNonNull(lambda);
try {
Method method = lambda.getClass().getDeclaredMethod(METHOD_NAME_WRITE_REPLACE);
method.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(lambda);
return serializedLambda.getImplMethodName();
} catch (Throwable ignored) {
}
return null;
}
/**
* Gets method name set from lambdas.
*
* Usage:
*
* Suppose there are a few methods.
*
* public String abc() { ... }
* public int def() { ... }
*
* Let's convert the lambda of these methods to a set which contains the method names.
*
* Set<String> methodNameSet = JavetReflectionUtils.getMethodNameSetFromLambdas(
* (Supplier & Serializable) this::abc,
* (Supplier & Serializable) this::def);
*
*
* @param lambdas the lambdas
* @return the method name set
* @since 0.9.13
*/
public static Set getMethodNameSetFromLambdas(Serializable... lambdas) {
return Stream.of(Objects.requireNonNull(lambdas))
.filter(Objects::nonNull)
.map(JavetReflectionUtils::getMethodNameFromLambda)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
/**
* Safe set accessible.
*
* @param accessibleObject the accessible object
* @since 0.9.7
*/
public static void safeSetAccessible(AccessibleObject accessibleObject) {
try {
accessibleObject.setAccessible(true);
} catch (Throwable ignored) {
}
}
}