Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.function.Function;
- class A implements Cloneable {
- public Double d = 1.0;
- public Double d2 = 10.0;
- public Function<Double, Double> f = (Double b) -> ((b + d) * d2);
- public Double calculate(Double arg) throws InvocationTargetException, IllegalAccessException {
- Class c = this.getClass();
- Method method = null;// ==c.getMethod("lambda$new$0");
- for (Method m : c.getDeclaredMethods()) {
- if (m.getName().startsWith("lambda")) {
- method = m;
- method.setAccessible(true);
- }
- }
- System.out.println("calculate");
- System.out.println(method);
- System.out.println(method.invoke(this, arg));
- return (Double) method.invoke(this, arg);
- }
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- @Override
- public String toString() {
- return "d = " + d + " d2 = " + d2;
- }
- }
- public class Main {
- public static void asserts(Object expected, Object actual) throws AssertionError{
- if (!expected.equals(actual))
- throw new AssertionError("expectes = " + expected + " actual = " + actual);
- }
- public static void main(String[] args) {
- A original = new A();
- System.out.println("original a " + original);
- try {
- asserts(40., original.calculate(3.));
- A clone = (A) original.clone();
- clone.d2 = 100.;
- asserts(400., clone.calculate(3.));
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- } catch (AssertionError e) {
- System.out.println(e);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement