1 /*
   2  * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.bcel.internal.classfile;
  22 
  23 import java.io.DataInput;
  24 import java.io.DataOutputStream;
  25 import java.io.IOException;
  26 
  27 /**
  28  * @since 6.0
  29  * @LastModified: Jan 2020
  30  */
  31 public abstract class ElementValue
  32 {
  33     private final int type;
  34 
  35     private final ConstantPool cpool;
  36 
  37     @Override
  38     public String toString()
  39     {
  40         return stringifyValue();
  41     }
  42 
  43     protected ElementValue(final int type, final ConstantPool cpool)
  44     {
  45         this.type = type;
  46         this.cpool = cpool;
  47     }
  48 
  49     public int getElementValueType()
  50     {
  51         return type;
  52     }
  53 
  54     public abstract String stringifyValue();
  55 
  56     public abstract void dump(DataOutputStream dos) throws IOException;
  57 
  58     public static final byte STRING            = 's';
  59     public static final byte ENUM_CONSTANT     = 'e';
  60     public static final byte CLASS             = 'c';
  61     public static final byte ANNOTATION        = '@';
  62     public static final byte ARRAY             = '[';
  63     public static final byte PRIMITIVE_INT     = 'I';
  64     public static final byte PRIMITIVE_BYTE    = 'B';
  65     public static final byte PRIMITIVE_CHAR    = 'C';
  66     public static final byte PRIMITIVE_DOUBLE  = 'D';
  67     public static final byte PRIMITIVE_FLOAT   = 'F';
  68     public static final byte PRIMITIVE_LONG    = 'J';
  69     public static final byte PRIMITIVE_SHORT   = 'S';
  70     public static final byte PRIMITIVE_BOOLEAN = 'Z';
  71 
  72     public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool) throws IOException
  73     {
  74         final byte type = input.readByte();
  75         switch (type)
  76         {
  77             case PRIMITIVE_BYTE:
  78             case PRIMITIVE_CHAR:
  79             case PRIMITIVE_DOUBLE:
  80             case PRIMITIVE_FLOAT:
  81             case PRIMITIVE_INT:
  82             case PRIMITIVE_LONG:
  83             case PRIMITIVE_SHORT:
  84             case PRIMITIVE_BOOLEAN:
  85             case STRING:
  86                 return new SimpleElementValue(type, input.readUnsignedShort(), cpool);
  87 
  88             case ENUM_CONSTANT:
  89                 return new EnumElementValue(ENUM_CONSTANT, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
  90 
  91             case CLASS:
  92                 return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool);
  93 
  94             case ANNOTATION:
  95                 // TODO isRuntimeVisible
  96                 return new AnnotationElementValue(ANNOTATION, AnnotationEntry.read(input, cpool, false), cpool);
  97 
  98             case ARRAY:
  99                 final int numArrayVals = input.readUnsignedShort();
 100                 final ElementValue[] evalues = new ElementValue[numArrayVals];
 101                 for (int j = 0; j < numArrayVals; j++)
 102                 {
 103                     evalues[j] = ElementValue.readElementValue(input, cpool);
 104                 }
 105                 return new ArrayElementValue(ARRAY, evalues, cpool);
 106 
 107             default:
 108                 throw new RuntimeException("Unexpected element value kind in annotation: " + type);
 109         }
 110     }
 111 
 112     /** @since 6.0 */
 113     final ConstantPool getConstantPool() {
 114         return cpool;
 115     }
 116 
 117     /** @since 6.0 */
 118     final int getType() {
 119         return type;
 120     }
 121 
 122     public String toShortString()
 123     {
 124         return stringifyValue();
 125     }
 126 }