Download presentation
Presentation is loading. Please wait.
Published byOsborne Marshall Modified over 8 years ago
1
Java Standard Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan http://www.softleader.com.tw Java Annotation Lesson 4 Final
2
Java5.0 Tiger – JDK 1.5 JSR-175 Annotation Facility
3
The value of metadata Documentation Compiler checking Code analysis –vs Java Reflection ?
4
Annotation Basic Marker annotations –@MarkerAnnotation Single-value annotations –@SingleValueAnnotation(“somevalue”) Full annotations –@FullAnnotation(var1=“value1”, var2=“value2”, value3=“value3” )
5
Arrayed Values in Annotations @MyItems( { @Item( name=“pencil”, code=“A12345” ), @Item( name=“pen”, code=“A12888” ), })
6
@Override Annotation public class MyOverride { public MyOverride() { } @Override public String toString() { return “Overrided”; } @Override public int hashCode() { return 12345; }
7
Try compile checking (TypoError) public class MyOverride { public MyOverride() { } @Override public String toString() { return “Overrided”; } @Override public int typoCode() { return 12345; }
8
@Deprecated Annotation public class MyDeprecated { @Deprecated public String asText() { return “Test”; } public String getText() { return “Test”; }
9
@SuppressWarning Annotation public class MySuppressWarnings { public void nonGenericList() { List mylist = new ArrayList(); mylist.add("foo"); } @SuppressWarnings(value={"unchecked"}) public void nonGenericListWithAnnotation() { List mylist = new ArrayList(); mylist.add("foo"); }
10
Custom Annotations
11
To make an annotation @interface To set the attributes To add the default of attributes To specify the target To set retention To add public documentation
12
ElementType ElementType.TYPE ElementType.FIELD ElementType.METHOD ElementType.PARAMETER ElementType.CONSTRUCTOR ElementType.LOCAL_VARIABLE ElementType.ANNOTATION_TYPE ElementType.PACKAGE
13
RetentionPolicy SOURCE –Annotations with this type will be retained only at the source level and will be ignored by the compiler CLASS –Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM RUNTIME –Annotations with this type will be retained by the VM so they can be read only at runtime.
14
@Inherited public @interface parentObject { boolean isInherited() default true; String doSomething default “do something” } @parentObject public class childObject { //… } public class childObject implements parentObject
15
Java Reflect annotation Annotation[] annotations = aClass.getAnnotations(); Annotation[] annotations = method.getDeclaredAnnotations(); Annotation[][] parameterAnnotations = method.getParameterAnnotations();
16
Java6 - J2SE 6.0 Common Annotations JSR-250
17
Common annotations javax.annotation.Generated javax.annotation.Resource javax.annotation.Resources javax.annotation.PostConstruct javax.annotation.PreDestroy javax.annotation.security.RunAs javax.annotation.security.RolesAllowed javax.annotation.security.PermitAll javax.annotation.security.DenyAll javax.annotation.security.DeclareRoles
18
JavaEE 5.0
19
Java EE 5.0 Annotations Common annotations (JSR#250) EJB 3.0 ( JSR#220) –javax.ejb.* –javax.interceptor.* –javax.persistence.* WebServices Metadata 2.0 (JSR#181) –javax.jws.* JAXB 2.0 (JSR#222) –javax.xml.bind.* JAX-WS 2.0 (JSR#224) –javax.xml.ws.*
20
javax.ejb.* javax.ejb.ActivationConfigProperty javax.ejb.ApplicationException javax.ejb.EJB(s) javax.ejb.Init javax.ejb.Local javax.ejb.LocalHome javax.ejb.MessageDriven javax.ejb.PostActivate javax.ejb.PrePassivate
21
javax.ejb.* (cont.) javax.ejb.Remote javax.ejb.RemoteHome javax.ejb.Remove javax.ejb.Stateful javax.ejb.Stateless javax.ejb.Timeout javax.ejb.TransactionAttribute javax.ejb.TransactionManagement
22
EJB 3.0 Basic POJOs and POJIs @Stateless public class HelloWorldBean implements HelloWorld { public void sayHello(String name) { log.info(“Hello ”+name+ “ in EJB 3.0”; } @Remote public interface HelloWorld { public void sayHello(String name); }
23
Interceptors javax.interceptor.AroundInvoke javax.interceptor.ExcludeClassInterceptors javax.interceptor.ExcludeDefaultInterceptors javax.interceptor.Interceptors
24
EJB 3.0 Interceptors public class ProfilerInterceptor { @AroundInvoke public Object profile(InvocationContext inovation) throws Exception { …. } @Interceptors(ProfilerInterceptor.class) public void Ticket bookCar() { // … }
25
JPA ( Java Persistence API ) Hibernate implementation @PersistenceContext EntityManager @Entity
26
javax.jws.* javax.jws.HandlerChain javax.jws.Oneway javax.jws.WebMethod javax.jws.WebParam javax.jws.WebResult javax.jws.WebService javax.jws.soap.InitParam javax.jws.soap.SOAPBinding javax.jws.soap.SOAPMessageHandler(s)
27
JAX-WS 2.0 javax.xml.ws.BindingType javax.xml.ws.RequestWrapper javax.xml.ws.ResponseWrapper javax.xml.ws.ServiceMode javax.xml.ws.WebEndpoint javax.xml.ws.WebFault javax.xml.ws.WebServiceClient javax.xml.ws.WebServiceProvider javax.xml.ws.WebServiceRef(s) javax.xml.ws.addressing.Action javax.xml.ws.addressing.FaultAction
28
JWS Annotations @WebService(serviceName=“ComplexService”, name=“ComplexPortType”, targetnamespace=http://ws.ossf.org)http://ws.ossf.org @SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.USe.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED) public class ComplexImpl { @WebMethod() @WebResult(name=“IntegerOutput”, targetNamespace=http://ws.ossf.org/complex)http://ws.ossf.org/complex public int echoInt( @WebParam( name=“IntegerInput”, targetNamespace=http://ws.ossf.org/complex) int input ) {http://ws.ossf.org/complex return input; } @WebMethod(operationName=“echoComplexType”) @WebResult(name=“Echo…..”) public …. }
29
WebService Client public class HelloClient { @WebServiceRef(wsdlLocation=http://ws.ossf.org/helloService?wsd l)http://ws.ossf.org/helloService?wsd l static HelloService service; public static void main(String[] args) { try { Hello port = service.getHelloPort(); String reponse = port.sayHello(“jini”); System.out.println(response); } catch (Exception ex) { ex.printStackTrace(); }
30
javax.xml.bind.* javax.xml.bind.annotation.XmlAccessorOrder javax.xml.bind.annotation.XmlAccessorType javax.xml.bind.annotation.XmlAnyAttribute javax.xml.bind.annotation.XmlAnyElement javax.xml.bind.annotation.XmlAttachmentRef javax.xml.bind.annotation.XmlAttribute javax.xml.bind.annotation.XmlElement(s) javax.xml.bind.annotation.XmlElementDecl javax.xml.bind.annotation.XmlElementRef(s) javax.xml.bind.annotation.XmlElementWrapper javax.xml.bind.annotation.XmlEnum javax.xml.bind.annotation.XmlEnumValue
31
JAXB
32
javax.xml.bind.* (Cont.) javax.xml.bind.annotation.XmlID javax.xml.bind.annotation.XmlIDREF javax.xml.bind.annotation.XmlInlinebinaryData javax.xml.bind.annotation.XmlList javax.xml.bind.annotation.XmlMimeType javax.xml.bind.annotation.XmlMixed javax.xml.bind.annotation.XmlNs javax.xml.bind.annotation.XmlRegistry javax.xml.bind.annotation.XmlRootElement javax.xml.bind.annotation.XmlSchema javax.xml.bind.annotation.XmlSchemaType(s) javax.xml.bind.annotation.XmlTransient javax.xml.bind.annotation.XmlType javax.xml.bind.annotation.XmlValue javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(s)
33
JAXB marshal Java Object to XML @XmlRootElement public class Movie { private String title; private String genre; private Integer year; } public void testXml() throws Exception { Movie movie = new Movie("Pulp Fiction", "Action", 1994); JAXBContext context = JAXBContext.newInstance(Movie.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(movie, System.out); }
34
JAXB UnMarshal abc @XmlRootElement class Foo { @XmlAttribute String name; @XmlAttribute String content; } Unmarshaller unmarshaller =
35
UnMarshal Default ? a1.xml -> a2.xml -> a3.xml -> abc @XmlRootElement class Foo { @XmlElement(defaultValue=“value”) public String a = null; } Foo foo = unmarshaller.unmarshall(“a1.xml”); Foo foo = unmarshaller.unmarshall(“a2.xml”); Foo foo = unmarshaller.unmarshall(“a3.xml”);
36
UnMarshal Default ? a1.xml -> a2.xml -> a3.xml -> abc @XmlRootElement class Foo { @XmlElement public String a = “value”; } Foo foo = unmarshaller.unmarshall(“a1.xml”); Foo foo = unmarshaller.unmarshall(“a2.xml”); Foo foo = unmarshaller.unmarshall(“a3.xml”);
37
Summary
38
There are lots of standard annotations in Java now ! Easy to learn it and to use it We can define new Annotation by myself JavaSE 5.0 standard Common annotations JavaEE 5.0 standard
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.