java.lang.invoke.ConstantCallSite Java Examples

The following examples show how to use java.lang.invoke.ConstantCallSite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Delegation.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<Delegation> factory = Proxy2.createAnonymousProxyFactory(Delegation.class, new Class<?>[] { PrintStream.class },
      new ProxyHandler.Default() { 
        @Override
        public CallSite bootstrap(ProxyContext context) throws Throwable {
          MethodHandle target =
            methodBuilder(context.type())
              .dropFirst()
              .unreflect(publicLookup(), PrintStream.class.getMethod("println", String.class));
          return new ConstantCallSite(target);
        }
      });
  
  Delegation hello = factory.create(System.out);
  hello.println("hello proxy2");
}
 
Example #2
Source File: StringConcatFactory.java    From AVM with MIT License 6 votes vote down vote up
/**
 * A bootstrap method for handling string concatenation
 *
 * @see java.lang.invoke.StringConcatFactory#makeConcatWithConstants(MethodHandles.Lookup, java.lang.String, MethodType, java.lang.String, Object...)
 */
public static java.lang.invoke.CallSite avm_makeConcatWithConstants(
        java.lang.invoke.MethodHandles.Lookup owner,
        java.lang.String invokedName,
        MethodType concatType,
        java.lang.String recipe,
        Object... constants) throws NoSuchMethodException, IllegalAccessException {
    InvokeDynamicChecks.checkOwner(owner);
    // Note that we currently only use the avm_makeConcatWithConstants invoked name.
    RuntimeAssertionError.assertTrue("avm_makeConcatWithConstants".equals(invokedName));
    
    final MethodType concatMethodType = MethodType.methodType(
            String.class, // NOTE! First arg is return value
            java.lang.String.class,
            Object[].class,
            Object[].class);
    final MethodHandle concatMethodHandle = owner
            .findStatic(StringConcatFactory.class, "avm_concat", concatMethodType)
            .bindTo(recipe)
            .bindTo(constants)
            .asVarargsCollector(Object[].class)
            .asType(concatType);
    return new ConstantCallSite(concatMethodHandle);
}
 
Example #3
Source File: ORMapper.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
@Override
protected MethodHandle computeValue(Class<?> type) {
  Lookup lookup = lookup();
  return Proxy2.createAnonymousProxyFactory(publicLookup(), methodType(type), new ProxyHandler.Default() {
    @Override
    public CallSite bootstrap(ProxyContext context) throws Throwable {
      Method method = context.method();
      switch(method.getName()) {
      case "create":
        MethodHandle target = methodBuilder(context.type())
            .dropFirst()
            .insertAt(0, ClassValue.class, beanFactories)
            .insertAt(1, Class.class, method.getReturnType())
            .convertTo(Object.class, ClassValue.class, Class.class)
            .unreflect(lookup, ORMapper.class.getDeclaredMethod("newBean", ClassValue.class, Class.class));
        return new ConstantCallSite(target);
      default:
        throw new NoSuchMethodError(method.toString());
      }
    }
  });
}
 
Example #4
Source File: HotSpotGraphBuilderPlugins.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void registerCallSitePlugins(InvocationPlugins plugins) {
    InvocationPlugin plugin = new InvocationPlugin() {
        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            ValueNode callSite = receiver.get();
            ValueNode folded = CallSiteTargetNode.tryFold(GraphUtil.originalValue(callSite), b.getMetaAccess(), b.getAssumptions());
            if (folded != null) {
                b.addPush(JavaKind.Object, folded);
            } else {
                b.addPush(JavaKind.Object, new CallSiteTargetNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), callSite));
            }
            return true;
        }

        @Override
        public boolean inlineOnly() {
            return true;
        }
    };
    plugins.register(plugin, ConstantCallSite.class, "getTarget", Receiver.class);
    plugins.register(plugin, MutableCallSite.class, "getTarget", Receiver.class);
    plugins.register(plugin, VolatileCallSite.class, "getTarget", Receiver.class);
}
 
