View Javadoc

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