2 recommended tools to use in the Java coding process

Source: Internet
Author: User
Tags assert

Recommended 2 tool classes that can reduce the amount of code and are easy to use during coding: for Java reflection and single-measurement assert.

1 Introduction to Mirror:java Reflection

Official website: http://projetos.vidageek.net/mirror/mirror/

Simplify the command-oriented syntax provided by the Java Native API: Class.getfield/getdeclaredfields/getmethod ()/getannotation to DSL syntax, Access field/method/ Anntation's code volume is greatly reduced, more importantly, easy to understand, it can be said that a glance to wear:
Example of the official website, modify the value of field, the Java SDK API code:

//"target" is the object containing the field whose value you want to set. null;for (Field f : target.getClass().getDeclaredFields()) {     //Get all fields DECLARED inside the target object class     if (f.getName().equals("field")) {        toSet = f;    }}ifnull0) {    toSet.setAccessible(true);    toSet.set(target, value);}

Mirror-based API

new Mirror().on(target).set().field(fieldName).withValue(value);

So as to be happy coding!!!

Characteristics

Mirror is available in the general mode:
* Field Read, modify
* Method's Read, call
* constructor read, create objects based on constructor
* Read annatation on class

and CGLIB-NODEP in combination mode to create a Java Proxy, a sentence to be done:

new Mirror().proxify(SomeClass.class)        .interceptingWith(new YourCustomMethodInterceptor());
2 ASSERTJ: Easier to use single-measurement assert

Before I met Assertj, I used the assert api,hamcrest of JUnit and hamcrest to be powerful but not easy to understand. JUnit's API: Hemp Rope for tofu-don't mention it.

ASSERTJ supports objects that specify assertions for Feild, method return values,

// common assertionsassertThat(frodo.getName()).isEqualTo("Frodo");assertThat(frodo).isNotEqualTo(sauron)                 .isIn(fellowshipOfTheRing);// String specific assertionsassertThat(frodo.getName()).startsWith("Fro")                           .endsWith("do")                           .isEqualToIgnoringCase("frodo");

And the traversal of the collection object is supported very well: You can customize the filter condition filters when traversing.

// collection specific assertionsList<TolkienCharacter> fellowshipOfTheRing = ...;assertThat(fellowshipOfTheRing).hasSize(9)                               .contains(frodo, sam)                               .doesNotContain(sauron);

There is also an interesting assert feature is continue on erros:soft-assertions

Detailed instructions for use see: joel-costigliola.github.io/assertj/assertj-core-features-highlight.html

Note
When you introduce MAVEN dependencies, be aware of the JDK version:

<dependency>  <groupId>Org.assertj</groupId>  <artifactid>Assertj-core</artifactid>  <!--use 3.0.0 for Java 8 projects  <!--2.0.0 required Java 7 --  <!--1.7.1 for Java 6 --  <version>1.7.1</version>  <scope>Test</Scope></Dependency>

The Premium API provides an API-level judgment for objects that assert guava, joda-time, neo4j, and swing.

2 recommended tools to use in the Java coding process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.