Example #5
Source File: Intercept.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<IntBinaryOperator> factory = Proxy2.createAnonymousProxyFactory(IntBinaryOperator.class, new Class<?>[] { IntBinaryOperator.class },
      new ProxyHandler.Default() { 
        @Override
        public CallSite bootstrap(ProxyContext context) throws Throwable {
          MethodHandle target =
            methodBuilder(context.type())
              .dropFirst()
              .before(b -> b
                  .dropFirst()
                  .unreflect(publicLookup(), Intercept.class.getMethod("intercept", int.class, int.class)))
              .unreflect(publicLookup(), context.method());
          return new ConstantCallSite(target);
        }
      });
  
  //IntBinaryOperator op = (a, b) -> a + b;
  IntBinaryOperator op = (a, b) -> {
    throw null;
  };
  
  IntBinaryOperator op2 = factory.create(op);
  System.out.println(op2.applyAsInt(1, 2));
}
 
Example #6
Source File: Print.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<Print> factory = Proxy2.createAnonymousProxyFactory(Print.class, new ProxyHandler.Default() { 
    @Override
    public CallSite bootstrap(ProxyContext context) throws Throwable {
      MethodHandle target =
          methodBuilder(context.type())
            .dropFirst()
            .insertAt(0, PrintStream.class, System.out)
            .unreflect(publicLookup(), PrintStream.class.getMethod("println", String.class));
      return new ConstantCallSite(target);
    }
  });
  
  Print hello = factory.create();
  hello.println("hello proxy2");
}
 
Example #7
Source File: Transparent.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<IntBinaryOperator> factory = Proxy2.createAnonymousProxyFactory(IntBinaryOperator.class, new Class<?>[] { IntBinaryOperator.class },
      new ProxyHandler.Default() { 
        @Override
        public CallSite bootstrap(ProxyContext context) throws Throwable {
          MethodHandle target =
            methodBuilder(context.type())
              .dropFirst()
              .unreflect(publicLookup(), context.method());
          return new ConstantCallSite(target);
        }
      });
  
  IntBinaryOperator op = (a, b) -> a + b;
  
  IntBinaryOperator op2 = factory.create(op);
  System.out.println(op2.applyAsInt(1, 2));
}
 
Example #8
Source File: GCTest.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<Runnable> factory = Proxy2.createAnonymousProxyFactory(Runnable.class, new ProxyHandler.Default() { 
    @Override
    public CallSite bootstrap(ProxyContext context) throws Throwable {
      return new ConstantCallSite(
          dropArguments(
            insertArguments(
              publicLookup().findVirtual(PrintStream.class, "println",
                  methodType(void.class, String.class)),
              0, System.out, "hello proxy"),
            0, Object.class));
    }
  });
  Runnable runnable = factory.create();
  runnable.run();
  
  factory = null;
  runnable = null;
  
  System.gc();  // should unload the proxy class
}
 
Example #9
Source File: Intercept2.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<IntBinaryOperator> factory = Proxy2.createAnonymousProxyFactory(IntBinaryOperator.class, new Class<?>[] { IntBinaryOperator.class },
      new ProxyHandler.Default() { 
        @Override
        public CallSite bootstrap(ProxyContext context) throws Throwable {
          MethodHandle target =
            methodBuilder(context.type())
              .dropFirst()
              .before(b -> b
                  .dropFirst()
                  .boxAll()
                  .unreflect(publicLookup(), Intercept2.class.getMethod("intercept", Object[].class)))
              .unreflect(publicLookup(), context.method());
          return new ConstantCallSite(target);
        }
      });
  
  IntBinaryOperator op = (a, b) -> a + b;
  
  IntBinaryOperator op2 = factory.create(op);
  System.out.println(op2.applyAsInt(1, 2));
}
 
Example #10
Source File: Hello.java    From proxy2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  ProxyFactory<Hello> factory = Proxy2.createAnonymousProxyFactory(Hello.class, new ProxyHandler.Default() { 
    @Override
    public CallSite bootstrap(ProxyContext context) throws Throwable {
      System.out.println("bootstrap method " + context.method());
      System.out.println("bootstrap type " + context.type());
      MethodHandle target =
          methodBuilder(context.type())
            .dropFirst()
            .unreflect(publicLookup(), String.class.getMethod("concat", String.class));
      return new ConstantCallSite(target);
    }
  });
  
  Hello simple = factory.create();
  System.out.println(simple.message("hello ", "proxy"));
  System.out.println(simple.message("hello ", "proxy 2"));
}
 
Example #11
Source File: Lng.java    From boon with Apache License 2.0 6 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public  static <T> long reduceBy( final long[] array, T object ) {
    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }


    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for ( long v : array ) {
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object);
    }

}
 
