src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/EncodedSpeculationReason.java
changeset 58793 81ad1da857f6
equal deleted inserted replaced
58790:5a9dba5a3eeb 58793:81ad1da857f6
       
     1 /*
       
     2  * Copyright (c) 2019, 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 
       
    25 package jdk.vm.ci.meta;
       
    26 
       
    27 import java.util.Arrays;
       
    28 import java.util.function.Supplier;
       
    29 
       
    30 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
       
    31 
       
    32 /**
       
    33  * An implementation of {@link SpeculationReason} based on encoded values.
       
    34  */
       
    35 public class EncodedSpeculationReason implements SpeculationReason {
       
    36     final int groupId;
       
    37     final String groupName;
       
    38     final Object[] context;
       
    39     private SpeculationLog.SpeculationReasonEncoding encoding;
       
    40 
       
    41     public EncodedSpeculationReason(int groupId, String groupName, Object[] context) {
       
    42         this.groupId = groupId;
       
    43         this.groupName = groupName;
       
    44         this.context = context;
       
    45     }
       
    46 
       
    47     @Override
       
    48     public boolean equals(Object obj) {
       
    49         if (obj instanceof EncodedSpeculationReason) {
       
    50             if (obj instanceof EncodedSpeculationReason) {
       
    51                 EncodedSpeculationReason that = (EncodedSpeculationReason) obj;
       
    52                 return this.groupId == that.groupId && Arrays.equals(this.context, that.context);
       
    53             }
       
    54             return false;
       
    55         }
       
    56         return false;
       
    57     }
       
    58 
       
    59     @Override
       
    60     public SpeculationLog.SpeculationReasonEncoding encode(Supplier<SpeculationLog.SpeculationReasonEncoding> encodingSupplier) {
       
    61         if (encoding == null) {
       
    62             encoding = encodingSupplier.get();
       
    63             encoding.addInt(groupId);
       
    64             for (Object o : context) {
       
    65                 if (o == null) {
       
    66                     encoding.addInt(0);
       
    67                 } else {
       
    68                     addNonNullObject(encoding, o);
       
    69                 }
       
    70             }
       
    71         }
       
    72         return encoding;
       
    73     }
       
    74 
       
    75     static void addNonNullObject(SpeculationLog.SpeculationReasonEncoding encoding, Object o) {
       
    76         Class<? extends Object> c = o.getClass();
       
    77         if (c == String.class) {
       
    78             encoding.addString((String) o);
       
    79         } else if (c == Byte.class) {
       
    80             encoding.addByte((Byte) o);
       
    81         } else if (c == Short.class) {
       
    82             encoding.addShort((Short) o);
       
    83         } else if (c == Character.class) {
       
    84             encoding.addShort((Character) o);
       
    85         } else if (c == Integer.class) {
       
    86             encoding.addInt((Integer) o);
       
    87         } else if (c == Long.class) {
       
    88             encoding.addLong((Long) o);
       
    89         } else if (c == Float.class) {
       
    90             encoding.addInt(Float.floatToRawIntBits((Float) o));
       
    91         } else if (c == Double.class) {
       
    92             encoding.addLong(Double.doubleToRawLongBits((Double) o));
       
    93         } else if (o instanceof Enum) {
       
    94             encoding.addInt(((Enum<?>) o).ordinal());
       
    95         } else if (o instanceof ResolvedJavaMethod) {
       
    96             encoding.addMethod((ResolvedJavaMethod) o);
       
    97         } else if (o instanceof ResolvedJavaType) {
       
    98             encoding.addType((ResolvedJavaType) o);
       
    99         } else if (o instanceof ResolvedJavaField) {
       
   100             encoding.addField((ResolvedJavaField) o);
       
   101         } else {
       
   102             throw new IllegalArgumentException("Unsupported type for encoding: " + c.getName());
       
   103         }
       
   104     }
       
   105 
       
   106     @Override
       
   107     public int hashCode() {
       
   108         return groupId + Arrays.hashCode(this.context);
       
   109     }
       
   110 
       
   111     @Override
       
   112     public String toString() {
       
   113         return String.format("%s@%d%s", groupName, groupId, Arrays.toString(context));
       
   114     }
       
   115 }