1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package vm.mlvm.meth.share;
  25 
  26 import nsk.share.test.TestUtils;
  27 
  28 public class Argument {
  29 
  30     private final Class<?> type;
  31     private final Object value;
  32     private boolean isPreserved;
  33     private String tag;
  34 
  35     public Argument(Class<?> type, Object value) {
  36         this(type, value, false, "");
  37     }
  38 
  39     public Argument(Class<?> type, Object value, boolean isPreserved, String tag) {
  40         this.type = type;
  41         this.value = value;
  42         this.isPreserved = isPreserved;
  43         this.tag = tag;
  44     }
  45 
  46     public Class<?> getType() {
  47         return this.type;
  48     }
  49 
  50     public Object getValue() {
  51         return this.value;
  52     }
  53 
  54     public void setPreserved(boolean newValue) {
  55         this.isPreserved = newValue;
  56     }
  57 
  58     public boolean isPreserved() {
  59         return this.isPreserved;
  60     }
  61 
  62     public String getTag() {
  63         return this.tag;
  64     }
  65 
  66     public void setTag(String newTag) {
  67         this.tag = newTag;
  68     }
  69 
  70     public static Argument fromValue(Object value) {
  71         return new Argument(value.getClass(), value);
  72     }
  73 
  74     public static Argument fromPrimitiveValue(Object boxedValue) {
  75         TestUtils.assertInCollection(TestTypes.UNBOX_MAP.keySet(), boxedValue.getClass());
  76         return new Argument(TestTypes.UNBOX_MAP.get(boxedValue.getClass()), boxedValue);
  77     }
  78 
  79     public static Argument fromArray(Object[] a) {
  80         boolean isProtected = false;
  81         if ( a.length > 2 && a[2].getClass().equals(Boolean.class) )
  82             isProtected = (Boolean) a[2];
  83 
  84         return new Argument((Class<?>) a[0], a[1], isProtected, "");
  85     }
  86 
  87     @Override
  88     public String toString() {
  89         return getType().getName().replaceFirst("^java.lang.", "") + "="
  90              + (getType().equals(String.class) ? "{" + getValue() + "}" : getValue() == null ? "null" : getValue() )
  91              + ((! getTag().isEmpty() || isPreserved()) ? ("[" + (isPreserved() ? "!" : "") + getTag() + "]") : "");
  92     }
  93 
  94     @Override
  95     public Argument clone() {
  96         return new Argument(getType(), getValue(), isPreserved(), getTag());
  97     }
  98 }