Example #12
Source File: Flt.java    From boon with Apache License 2.0 6 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public  static <T> double reduceBy( final float[] array, T object ) {
    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }


    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for ( float v : array ) {
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object);
    }

}
 
Example #13
Source File: Int.java    From boon with Apache License 2.0 6 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public  static <T> long reduceBy( final int[] array, T object ) {
    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }


    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for ( int v : array ) {
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object);
    }

}
 
Example #14
Source File: Dbl.java    From boon with Apache License 2.0 6 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public  static <T> double reduceBy( final double[] array, T object ) {
    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }


    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for ( double v : array ) {
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object);
    }

}
 
Example #15
Source File: NativeCalls.java    From es6draft with MIT License 6 votes vote down vote up
/**
 * Returns the native call {@code CallSite} object.
 * 
 * @param caller
 *            the caller lookup object
 * @param name
 *            the native call name
 * @param type
 *            the native call type
 * @return the native call {@code CallSite} object
 */
public static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
    MethodHandle target;
    try {
        if (!name.startsWith("native:")) {
            throw new IllegalArgumentException();
        }
        String methodName = name.substring("native:".length());
        MethodHandle mh = nativeMethods.computeIfAbsent(methodName, NativeCalls::getNativeMethodHandle);
        if (mh == null) {
            return createRuntimeCallSite(methodName, type);
        }
        target = adaptMethodHandle(methodName, type, mh);
    } catch (IllegalArgumentException e) {
        target = invalidCallHandle(name, type);
    }
    return new ConstantCallSite(target);
}
 
Example #16
Source File: MethodLinker.java    From gravel with Apache License 2.0 5 votes vote down vote up
public static CallSite globalReadBootstrap(Lookup lookup, String selector,
		MethodType type, String namespaceString) throws Throwable {
	AbsoluteReference namespace = (AbsoluteReference) Reference.factory
			.value_(namespaceString);
	AbsoluteReference fullReference = namespace.$slash$(Symbol
			.value(selector));
	final AlmostFinalValue singletonHolder = ImageBootstrapper.systemMapping
			.resolveSingletonHolder_(fullReference);
	return new ConstantCallSite(singletonHolder.createGetter());
}
 
Example #17
Source File: MethodLinker.java    From gravel with Apache License 2.0 5 votes vote down vote up
public static CallSite constructorBootstrap(Lookup lookup, String selector,
		MethodType type, String referenceString) throws Throwable {
	Reference reference = Reference.factory.value_(referenceString);
	Constructor constructor = ImageBootstrapper.systemMapping.classMappingAtReference_(reference).identityClass().getConstructor();
	MethodHandle constructorHandle = lookup.unreflectConstructor(constructor);
	return new ConstantCallSite(constructorHandle.asType(type));
}
 
Example #18
Source File: AdviceBootstrap.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
public static ConstantCallSite bootstrap(MethodHandles.Lookup lookup,
                                         String invokedMethodName,
                                         MethodType invokedMethodType,
                                         String invokedClassName,
                                         int adviceType,
                                         Class<?> sourceType,
                                         String sourceMethodName,
                                         MethodHandle sourceMethod) throws Exception {
    return new ConstantCallSite(lookup.findStatic(Class.forName(invokedClassName, false, lookup.lookupClass().getClassLoader()),
            invokedMethodName,
            invokedMethodType));
}
 
Example #19
Source File: Int.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param object function
 * @return reduction
 */
public static long reduceBy( final int[] array,  int length,
                             Object object ) {


    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, length, object );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for (int index=0; index < length; index++) {
                int v = array[index];
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, object );
    }


}
 
Example #20
Source File: Int.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public static <T> long reduceBy( final int[] array, T object, String methodName ) {

    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object, methodName);
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object, methodName);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for ( int v : array ) {
                    sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object, methodName);
    }


}
 
Example #21
Source File: Flt.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param object function
 * @return reduction
 */
public static double reduceBy( final float[] array, int start, int length,
                             Object object ) {


    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for (int index=start; index < length; index++) {
                float v = array[index];
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object );
    }

}
 
Example #22
Source File: Flt.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param function function
 * @param function functionName
 * @return reduction
 */
