KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > classfmt > AnnotationMethodInfo


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.classfmt;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.codegen.AttributeNamesConstants;
15
16 public class AnnotationMethodInfo extends MethodInfo {
17     protected Object JavaDoc defaultValue = null;
18
19 public static MethodInfo createAnnotationMethod(byte classFileBytes[], int offsets[], int offset) {
20     MethodInfo methodInfo = new MethodInfo(classFileBytes, offsets, offset);
21     int attributesCount = methodInfo.u2At(6);
22     int readOffset = 8;
23     AnnotationInfo[] annotations = null;
24     Object JavaDoc defaultValue = null;
25     for (int i = 0; i < attributesCount; i++) {
26         // check the name of each attribute
27
int utf8Offset = methodInfo.constantPoolOffsets[methodInfo.u2At(readOffset)] - methodInfo.structOffset;
28         char[] attributeName = methodInfo.utf8At(utf8Offset + 3, methodInfo.u2At(utf8Offset + 1));
29         if (attributeName.length > 0) {
30             switch(attributeName[0]) {
31                 case 'A':
32                     if (CharOperation.equals(attributeName, AttributeNamesConstants.AnnotationDefaultName)) {
33                         // readOffset + 6 so the offset is at the start of the 'member_value' entry
34
// u2 attribute_name_index + u4 attribute_length = + 6
35
AnnotationInfo info =
36                             new AnnotationInfo(methodInfo.reference, methodInfo.constantPoolOffsets, readOffset + 6 + methodInfo.structOffset);
37                         defaultValue = info.decodeDefaultValue();
38                     }
39                     break;
40                 case 'S' :
41                     if (CharOperation.equals(AttributeNamesConstants.SignatureName, attributeName))
42                         methodInfo.signatureUtf8Offset = methodInfo.constantPoolOffsets[methodInfo.u2At(readOffset + 6)] - methodInfo.structOffset;
43                     break;
44                 case 'R' :
45                     AnnotationInfo[] methodAnnotations = null;
46                     if (CharOperation.equals(attributeName, AttributeNamesConstants.RuntimeVisibleAnnotationsName)) {
47                         methodAnnotations = decodeMethodAnnotations(readOffset, true, methodInfo);
48                     } else if (CharOperation.equals(attributeName, AttributeNamesConstants.RuntimeInvisibleAnnotationsName)) {
49                         methodAnnotations = decodeMethodAnnotations(readOffset, false, methodInfo);
50                     }
51                     if (methodAnnotations != null) {
52                         if (annotations == null) {
53                             annotations = methodAnnotations;
54                         } else {
55                             int length = annotations.length;
56                             AnnotationInfo[] newAnnotations = new AnnotationInfo[length + methodAnnotations.length];
57                             System.arraycopy(annotations, 0, newAnnotations, 0, length);
58                             System.arraycopy(methodAnnotations, 0, newAnnotations, length, methodAnnotations.length);
59                             annotations = newAnnotations;
60                         }
61                     }
62                     break;
63             }
64         }
65         readOffset += (6 + methodInfo.u4At(readOffset + 2));
66     }
67     methodInfo.attributeBytes = readOffset;
68
69     if (defaultValue != null) {
70         if (annotations != null) {
71             return new AnnotationMethodInfoWithAnnotations(methodInfo, defaultValue, annotations);
72         }
73         return new AnnotationMethodInfo(methodInfo, defaultValue);
74     }
75     if (annotations != null)
76         return new MethodInfoWithAnnotations(methodInfo, annotations);
77     return methodInfo;
78 }
79
80 AnnotationMethodInfo(MethodInfo methodInfo, Object JavaDoc defaultValue) {
81     super(methodInfo.reference, methodInfo.constantPoolOffsets, methodInfo.structOffset);
82     this.defaultValue = defaultValue;
83
84     this.accessFlags = methodInfo.accessFlags;
85     this.attributeBytes = methodInfo.attributeBytes;
86     this.descriptor = methodInfo.descriptor;
87     this.exceptionNames = methodInfo.exceptionNames;
88     this.name = methodInfo.name;
89     this.signature = methodInfo.signature;
90     this.signatureUtf8Offset = methodInfo.signatureUtf8Offset;
91     this.tagBits = methodInfo.tagBits;
92 }
93 public Object JavaDoc getDefaultValue() {
94     return this.defaultValue;
95 }
96 protected void toStringContent(StringBuffer JavaDoc buffer) {
97     super.toStringContent(buffer);
98     if (this.defaultValue != null) {
99         buffer.append(" default "); //$NON-NLS-1$
100
if (this.defaultValue instanceof Object JavaDoc[]) {
101             buffer.append('{');
102             Object JavaDoc[] elements = (Object JavaDoc[]) this.defaultValue;
103             for (int i = 0, len = elements.length; i < len; i++) {
104                 if (i > 0)
105                     buffer.append(", "); //$NON-NLS-1$
106
buffer.append(elements[i]);
107             }
108             buffer.append('}');
109         } else {
110             buffer.append(this.defaultValue);
111         }
112         buffer.append('\n');
113     }
114 }
115 }
116
Popular Tags