< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/Field.java

Print this page




   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import  com.sun.org.apache.bcel.internal.Constants;



  25 import com.sun.org.apache.bcel.internal.generic.Type;
  26 import java.io.*;
  27 
  28 /**
  29  * This class represents the field info structure, i.e., the representation
  30  * for a variable in the class. See JVM specification for details.
  31  *
  32  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  33  */
  34 public final class Field extends FieldOrMethod {




















  35   /**
  36    * Initialize from another object. Note that both objects use the same
  37    * references (shallow copy). Use clone() for a physical copy.
  38    */
  39   public Field(Field c) {
  40     super(c);
  41   }
  42 

  43   /**
  44    * Construct object from file stream.
  45    * @param file Input stream
  46    */
  47   Field(DataInputStream file, ConstantPool constant_pool)
  48        throws IOException, ClassFormatException
  49   {
  50     super(file, constant_pool);
  51   }
  52 

  53   /**
  54    * @param access_flags Access rights of field
  55    * @param name_index Points to field name in constant pool
  56    * @param signature_index Points to encoded signature
  57    * @param attributes Collection of attributes
  58    * @param constant_pool Array of constants
  59    */
  60   public Field(int access_flags, int name_index, int signature_index,
  61                Attribute[] attributes, ConstantPool constant_pool)
  62   {
  63     super(access_flags, name_index, signature_index, attributes, constant_pool);
  64   }
  65 

  66   /**
  67    * Called by objects that are traversing the nodes of the tree implicitely
  68    * defined by the contents of a Java class. I.e., the hierarchy of methods,
  69    * fields, attributes, etc. spawns a tree of objects.
  70    *
  71    * @param v Visitor object
  72    */
  73   public void accept(Visitor v) {

  74     v.visitField(this);
  75   }
  76 

  77   /**
  78    * @return constant value associated with this field (may be null)
  79    */
  80   public final ConstantValue getConstantValue() {
  81     for(int i=0; i < attributes_count; i++)
  82       if(attributes[i].getTag() == Constants.ATTR_CONSTANT_VALUE)
  83         return (ConstantValue)attributes[i];
  84 

  85     return null;
  86   }
  87 

  88   /**
  89    * Return string representation close to declaration format,
  90    * `public static final short MAX = 100', e.g..
  91    *
  92    * @return String representation of field, including the signature.
  93    */

  94   public final String toString() {
  95     String name, signature, access; // Short cuts to constant pool


  96 
  97     // Get names from constant pool
  98     access    = Utility.accessToString(access_flags);
  99     access    = access.equals("")? "" : (access + " ");
 100     signature = Utility.signatureToString(getSignature());
 101     name      = getName();
 102 
 103     StringBuffer  buf = new StringBuffer(access + signature + " " + name);
 104     ConstantValue cv  = getConstantValue();
 105 
 106     if(cv != null)
 107       buf.append(" = " + cv);
 108 
 109     for(int i=0; i < attributes_count; i++) {
 110       Attribute a = attributes[i];
 111 
 112       if(!(a instanceof ConstantValue))
 113         buf.append(" [" + a.toString() + "]");
 114     }
 115 
 116     return buf.toString();
 117   }
 118 

 119   /**
 120    * @return deep copy of this field
 121    */
 122   public final Field copy(ConstantPool constant_pool) {
 123     return (Field)copy_(constant_pool);
 124   }
 125 

 126   /**
 127    * @return type of field
 128    */
 129   public Type getType() {
 130     return Type.getReturnType(getSignature());
 131   }









































 132 }


   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataInput;
  25 import java.io.IOException;
  26 
  27 import com.sun.org.apache.bcel.internal.Const;
  28 import com.sun.org.apache.bcel.internal.generic.Type;
  29 import com.sun.org.apache.bcel.internal.util.BCELComparator;
  30 
  31 /**
  32  * This class represents the field info structure, i.e., the representation
  33  * for a variable in the class. See JVM specification for details.
  34  *
  35  * @version $Id: Field.java 1749603 2016-06-21 20:50:19Z ggregory $
  36  */
  37 public final class Field extends FieldOrMethod {
  38 
  39     private static BCELComparator bcelComparator = new BCELComparator() {
  40 
  41         @Override
  42         public boolean equals( final Object o1, final Object o2 ) {
  43             final Field THIS = (Field) o1;
  44             final Field THAT = (Field) o2;
  45             return THIS.getName().equals(THAT.getName())
  46                     && THIS.getSignature().equals(THAT.getSignature());
  47         }
  48 
  49 
  50         @Override
  51         public int hashCode( final Object o ) {
  52             final Field THIS = (Field) o;
  53             return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
  54         }
  55     };
  56 
  57 
  58     /**
  59      * Initialize from another object. Note that both objects use the same
  60      * references (shallow copy). Use clone() for a physical copy.
  61      */
  62     public Field(final Field c) {
  63         super(c);
  64     }
  65 
  66 
  67     /**
  68      * Construct object from file stream.
  69      * @param file Input stream
  70      */
  71     Field(final DataInput file, final ConstantPool constant_pool) throws IOException,
  72             ClassFormatException {

  73         super(file, constant_pool);
  74     }
  75 
  76 
  77     /**
  78      * @param access_flags Access rights of field
  79      * @param name_index Points to field name in constant pool
  80      * @param signature_index Points to encoded signature
  81      * @param attributes Collection of attributes
  82      * @param constant_pool Array of constants
  83      */
  84     public Field(final int access_flags, final int name_index, final int signature_index, final Attribute[] attributes,
  85             final ConstantPool constant_pool) {

  86         super(access_flags, name_index, signature_index, attributes, constant_pool);
  87     }
  88 
  89 
  90     /**
  91      * Called by objects that are traversing the nodes of the tree implicitely
  92      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  93      * fields, attributes, etc. spawns a tree of objects.
  94      *
  95      * @param v Visitor object
  96      */
  97     @Override
  98     public void accept( final Visitor v ) {
  99         v.visitField(this);
 100     }
 101 
 102 
 103     /**
 104      * @return constant value associated with this field (may be null)
 105      */
 106     public final ConstantValue getConstantValue() {
 107         for (final Attribute attribute : super.getAttributes()) {
 108             if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
 109                 return (ConstantValue) attribute;
 110             }
 111         }
 112         return null;
 113     }
 114 
 115 
 116     /**
 117      * Return string representation close to declaration format,
 118      * `public static final short MAX = 100', e.g..
 119      *
 120      * @return String representation of field, including the signature.
 121      */
 122     @Override
 123     public final String toString() {
 124         String name;
 125         String signature;
 126         String access; // Short cuts to constant pool
 127 
 128         // Get names from constant pool
 129         access = Utility.accessToString(super.getAccessFlags());
 130         access = access.isEmpty() ? "" : (access + " ");
 131         signature = Utility.signatureToString(getSignature());
 132         name = getName();
 133         final StringBuilder buf = new StringBuilder(64); // CHECKSTYLE IGNORE MagicNumber
 134         buf.append(access).append(signature).append(" ").append(name);
 135         final ConstantValue cv = getConstantValue();
 136         if (cv != null) {
 137             buf.append(" = ").append(cv);
 138         }
 139         for (final Attribute attribute : super.getAttributes()) {
 140             if (!(attribute instanceof ConstantValue)) {
 141                 buf.append(" [").append(attribute).append("]");
 142             }


 143         }

 144         return buf.toString();
 145     }
 146 
 147 
 148     /**
 149      * @return deep copy of this field
 150      */
 151     public final Field copy( final ConstantPool _constant_pool ) {
 152         return (Field) copy_(_constant_pool);
 153     }
 154 
 155 
 156     /**
 157      * @return type of field
 158      */
 159     public Type getType() {
 160         return Type.getReturnType(getSignature());
 161     }
 162 
 163 
 164     /**
 165      * @return Comparison strategy object
 166      */
 167     public static BCELComparator getComparator() {
 168         return bcelComparator;
 169     }
 170 
 171 
 172     /**
 173      * @param comparator Comparison strategy object
 174      */
 175     public static void setComparator( final BCELComparator comparator ) {
 176         bcelComparator = comparator;
 177     }
 178 
 179 
 180     /**
 181      * Return value as defined by given BCELComparator strategy.
 182      * By default two Field objects are said to be equal when
 183      * their names and signatures are equal.
 184      *
 185      * @see java.lang.Object#equals(java.lang.Object)
 186      */
 187     @Override
 188     public boolean equals( final Object obj ) {
 189         return bcelComparator.equals(this, obj);
 190     }
 191 
 192 
 193     /**
 194      * Return value as defined by given BCELComparator strategy.
 195      * By default return the hashcode of the field's name XOR signature.
 196      *
 197      * @see java.lang.Object#hashCode()
 198      */
 199     @Override
 200     public int hashCode() {
 201         return bcelComparator.hashCode(this);
 202     }
 203 }
< prev index next >