Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.92 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.util.function.Function;
  4.  
  5. class A implements Cloneable {
  6.  
  7.     public Double d = 1.0;
  8.     public Double d2 = 10.0;
  9.  
  10.     public Function<Double, Double> f = (Double b) -> ((b + d) * d2);
  11.  
  12.     public Double calculate(Double arg) throws InvocationTargetException, IllegalAccessException {
  13.         Class c = this.getClass();
  14.         Method method = null;// ==c.getMethod("lambda$new$0");
  15.         for (Method m : c.getDeclaredMethods()) {
  16.             if (m.getName().startsWith("lambda")) {
  17.                 method = m;
  18.                 method.setAccessible(true);
  19.             }
  20.         }
  21.         System.out.println("calculate");
  22.         System.out.println(method);
  23.         System.out.println(method.invoke(this, arg));
  24.  
  25.         return (Double) method.invoke(this, arg);
  26.     }
  27.  
  28.  
  29.     @Override
  30.     protected Object clone() throws CloneNotSupportedException {
  31.         return super.clone();
  32.     }
  33.  
  34.     @Override
  35.     public String toString() {
  36.         return "d = " + d + " d2 = " + d2;
  37.     }
  38.  
  39. }
  40.  
  41. public class Main {
  42.  
  43.     public static void asserts(Object expected, Object actual) throws AssertionError{
  44.         if (!expected.equals(actual))
  45.             throw new AssertionError("expectes = " + expected + " actual = " + actual);
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.         A original = new A();
  50.         System.out.println("original a " + original);
  51.  
  52.         try {
  53.             asserts(40., original.calculate(3.));
  54.             A clone = (A) original.clone();
  55.             clone.d2 = 100.;
  56.            
  57.             asserts(400., clone.calculate(3.));
  58.         } catch (CloneNotSupportedException e) {
  59.             e.printStackTrace();
  60.         } catch (AssertionError e) {
  61.             System.out.println(e);
  62.         } catch (Exception e) {
  63.             System.out.println(e);
  64.         }
  65.  
  66.  
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement