1 package attrib4j.examples;
2
3 import java.lang.reflect.Method;
4
5 import attrib4j.Attribute;
6 import attrib4j.Attributes;
7 import attrib4j.Log;
8 import attrib4j.examples.attributes.DebugAttribute;
9 import attrib4j.examples.attributes.TraceAttribute;
10 import attrib4j.examples.attributes.TransactionAttribute;
11
12 /***
13 * A simple example class that demonstrates the use of custom
14 * attributes. The idea is to associate EJB like transaction
15 * attributes to a method, much like would be described in the
16 * deployment descriptor. This example shows inheritance of
17 * attributes through interfaces an abstract base classes.
18 *
19 * @author <a href="mailto:mpollack@speakeasy.org">Mark Pollack</a>
20 *
21 */
22 public class InheritanceTxExample extends AbstractDeposit {
23
24 /***
25 * Create an instance.
26 */
27 public InheritanceTxExample() {
28
29 }
30
31 /***
32 *
33 * @param inputData represents information to use in a transaction.
34 *
35 *
36 * @attrib4j.examples.attributes.Debug(true)
37 *
38 */
39 public void deposit(int accountNbr, long amount) {
40
41 System.out.println("Working inside attribute demarcated transaction.");
42 }
43
44 /***
45 * Show how attributes can be extracted at runtime.
46 * @throws NoSuchMethodException if cannot find declared method on class.
47 */
48 public void extractAttributes() throws NoSuchMethodException {
49
50 //Get a reference to the java.lang.Class that we want
51 //to examine for attributes.
52 Class cls = InheritanceTxExample.class;
53
54 //Get a reference to the method we want to examine for
55 //attributes.
56 String[] depositIJParamTypes = new String[2];
57 depositIJParamTypes[0] = "int";
58 depositIJParamTypes[1] = "long";
59
60 Class[] methodSignature = new Class[] { int.class, long.class };
61 Method depositMethod =
62 cls.getDeclaredMethod("deposit", methodSignature);
63
64 //Get the method level attributes.
65 Attribute[] customMethodAttributes =
66 Attributes.getAttributes(depositMethod);
67
68 System.out.println(
69 "Found "
70 + customMethodAttributes.length
71 + " attributes on method "
72 + " deposit(int,long)");
73
74 //Test for debug attribute.
75 Attribute[] debugAttribute =
76 Attributes.getAttributes(depositMethod, DebugAttribute.class);
77
78 System.out.println(
79 "Debug attribute on deposit(int,long) is "
80 + ((DebugAttribute) debugAttribute[0]).isDebug());
81
82 //Test for transaction attribute.
83 Attribute[] transAttribute =
84 Attributes.getAttributes(depositMethod, TransactionAttribute.class);
85 System.out.println(
86 "Transaction attribute on deposit(int,long) is "
87 + ((TransactionAttribute) transAttribute[0]).getAsString());
88
89 //Test for trace attribute
90 Attribute[] traceAttribute =
91 Attributes.getAttributes(depositMethod, TraceAttribute.class);
92 System.out.println(
93 "Trace attribute on deposit(int, long) is "
94 + ((TraceAttribute) traceAttribute[0]).getLevel());
95
96 }
97
98 /***
99 * Run the example program. It will execute a method that has been
100 * decorated with an attribute, just to show that the functionality has not
101 * been altered. Then it will extract the attributes from the class.
102 * @param args The command line arguments, not used.
103 */
104 public static void main(String[] args) {
105 //turn off debug verbositiy of Attrib4j.
106 System.out.println("\nRunning InheritanceTxExamaple\n");
107 Log.setLevel(Log.WARNING);
108 InheritanceTxExample te = new InheritanceTxExample();
109 te.deposit(1001002, 2000);
110 try {
111 te.extractAttributes();
112 } catch (NoSuchMethodException e) {
113 e.printStackTrace();
114 }
115 }
116 }
This page was automatically generated by Maven