< prev index next >

src/java.base/share/classes/jdk/internal/classfile/impl/Util.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

*** 26,17 ***
--- 26,20 ---
  
  import java.lang.classfile.*;
  import java.lang.classfile.attribute.CodeAttribute;
  import jdk.internal.classfile.components.ClassPrinter;
  import java.lang.classfile.constantpool.ClassEntry;
+ import java.lang.classfile.constantpool.ConstantPool;
+ import java.lang.classfile.constantpool.ConstantPoolBuilder;
  import java.lang.classfile.constantpool.ModuleEntry;
  import java.lang.classfile.constantpool.PoolEntry;
  import java.lang.classfile.constantpool.Utf8Entry;
  import java.lang.constant.ClassDesc;
  import java.lang.constant.MethodTypeDesc;
  import java.lang.constant.ModuleDesc;
  import java.lang.reflect.AccessFlag;
+ import java.lang.reflect.ClassFileFormatVersion;
  import java.util.AbstractList;
  import java.util.Collection;
  import java.util.List;
  import java.util.function.Consumer;
  import java.util.function.Function;

*** 45,10 ***
--- 48,11 ---
  import jdk.internal.constant.ClassOrInterfaceDescImpl;
  import jdk.internal.vm.annotation.ForceInline;
  import jdk.internal.vm.annotation.Stable;
  
  import static java.lang.classfile.ClassFile.ACC_STATIC;
+ import static java.lang.constant.ConstantDescs.INIT_NAME;
  import static jdk.internal.constant.PrimitiveClassDescImpl.CD_double;
  import static jdk.internal.constant.PrimitiveClassDescImpl.CD_long;
  import static jdk.internal.constant.PrimitiveClassDescImpl.CD_void;
  
  /**

*** 198,20 ***
      }
  
      public static int flagsToBits(AccessFlag.Location location, AccessFlag... flags) {
          int i = 0;
          for (AccessFlag f : flags) {
!             if (!f.locations().contains(location)) {
                  throw new IllegalArgumentException("unexpected flag: " + f + " use in target location: " + location);
              }
              i |= f.mask();
          }
          return i;
      }
  
      public static boolean has(AccessFlag.Location location, int flagsMask, AccessFlag flag) {
!         return (flag.mask() & flagsMask) == flag.mask() && flag.locations().contains(location);
      }
  
      public static ClassDesc fieldTypeSymbol(Utf8Entry utf8) {
          return ((AbstractPoolEntry.Utf8EntryImpl) utf8).fieldTypeSymbol();
      }
--- 202,21 ---
      }
  
      public static int flagsToBits(AccessFlag.Location location, AccessFlag... flags) {
          int i = 0;
          for (AccessFlag f : flags) {
!             if (!f.locations().contains(location) && !f.locations(ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES).contains(location)) {
                  throw new IllegalArgumentException("unexpected flag: " + f + " use in target location: " + location);
              }
              i |= f.mask();
          }
          return i;
      }
  
      public static boolean has(AccessFlag.Location location, int flagsMask, AccessFlag flag) {
!         return (flag.mask() & flagsMask) == flag.mask() && (flag.locations().contains(location)
+                 || flag.locations(ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES).contains(location));
      }
  
      public static ClassDesc fieldTypeSymbol(Utf8Entry utf8) {
          return ((AbstractPoolEntry.Utf8EntryImpl) utf8).fieldTypeSymbol();
      }

*** 258,10 ***
--- 263,18 ---
  
      public static boolean isDoubleSlot(ClassDesc desc) {
          return desc == CD_double || desc == CD_long;
      }
  
+     public static boolean checkConstantPoolsCompatible(ConstantPool one, ConstantPool two) {
+         if (one.equals(two))
+             return true;
+         if (one instanceof ConstantPoolBuilder cpb && cpb.canWriteDirect(two))
+             return true;
+         return two instanceof ConstantPoolBuilder cpb && cpb.canWriteDirect(one);
+     }
+ 
      public static void dumpMethod(SplitConstantPool cp,
                                    ClassDesc cls,
                                    String methodName,
                                    MethodTypeDesc methodDesc,
                                    int acc,

*** 301,10 ***
--- 314,21 ---
              }
              dump.accept(" %02x".formatted(bytes[i]));
          }
      }
  
+     public static boolean canSkipMethodInflation(ClassReader cr, MethodInfo method, BufWriterImpl buf) {
+         if (!buf.canWriteDirect(cr)) {
+             return false;
+         }
+         if (method.methodName().equalsString(INIT_NAME) &&
+                 !buf.strictFieldsMatch(((ClassReaderImpl) cr).getContainedClass())) {
+             return false;
+         }
+         return true;
+     }
+ 
      public static void writeListIndices(BufWriter writer, List<? extends PoolEntry> list) {
          writer.writeU2(list.size());
          for (PoolEntry info : list) {
              writer.writeIndex(info);
          }
< prev index next >