Skip to content

Annotations

stockiNail edited this page Oct 12, 2015 · 2 revisions

Java annotations

(quoted from java annotations definition in Oracle documentation site)

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Annotations have a number of uses, among them:

  • Information for the compiler - Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compile-time and deployment-time processing - Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing - Some annotations are available to be examined at runtime.

This lesson explains where annotations can be used, how to apply annotations, what predefined annotation types are available in the Java Platform, Standard Edition (Java SE API), how type annotations can be used in conjunction with pluggable type systems to write code with stronger type checking, and how to implement repeating annotations.

JEM annotations implementation

Inside JEM, you could find some interesting annotations which can help to develop business logic and simplify heavily the code. The annotations are used to describe plugins and custom code to be executed.

Data descritpions and data sources

JEM provides out-of-the-box 2 annotations to load data descriptions and data sources directly to field of your classes. This allows you do not perform all JNDI call, even if creates a dependency with JEM code.

@AssignDataDescritor

This annotation can assign a stream (input or output, depends on the data description definition in the JCL).

Here is an example:

   @AssignDataDescription("INPUT")
   private InputStream istream = null;
       
   @AssignDataDescription("OUTPUT")
   private OutputStream ostream = null;

In the above example, JEM can assign to the fields the InputStream and OutputStream defined in the JCL, as following (ANT sample):

   <dataDescription name="OUTPUT" disposition="NEW">
      <dataSet name="gdg/jemtest(1)"/>
   </dataDescription>

   <dataDescription name="INPUT" disposition="SHR">
      <dataSet name="gdg/jemtest(0)"/>
   </dataDescription>

Here is a SpringBatch sample:

   <!--
    Data description list INPUT-1 file
   -->
   <beans:bean id="INPUT-1" class="org.pepstock.jem.springbatch.tasks.DataDescription">
      <beans:property name="name" value="INPUT" />
      <beans:property name="disposition" value="SHR" />
      <beans:property name="datasets">
         <beans:list>
            <beans:bean class="org.pepstock.jem.springbatch.tasks.DataSet">
               <beans:property name="name" value="gdg/jemtest(0)" />
            </beans:bean>
         </beans:list>
      </beans:property>
   </beans:bean>

   <!--
    Data description list OUTPUT-1 file
   -->
   <beans:bean id="OUTPUT-1" class="org.pepstock.jem.springbatch.tasks.DataDescription">
      <beans:property name="name" value="OUTPUT" />
      <beans:property name="disposition" value="NEW" />
      <beans:property name="datasets">
         <beans:list>
            <beans:bean class="org.pepstock.jem.springbatch.tasks.DataSet">
               <beans:property name="name" value="gdg/jemtest(1)" />
            </beans:bean>
         </beans:list>
      </beans:property>
   </beans:bean>

Here is a JBoss JBPM sample:

	<ioSpecification id="InputOutputSpecification_1">
		<dataInput id="_2_input" name="jem.dataDescription.INPUT" />
		<dataInput id="_2_output" name="jem.dataDescription.OUTPUT" />
		<inputSet id="InputSet_1">
			<dataInputRefs>_2_input</dataInputRefs>
			<dataInputRefs>_2_output</dataInputRefs>
		</inputSet>
		<outputSet id="OutputSet_1" />
	</ioSpecification>
	<dataInputAssociation id="DataInputAssociation_1">
		<targetRef>_2_input</targetRef>
		<assignment id="Assignment_1">
			<from xs:type="tFormalExpression" id="FormalExpression_1">DSN=gdg/jemtest(0),DISP=SHR</from>
			<to xs:type="tFormalExpression" id="FormalExpression_2">_2_input</to>
		</assignment>
	</dataInputAssociation>
	<dataInputAssociation id="DataInputAssociation_2">
		<targetRef>_2_output</targetRef>
		<assignment id="Assignment_2">
			<from xs:type="tFormalExpression" id="FormalExpression_3">DSN=gdg/jemtest(+1),DISP=NEW</from>
			<to xs:type="tFormalExpression" id="FormalExpression_4">_2_output</to>
		</assignment>
	</dataInputAssociation>

JEM is able to set:

  • all fields with every visibility (private included)
  • if your class is a main java class, the fields must be defined with static modifier

If there is any mistake to cast the fields, an exception will occur.

@AssignDataSource

This annotation can assign a common resource object (the object type depends on the kind of resource you need). For example, if you need a connection to a relational data base, a javax.sql.DataSource will be returned.

Here is an example:

    @AssignDataSource("MYSQL_PRODUCTION")
    private Datasource datasource = null;

In the above example, JEM can assign to the field a Datasource instance, as defined in the JCL, as following (ANT sample):

    <dataSource name="MYSQL_PRODUCTION" resource="MYSQL_JEM_DEFINTION">

Here is a SpringBatch sample:

    <beans:bean id="datasource" class="org.pepstock.jem.springbatch.tasks.DataSource">
       <beans:property name="name" value="MYSQL_PRODUCTION" />
       <beans:property name="resource" value="MYSQL_JEM_DEFINTION" />
    </beans:bean>

Here is a JBoss JBPM sample:

	<ioSpecification id="InputOutputSpecification_1">
		<dataInput id="_2_ds" name="jem.dataSource.MYSQL_PRODUCTION" />
		<inputSet id="InputSet_1">
			<dataInputRefs>_2_ds</dataInputRefs>
		</inputSet>
		<outputSet id="OutputSet_1" />
	</ioSpecification>
	<dataInputAssociation id="DataInputAssociation_3">
		<targetRef>_2_ds</targetRef>
		<assignment id="Assignment_3">
			<from xs:type="tFormalExpression" id="FormalExpression_7">RESOURCE=MYSQL_JEM_DEFINTION</from>
			<to xs:type="tFormalExpression" id="FormalExpression_8">_2_ds</to>
		</assignment>
	</dataInputAssociation>

JEM is able to set:

  • all fields with every visibility (private included)
  • if your class is a main java class, the fields must be defined with static modifier
Clone this wiki locally