Giter VIP home page Giter VIP logo

jaque's Introduction

Build Status Issue Stats Issue Stats

JAva QUEry

This library enables Java 8 Lambdas to be represented as objects in the form of expression trees at runtime:

void method(Predicate<Customer> p) {
  LambdaExpression<Predicate<Customer>> parsed = LambdaExpression.parse(p);
  //Use parsed Expression Tree...
}

making it possible to create type-safe fluent interfaces, i.e. instead of:

Customer obj = ...
obj.property("name").eq("John")

one can write

method<Customer>(obj -> obj.getName() == "John")

in type-safe, refactoring friendly manner. And then the library developer will be able to parse the produced Lambda to the corresponding Expression Tree for analysis.


For example, JPA Criteria API could benefit a lot from using JaQue, e.g.:

//instead of this:
Root<Pet> pet = cq.from(Pet.class);
Join<Pet, Owner> owner = pet.join(Pet_.owners);

//it could be this:
Join<Pet, Owner> owner = pet.join(Pet::getOwners);

//and instead of this:
query.where(pet.get(Pet_.color).isNull());
query.where(builder.equal(pet.get(Pet_.name), "Fido")
	.and(builder.equal(pet.get(Pet_.color), "brown")));
	
//it could be this:
query.where(pet -> pet.getColor() == null);
query.where(pet -> (pet.getName() == "Fido") && (pet.getColor() == "brown"));

If you are looking for an oportunity to start an open source project, implementing the above should be very beneficial for a very large developer comminuty. Should you start this or any other open source project based on JaQue, I'll be happy to assist you.

How to write fluent interface with JaQue?

  • Suppose you want to reference some class property
public class Fluent<T> {

	// this interface is required to make the lambda Serializable, which removes a need for 
	// jdk.internal.lambda.dumpProxyClasses system property. See below.
	public static interface Property<T, R> extends Function<T, R>, Serializable {
	}

	public Fluent<T> property(Property<T, ?> propertyRef) {
		LambdaExpression<Function<T, ?>> parsed = LambdaExpression
				.parse(propertyRef);
		Expression body = parsed.getBody();
		Expression methodCall = body;
		
		// remove casts
		while (methodCall instanceof UnaryExpression)
			methodCall = ((UnaryExpression) methodCall).getFirst();

		// checks are omitted for brevity
		Member member = ((MemberExpression) ((InvocationExpression) methodCall)
				.getTarget()).getMember();
		
		// use member
		...
		
		return this;
	}
}
  • Now your users will be able to write
Fluent<Customer> f = new Fluent<Customer>();
f.property(Customer::getName);

Make the lambda Serializable, as shown in example above. If the lambda is not serializable, the jdk.internal.lambda.dumpProxyClasses system property must be set and point to an existing writable directory to give the parser access to the lambda byte code.

Resources

jaque's People

Contributors

kostat avatar tomwhoiscontrary avatar

Watchers

James Cloos avatar fattyhan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.