View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  import java.io.*;
21  
22  /***
23   * AnnotatedMethodAttribute represents a class level attribute
24   * class file which identifies the level of annotation of the class.
25   */
26  public class AnnotatedMethodAttribute extends ClassAttribute {
27  
28      /* The expected attribute name */
29      public final static String expectedAttrName = "filter.annotatedMethod";
30  
31      /* The expected attribute version */
32      public final static short expectedAttrVersion = 1;
33  
34      /* Bit mask indicating that the class was filter generated */
35      public final static short generatedFlag = 0x1;
36  
37      /* Bit mask indicating that the class was filter annotated */
38      public final static short annotatedFlag = 0x2;
39  
40      /* Bit mask indicating that the class was "repackaged" *//package-summary/html">class="comment"> Bit mask indicating that the class was "repackaged" *//package-summary.html">/* Bit mask indicating that the class was "repackaged" *//package-summary.html">class="comment"> Bit mask indicating that the class was "repackaged" */
41      public final static short modifiedFlag = 0x4;
42  
43      /* The version of the attribute */
44      private short attrVersion;
45  
46      /* Flags associated with the annotation */
47      private short annotationFlags;
48  
49      /* list of targets in the code sequence delimiting inserted instruction
50       * sequences.  Even index targets are a range start (inclusive) and odd
51       * targets represent a range end (exclusive) */
52      private InsnTarget annotationRanges[];
53  
54      /* public accessors */
55  
56      public short getVersion() {
57          return attrVersion;
58      }
59  
60      public void setVersion(short version) {
61          attrVersion = version;
62      }
63  
64      public short getFlags() {
65          return annotationFlags;
66      }
67  
68      public void setFlags(short flags) {
69          annotationFlags = flags;
70      }
71  
72      public InsnTarget[] getAnnotationRanges() {
73          return annotationRanges;
74      }
75  
76      public void setAnnotationRanges(InsnTarget[] ranges) {
77          annotationRanges = ranges;
78      }
79  
80      /***
81       * Constructor
82       */
83      public AnnotatedMethodAttribute(
84  	ConstUtf8 nameAttr, short version, short annFlags,
85  	InsnTarget[] annRanges) {
86          super(nameAttr);
87          attrVersion = version;
88          annotationFlags = annFlags;
89          annotationRanges = annRanges;
90      }
91  
92      /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
93  
94      static AnnotatedMethodAttribute read(
95  	ConstUtf8 attrName, DataInputStream data, CodeEnv env)
96          throws IOException {
97          short version = data.readShort();
98          short annFlags = data.readShort();
99  
100         short nRanges = data.readShort();
101 
102         InsnTarget ranges[] = new InsnTarget[nRanges*2];
103         for (int i=0; i<nRanges; i++) {
104             ranges[i*2] = env.getTarget(data.readShort());
105             ranges[i*2+1] = env.getTarget(data.readShort());
106         }
107         return  new AnnotatedMethodAttribute(attrName, version, annFlags, ranges);
108     }
109 
110     void write(DataOutputStream out) throws IOException {
111         out.writeShort(attrName().getIndex());
112         if (annotationRanges == null)
113             out.writeShort(2);
114         else
115             out.writeShort(4 + 2 * annotationRanges.length);
116         out.writeShort(attrVersion);
117         out.writeShort(annotationFlags);
118         if (annotationRanges == null)
119             out.writeShort(0);
120         else {
121             out.writeShort(annotationRanges.length / 2);
122             for (int i=0; i<annotationRanges.length; i++)
123                 out.writeShort(annotationRanges[i].offset());
124         }
125     }
126 
127     void print(PrintStream out, int indent) {
128         ClassPrint.spaces(out, indent);
129         out.println("version: " + attrVersion);
130         out.println(" flags: " + annotationFlags);
131         if (annotationRanges != null) {
132             out.println("Annotations: ");
133             for (int i=0; i<annotationRanges.length/2; i++) {
134                 ClassPrint.spaces(out, indent+2);
135                 out.println(annotationRanges[i*2] + " to " +
136                             annotationRanges[i*2+1]);
137             }
138         }
139     }
140 }