Simply pass a method reference to Executables
class. This will provide you with a java.lang.reflect.Method
or a java.lang.reflect.Constructor
.
// Get method
Method containsMethod = Executables.findMethod(String::contains);
// Get another method
Method getPropertyMethod = Executables.findMethod(MyBean::getProperty);
// Get method that throws checked exception
Method exceptionThrowingMethod = Executables.findMethod(Files::deleteIfExists);
// To get vararg method, you must specify parameters in generics
Method varargMethod = Executables.<Path, FileAttribute<?>[]> findMethod(Files::createDirectories);
// Get constructor
Constructor<MyBean> ctor = Executables.findConstructor(MyBean::new);
Notice that you have to provide the method parameters in generics under certain circumstances (When the method is overloaded, or if the method has a varargs parameter).
The project requires Java 8 or above.
Add this to your pom.xml:
<dependency>
<groupId>com.github.meanbeanlib</groupId>
<artifactId>meanmirror</artifactId>
<version>1.0.0</version>
</dependency>
This is a fork of safety-mirror project.
This code is released under the Apache 2.0 license.
meanmirror
Copyright (C) 2020 the original author or authors.
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.