View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.classfile.metadata;
25  
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.Set;
30  import javassist.bytecode.annotation.Annotation;
31  import net.jcip.annotations.Immutable;
32  
33  @Immutable
34  public class AnnotationMetadata {
35  
36      private final String annotationClassName;
37      private final Map<String, String> memberValues;
38  
39      @SuppressWarnings( "unchecked" )
40      AnnotationMetadata( Annotation annotation ) {
41          this.annotationClassName = annotation.getTypeName();
42  
43          Set<Object> memberNames = annotation.getMemberNames();
44          if (memberNames != null) {
45              Map<String, String> members = new HashMap<String, String>(memberNames.size());
46  
47              for (Object rawMemberName : memberNames) {
48                  String memberName = (String)rawMemberName;
49  
50                  members.put(memberName, annotation.getMemberValue(memberName).toString());
51              }
52  
53              this.memberValues = Collections.unmodifiableMap(members);
54          } else {
55              this.memberValues = Collections.emptyMap();
56          }
57      }
58  
59      public String getAnnotationClassName() {
60          return annotationClassName;
61      }
62  
63      public Map<String, String> getMemberValues() {
64          return memberValues;
65      }
66  
67      @Override
68      public String toString() {
69          StringBuilder buff = new StringBuilder();
70  
71          buff.append('@').append(annotationClassName);
72  
73          if (!memberValues.isEmpty()) {
74              buff.append('(');
75  
76              boolean first = true;
77              for (Map.Entry<String, String> entry : memberValues.entrySet()) {
78                  if (first) {
79                      first = false;
80                  } else {
81                      buff.append(", ");
82                  }
83                  buff.append(entry.getKey()).append('=').append(entry.getValue());
84              }
85  
86              buff.append(')');
87          }
88  
89          return buff.toString();
90      }
91  
92  }