public static double reduceBy( final float[] array,  int length,
                             Object function, String functionName ) {


    if (function.getClass().isAnonymousClass()) {
        return reduceByR(array, length, function, functionName );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(function, functionName );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for (int index=0; index < length; index++) {
                float v = array[index];
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, function, functionName );
    }


}
 
Example #23
Source File: Flt.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param object function
 * @return reduction
 */
public static double reduceBy( final float[] array,  int length,
                             Object object ) {


    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, length, object );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for (int index=0; index < length; index++) {
                float v = array[index];
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, object );
    }


}
 
Example #24
Source File: Flt.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce by functional support for int arrays.
 * @param array array of items to reduce by
 * @param object object that contains the reduce by function
 * @param <T> the type of object
 * @return the final reduction
 */
public static <T> double reduceBy( final float[] array, T object, String methodName ) {

    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object, methodName);
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object, methodName);
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            double sum = 0;
            for ( float v : array ) {
                sum = (double) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object, methodName);
    }


}
 
Example #25
Source File: VMAnonymousClasses.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws ReflectiveOperationException  {
    // Objects
    test(new Object());
    test("TEST");
    test(new VMAnonymousClasses());
    test(null);

    // Class
    test(String.class);

    // Arrays
    test(new boolean[0]);
    test(new byte[0]);
    test(new char[0]);
    test(new short[0]);
    test(new int[0]);
    test(new long[0]);
    test(new float[0]);
    test(new double[0]);
    test(new Object[0]);

    // Multi-dimensional arrays
    test(new byte[0][0]);
    test(new Object[0][0]);

    // MethodHandle-related
    MethodType   mt = MethodType.methodType(void.class, String[].class);
    MethodHandle mh = MethodHandles.lookup().findStatic(VMAnonymousClasses.class, "main", mt);
    test(mt);
    test(mh);
    test(new ConstantCallSite(mh));
    test(new MutableCallSite(MethodType.methodType(void.class)));
    test(new VolatileCallSite(MethodType.methodType(void.class)));

    System.out.println("TEST PASSED");
}
 
Example #26
Source File: Lng.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param object function
 * @return reduction
 */
public static long reduceBy( final long[] array, int start, int length,
                             Object object ) {


    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, object );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for (int index=start; index < length; index++) {
                long v = array[index];
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, object );
    }

}
 
Example #27
Source File: Lng.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param function function
 * @param function functionName
 * @return reduction
 */
public static long reduceBy( final long[] array,  int length,
                             Object function, String functionName ) {


    if (function.getClass().isAnonymousClass()) {
        return reduceByR(array, length, function, functionName );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(function, functionName );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for (int index=0; index < length; index++) {
                long v = array[index];
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, function, functionName );
    }


}
 
Example #28
Source File: Lng.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param object function
 * @return reduction
 */
public static long reduceBy( final long[] array,  int length,
                             Object object ) {


    if (object.getClass().isAnonymousClass()) {
        return reduceByR(array, length, object );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for (int index=0; index < length; index++) {
                long v = array[index];
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, object );
    }


}
 
Example #29
Source File: Tester.java    From grappa with Apache License 2.0 5 votes vote down vote up
public static CallSite genCallSite(final MethodHandles.Lookup caller,
    final String invokedName, final MethodType invokedType,
    final String methodName)
    throws NoSuchMethodException, IllegalAccessException
{
    final MethodHandles.Lookup lookup = MethodHandles.lookup();
    final MethodHandle handle
        = lookup.findStatic( Tester.class, methodName, MethodType

            .methodType(void.class, String.class));
    return new ConstantCallSite(handle);
}
 
Example #30
Source File: Int.java    From boon with Apache License 2.0 5 votes vote down vote up
/**
 * Reduce By
 * @param array array of items to reduce by
 * @param length where to end in the array
 * @param function function
 * @param function functionName
 * @return reduction
 */
public static long reduceBy( final int[] array,  int length,
                             Object function, String functionName ) {


    if (function.getClass().isAnonymousClass()) {
        return reduceByR(array, length, function, functionName );
    }

    try {
        ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(function, functionName );
        MethodHandle methodHandle = callSite.dynamicInvoker();
        try {

            long sum = 0;
            for (int index=0; index < length; index++) {
                int v = array[index];
                sum = (long) methodHandle.invokeExact( sum, v );

            }
            return sum;
        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }
    } catch (Exception ex) {
        return reduceByR(array, length, function, functionName );
    }


}