< prev index next >

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

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2022, 2026, 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

@@ -47,10 +47,11 @@
  
      private final byte[] buffer;
      private final int metadataStart;
      private final int classfileLength;
      private final Function<Utf8Entry, AttributeMapper<?>> attributeMapper;
+     private final int version;
      private final int flags;
      private final int thisClassPos;
      private ClassEntry thisClass;
      private Optional<ClassEntry> superclass;
      private final int constantPoolCount;

@@ -71,13 +72,15 @@
          this.context = context;
          this.attributeMapper = this.context.attributeMapper();
          if (classfileLength < 4 || readInt(0) != 0xCAFEBABE) {
              throw new IllegalArgumentException("Bad magic number");
          }
-         if (readU2(6) > ClassFile.latestMajorVersion()) {
-             throw new IllegalArgumentException("Unsupported class file version: " + readU2(6));
+         int version = readInt(4);
+         if ((version & 0xFFFF) > ClassFile.latestMajorVersion()) {
+             throw new IllegalArgumentException("Unsupported class file version: " + version);
          }
+         this.version = version;
          int constantPoolCount = readU2(8);
          int[] cpOffset = new int[constantPoolCount];
          int p = CP_ITEM_START;
          for (int i = 1; i < cpOffset.length; ++i) {
              cpOffset[i] = p;

@@ -153,10 +156,14 @@
  
      public int thisClassPos() {
          return thisClassPos;
      }
  
+     public int classFileVersion() {
+         return version;
+     }
+ 
      @Override
      public int classfileLength() {
          return classfileLength;
      }
  
< prev index next >