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.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import javassist.bytecode.AccessFlag;
30  import javassist.bytecode.ClassFile;
31  
32  public class EnumMetadata extends ClassMetadata {
33  
34      private final List<FieldMetadata> enumFields;
35      private final List<String> values;
36  
37      public EnumMetadata( ClassFile clazz ) {
38          super(clazz);
39  
40          assert (clazz.getAccessFlags() & AccessFlag.ENUM) == AccessFlag.ENUM;
41          List<FieldMetadata> fieldsFromClass = super.getFields();
42  
43          /*
44           * Each enum value is stored as a static field in the class file.  Filter those out and treat them separately.
45           */
46          List<FieldMetadata> enumFields = new ArrayList<FieldMetadata>(fieldsFromClass.size());
47          List<String> values = new ArrayList<String>(fieldsFromClass.size());
48          for (FieldMetadata fieldFromClass : fieldsFromClass) {
49              if (fieldFromClass.getTypeName().equals(getClassName())) {
50                  values.add(fieldFromClass.getName());
51              } else {
52                  enumFields.add(fieldFromClass);
53              }
54          }
55  
56          this.enumFields = Collections.unmodifiableList(enumFields);
57          this.values = Collections.unmodifiableList(values);
58  
59      }
60      
61      public List<String> getValues() {
62          return values;
63      }
64  
65      @Override
66      public List<FieldMetadata> getFields() {
67          return enumFields;
68      }
69  
70      @Override
71      public boolean isEnumeration() {
72          return true;
73      }
74  
75  }