[In-house study session] Basics of Java annotation (2017/11/02) ~ Under construction ~

0. Introduction


Contents

Prerequisite knowledge


Reference books

image

"Effective Java 2nd Edition" Maruzen Publishing


table of contents

  1. Benefits of annotation
  2. Self-made annotation
  3. View the contents of JUnit, lombok

1. Benefits of annotation


Java Annotation Overview

Annotation (English: annotation) is to add related information (metadata) to a certain data as an annotation.


Annotation example (JUnit4)

CalculationTest.java


public class CalculationTest {
    /**Test case 1*/
	@Test
	public void sampleTest1() {
		//...
	}
    /**Test case 2*/
	@Test
	public void sampleTest2() {
		//...
	}    
}

If you can't use annotations in JUnit (JUnit3)

CalculationTest.java


public class CalculationTest extends TestCase{
    /**Test case 1*/
	public void testSample1() {
		//...
	}
    /**Test case 2*/
	public void testSample2() {
		//...
	}    
}

Disadvantages of naming patterns

Don't notice the typo

Parameter values cannot be associated

For example, consider a test method that expects to throw ʻIllegalArgumentException`.

/**How to write JUnit4*/
@Test(expected = IllegalArgumentException.class)
public void sample() {
	//...
}

/**Example when annotation cannot be used*/
public void test_IllegalArgumentException_sample() {
	//...
}    

Comparison of XML and annotation

Servlet can be set with either annotation or XML.

@WebServlet("/Hello")
public class HelloServlet extends HttpServlet {
  //...
}

web.xml


<!--If not use annotation, specify in XML-->
<servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>jp.co.sample.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Hello</servlet-name>
  <url-pattern>/Hello</url-pattern>
</servlet-mapping>

Annotation features compared to XML

⇒ ** Strong against source changes **


Standard library annotation


@Override

Annotation indicating that the method is overridden.

Book.java


public class Book {
	public Book(String title, String author) {
		this.title = title;
		this.author = author;
	}
	String title;
	String author;

	@Override //Compile Error.Correctly equals(Object obj)
	public boolean equals(Book obj) {
		//Null check omitted
		return title.equals(obj.title) && author.equals(obj.author);
	}

}

At compile time, notice that it is not overriding (overloaded).


@Deprecated

From Java9, you can write @Deprecated (since =" 9 ", forRemoval = true).


@SuppressWarnings

Make the IDE suppress warning messages.

@SuppressWarnings( "unchecked" ) //Exclude warnings about type checking
@SuppressWarnings( "javadoc" ) //JavaDoc warning exclusion
public class Book {
	//..
}

value attribute


appendix


Standard annotation


Reference site

http://www.oracle.com/webfolder/technetwork/jp/javamagazine/Java-MA14-Architect-TypeAnnotations.pdf http://www.oracle.com/webfolder/technetwork/jp/javamagazine/Java-MA16-Annotations.pdf

Recommended Posts

[In-house study session] Basics of Java annotation (2017/11/02) ~ Under construction ~
Summary of in-house newcomer study session [Java]
[In-house study session] Java exception handling (2017/04/26)
[In-house study session] Introduction of "Readable Code"
[In-house study session] Java basics-execution without using IDE- (2017/07/06)
History of Java annotation
[In-house study session] Java basics-Lambda expression and Stream API- (2017/07/13)
Basics of character operation (java)
[Java] Creation of original annotation
Summary of Java language basics
Summary of [Java silver study] package
Java method list (memorial) (under construction)
[Study session memo] Java Day Tokyo 2017
[Processing x Java] Construction of development environment
Memorandum of new graduate SES [Java basics]
[# 1 Java] Basics of Java-Major premise before studying-
[day: 5] I summarized the basics of Java
Looking back on the basics of Java
Basics of java basics ② ~ if statement and switch statement ~
[Java] Annotation
[Java] Annotation
Java basics
Java basics
Study session memo: Kansai Java Engineers Association 8/5 --Selenium
Basics of threads and Callable in Java [Beginner]
[Introduction to Java] Basics of java arithmetic (for beginners)