Git Product home page Git Product logo

easybuilder's Introduction

EasyBuilder

A dynamic builder pattern tool for Java.

README - EasyBuilder 1.0

EasyBuilder is licensed under the MIT License, see LICENSE file.

EasyBuilder is a utility package that allows you to quickly create Builders for your applications objects. EasyBuilder has been built and tested with JDK 1.4 and JDK 1.5. It relies on the Objenesis 1.1 library. If you have Aspect-J (1.5.4) you can also use the TracingAspect to generate logging information. Logging is supported through SLF4J 1.5.8 and uses the 'Simple Logger' by default.

Installation:

Add the following jars to your classpath;

  • easybuilder-1.0.jar
  • objenesis-3.2.jar

Optionally you may include

  • aspectjrt-1.9.9.1.jar
  • slf4j-api-2.0.3.jar
  • slf4j-simple-2.0.3.jar

Usage:

EasyBuilder establishes a simple DSL for the builder pattern.

The first step is to create the EasyBuilder instance. To do this, use the constructor to specify the target type to be built, e.g.

	new EasyBuilder(SomeClass.class);

Once you've created the builder, apply the builder DSL methods to establish the assembly instructions for the target class. Once all of the instructions have been created, call the build() method to assemble the object.

The DSL methods are available;

Method Description
build() Execute all the instructions provided. This should be the last thing you call.
bypassConstructor() Use Objenesis to skip the class constructor, handy when the constructor has some undesirable side-effects.
setField(,) Sets the value of a field on the target class
setField(,,) Sets the value of a field on the target class based on it's defining class
setFields() Sets the values of fields named in the map. The map is keyed by field name, and the values in the map are the values to use.
useAlternateConstructor(Object[]) Construct the object using some complex constructor that accepts the arguments provided. The arguments should be presented in the same order as the constructor expects them using the specific types of the constructors arguments. EasyBuilder will do the rest.

Example Usage:

Your best examples are in the EasyBuilderTest.java file, however, the simplest usage pattern is as follows;

JDK 1.4

EasyBuilder builder = new EasyBuilder(SomeNonJavaBean.class);
builder.builder.bypassConstructor();
builder.setField("param1", "someValue");
builder.setField("param2", 42);
SomeNonJavaBean anotherNonBean = (SomeNonJavaBean) builder.build();

JDK 1.5

EasyBuilder builder = new EasyBuilder(SomeNonJavaBean.class) {
	{
		bypassConstructor();
		setField("param1", "someValue");
		setField("param2", 42);
	}
};
SomeNonJavaBean anotherNonBean = (SomeNonJavaBean) builder.build();

Tracing EasyBuilders Internals:

Using AspectJ you can trace the internal activity of EasyBuilder.

easybuilder's People

Contributors

rdammkoehler avatar

Stargazers

 avatar

Watchers

 avatar  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.