├── settings.gradle.kts ├── .gitignore ├── src ├── main │ └── java │ │ ├── org │ │ └── glavo │ │ │ └── classfile │ │ │ ├── jdk │ │ │ ├── package-info.java │ │ │ ├── ArrayUtils.java │ │ │ ├── JavaLangAccessUtils.java │ │ │ ├── ClassDescUtils.java │ │ │ ├── JdkUtils.java │ │ │ └── CollectionUtils.java │ │ │ ├── constant │ │ │ ├── package-info.java │ │ │ ├── ModuleDescImpl.java │ │ │ └── PackageDescImpl.java │ │ │ ├── impl │ │ │ ├── package-info.java │ │ │ ├── TerminalFieldBuilder.java │ │ │ ├── TerminalCodeBuilder.java │ │ │ ├── LabelContext.java │ │ │ ├── TerminalMethodBuilder.java │ │ │ ├── MethodInfo.java │ │ │ ├── AbstractElement.java │ │ │ ├── SuperclassImpl.java │ │ │ ├── BoundLocalVariableType.java │ │ │ ├── ClassFileVersionImpl.java │ │ │ ├── InterfacesImpl.java │ │ │ ├── AbstractDirectBuilder.java │ │ │ ├── BoundLocalVariable.java │ │ │ ├── LineNumberImpl.java │ │ │ ├── ChainedCodeBuilder.java │ │ │ ├── BoundRecordComponentInfo.java │ │ │ └── AttributeHolder.java │ │ │ ├── attribute │ │ │ ├── package-info.java │ │ │ ├── UnknownAttribute.java │ │ │ ├── LineNumberInfo.java │ │ │ ├── SyntheticAttribute.java │ │ │ ├── DeprecatedAttribute.java │ │ │ ├── LocalVariableTypeInfo.java │ │ │ ├── StackMapTableAttribute.java │ │ │ ├── LocalVariableInfo.java │ │ │ └── ModuleHashInfo.java │ │ │ ├── instruction │ │ │ ├── package-info.java │ │ │ ├── NopInstruction.java │ │ │ ├── LabelTarget.java │ │ │ ├── ThrowInstruction.java │ │ │ ├── SwitchCase.java │ │ │ ├── LineNumber.java │ │ │ ├── NewObjectInstruction.java │ │ │ ├── MonitorInstruction.java │ │ │ ├── StackInstruction.java │ │ │ ├── NewPrimitiveArrayInstruction.java │ │ │ ├── NewReferenceArrayInstruction.java │ │ │ ├── ArrayLoadInstruction.java │ │ │ ├── OperatorInstruction.java │ │ │ ├── ArrayStoreInstruction.java │ │ │ ├── IncrementInstruction.java │ │ │ └── LookupSwitchInstruction.java │ │ │ ├── constantpool │ │ │ ├── package-info.java │ │ │ ├── NameAndTypeEntry.java │ │ │ ├── ModuleEntry.java │ │ │ ├── StringEntry.java │ │ │ ├── PackageEntry.java │ │ │ ├── AnnotationConstantValueEntry.java │ │ │ ├── ConstantValueEntry.java │ │ │ ├── FieldRefEntry.java │ │ │ ├── Utf8Entry.java │ │ │ ├── LoadableConstantEntry.java │ │ │ ├── MethodRefEntry.java │ │ │ ├── LongEntry.java │ │ │ ├── DoubleEntry.java │ │ │ ├── FloatEntry.java │ │ │ ├── IntegerEntry.java │ │ │ ├── InterfaceMethodRefEntry.java │ │ │ ├── MethodTypeEntry.java │ │ │ ├── ClassEntry.java │ │ │ ├── PoolEntry.java │ │ │ ├── MemberRefEntry.java │ │ │ ├── MethodHandleEntry.java │ │ │ └── DynamicConstantPoolEntry.java │ │ │ ├── Superclass.java │ │ │ ├── ClassFileElement.java │ │ │ ├── WritableElement.java │ │ │ ├── ClassFileVersion.java │ │ │ ├── CodeElement.java │ │ │ ├── Label.java │ │ │ ├── PseudoInstruction.java │ │ │ ├── FieldElement.java │ │ │ ├── BootstrapMethodEntry.java │ │ │ ├── FieldModel.java │ │ │ ├── CodeModel.java │ │ │ ├── CustomAttribute.java │ │ │ └── MethodModel.java │ │ └── module-info.java ├── test │ └── java │ │ ├── testdata │ │ ├── Pattern3.java │ │ ├── Pattern5.java │ │ ├── Pattern10.java │ │ ├── Pattern4.java │ │ ├── Pattern7.java │ │ ├── Pattern9.java │ │ ├── Pattern1.java │ │ ├── Pattern6.java │ │ ├── Lvt.java │ │ └── Pattern2.java │ │ ├── helpers │ │ └── TestConstants.java │ │ ├── PreviewMinorVersionTest.java │ │ ├── AnnotationModelTest.java │ │ ├── SwapTest.java │ │ └── BuilderParamTest.java └── examples │ └── java │ └── ExperimentalTransformExamples.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .github └── workflows │ ├── release.yml │ └── check.yml ├── SYNC.md └── README.md /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "classfile" 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.gradle 2 | /.idea 3 | /build 4 | /*.class 5 | /gradle.properties -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.glavo.classfile.jdk; -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glavo/classfile/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constant/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.glavo.classfile.constant; -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal implementation of the Class-File API. 3 | */ 4 | package org.glavo.classfile.impl; -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module org.glavo.classfile { 2 | exports org.glavo.classfile; 3 | exports org.glavo.classfile.attribute; 4 | exports org.glavo.classfile.components; 5 | exports org.glavo.classfile.constantpool; 6 | exports org.glavo.classfile.instruction; 7 | exports org.glavo.classfile.constant; 8 | } -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Create Release 2 | on: 3 | push: 4 | tags: 5 | - "*.*.*" 6 | jobs: 7 | release: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | contents: write 11 | steps: 12 | - uses: softprops/action-gh-release@v1 13 | with: 14 | body: | 15 | [Changelog](https://github.com/Glavo/classfile/blob/main/CHANGELOG.md) 16 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package org.glavo.classfile.jdk; 2 | 3 | public final class ArrayUtils { 4 | public static int signedHashCode(int result, byte[] a, int fromIndex, int length) { 5 | int end = fromIndex + length; 6 | for (int i = fromIndex; i < end; i++) { 7 | result = 31 * result + (a[i] & 0xff); 8 | } 9 | return result; 10 | } 11 | 12 | private ArrayUtils() { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /SYNC.md: -------------------------------------------------------------------------------- 1 | # Synchronize with upstream 2 | 3 | This repository is updated synchronously with the [OpenJDK](https://github.com/openjdk/jdk) source code. 4 | 5 | The directories that need to be synchronized are: 6 | 7 | * Source file directory: 8 | * `src/java.base/share/classes/java/lang/classfile`: https://github.com/openjdk/jdk/commits/master/src/java.base/share/classes/java/lang/classfile/ 9 | * `src/java.base/share/classes/jdk/internal/classfile`: https://github.com/openjdk/jdk/commits/master/src/java.base/share/classes/jdk/internal/classfile/ 10 | * Test file directory 11 | * `test/jdk/jdk/classfile`: https://github.com/openjdk/jdk/commits/master/test/jdk/jdk/classfile 12 | -------------------------------------------------------------------------------- /.github/workflows/check.yml: -------------------------------------------------------------------------------- 1 | name: Gradle Check 2 | on: 3 | push: 4 | branches: 5 | - main 6 | paths-ignore: 7 | - '**/*.md' 8 | pull_request: 9 | branches: 10 | - main 11 | jobs: 12 | gradle-check: 13 | runs-on: ubuntu-20.04 14 | steps: 15 | - uses: actions/checkout@v3 16 | - uses: actions/setup-java@v3 17 | with: 18 | distribution: 'temurin' 19 | java-version: '17' 20 | - name: Run tests 21 | uses: gradle/gradle-build-action@v2 22 | with: 23 | arguments: check jacocoTestReport --info --no-daemon --stacktrace 24 | - name: Upload coverage to Codecov 25 | uses: codecov/codecov-action@v3 26 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/JavaLangAccessUtils.java: -------------------------------------------------------------------------------- 1 | package org.glavo.classfile.jdk; 2 | 3 | public final class JavaLangAccessUtils { 4 | public static int countPositives(byte[] ba, int off, int len) { 5 | int limit = off + len; 6 | for (int i = off; i < limit; i++) { 7 | if (ba[i] < 0) { 8 | return i - off; 9 | } 10 | } 11 | return len; 12 | } 13 | 14 | public static void inflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len) { 15 | for (int i = 0; i < len; i++) { 16 | dst[dstOff++] = (char)(src[srcOff++] & 0xff); 17 | } 18 | } 19 | 20 | private JavaLangAccessUtils() { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/ClassDescUtils.java: -------------------------------------------------------------------------------- 1 | package org.glavo.classfile.jdk; 2 | 3 | import java.lang.constant.ClassDesc; 4 | import java.util.Objects; 5 | 6 | public final class ClassDescUtils { 7 | private static void validateInternalClassName(String name) { 8 | for (int i = 0; i < name.length(); i++) { 9 | char ch = name.charAt(i); 10 | if (ch == ';' || ch == '[' || ch == '.') 11 | throw new IllegalArgumentException("Invalid class name: " + name); 12 | } 13 | } 14 | 15 | public static ClassDesc ofInternalName(String name) { 16 | validateInternalClassName(Objects.requireNonNull(name)); 17 | return ClassDesc.ofDescriptor("L" + name + ";"); 18 | } 19 | 20 | private ClassDescUtils() { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/JdkUtils.java: -------------------------------------------------------------------------------- 1 | package org.glavo.classfile.jdk; 2 | 3 | public class JdkUtils { 4 | 5 | public static final int LATEST_CLASSFILE_MAJOR_VERSION; 6 | public static final int LATEST_CLASSFILE_MINOR_VERSION; 7 | 8 | static { 9 | String classVersion = System.getProperty("java.class.version"); 10 | int idx = classVersion.indexOf('.'); 11 | if (idx > 0) { 12 | LATEST_CLASSFILE_MAJOR_VERSION = Integer.parseInt(classVersion.substring(0, idx)); 13 | LATEST_CLASSFILE_MINOR_VERSION = Integer.parseInt(classVersion.substring(idx + 1)); 14 | } else { 15 | LATEST_CLASSFILE_MAJOR_VERSION = Integer.parseInt(classVersion); 16 | LATEST_CLASSFILE_MINOR_VERSION = 0; 17 | } 18 | } 19 | 20 | private JdkUtils() { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/jdk/CollectionUtils.java: -------------------------------------------------------------------------------- 1 | package org.glavo.classfile.jdk; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | public final class CollectionUtils { 9 | 10 | @SuppressWarnings("unchecked") 11 | public static List listFromTrustedArray(Object[] arr) { 12 | return (List) List.of(arr); 13 | } 14 | 15 | @SuppressWarnings("unchecked") 16 | public static List listFromTrustedArrayNullsAllowed(Object[] arr) { 17 | //noinspection Java9CollectionFactory 18 | return arr.length == 0 19 | ? Collections.emptyList() 20 | : (List) Collections.unmodifiableList(Arrays.asList(arr)); 21 | } 22 | 23 | private static int calculateHashMapCapacity(int numMappings) { 24 | return (int) Math.ceil(numMappings / 0.75); 25 | } 26 | 27 | public static HashMap newHashMap(int numMappings) { 28 | return new HashMap<>(calculateHashMapCapacity(numMappings)); 29 | } 30 | 31 | private CollectionUtils() { 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern3 { 26 | 27 | private String file; 28 | 29 | void troublesCausingMethod() { 30 | String path = null; 31 | String query = null; 32 | this.file = query == null ? path : path + query; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern5 { 26 | 27 | String troublesCausingMethod() { 28 | Object o = null; 29 | String t; 30 | if (o != null || (t = o.toString()) == null) { 31 | t = toString(); 32 | } 33 | return t; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern10.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern10 { 26 | 27 | @SuppressWarnings("rawtypes") 28 | private static void troublesCausingMethod(Object arg) { 29 | boolean b = true; 30 | if (b) { 31 | var v = new byte[0]; 32 | } else { 33 | var v = new int[0]; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern4 { 26 | 27 | static void troublesCausingMethod() { 28 | try { 29 | Object o = null; 30 | try { 31 | o = null; 32 | } catch (Exception e) { 33 | if (o != null) o = null; 34 | } 35 | if (o != null) {} 36 | } catch (Exception e) {} 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/TerminalFieldBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.FieldBuilder; 28 | 29 | public sealed interface TerminalFieldBuilder 30 | extends FieldBuilder 31 | permits DirectFieldBuilder, BufferedFieldBuilder { 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/TerminalCodeBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.CodeBuilder; 28 | 29 | public sealed interface TerminalCodeBuilder extends CodeBuilder 30 | permits DirectCodeBuilder, BufferedCodeBuilder, TransformingCodeBuilder { 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern7.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern7 { 26 | 27 | static void troublesCausingMethod() { 28 | Object r = null; 29 | Boolean e = null; 30 | String x; 31 | String d = null; 32 | if (r instanceof Integer && (x = ((Integer) r).toString()) != null) { 33 | d = x + r.toString(); 34 | } else { 35 | if (e != null) {} 36 | } 37 | d.chars(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | /** 27 | *

Provides interfaces describing classfile attributes for the {@link org.glavo.classfile} library.

28 | * 29 | * The {@code java.lang.classfile.attribute} package contains interfaces describing classfile attributes. 30 | * 31 | * @since 22 32 | */ 33 | package org.glavo.classfile.attribute; 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | /** 27 | *

Provides interfaces describing code instructions for the {@link org.glavo.classfile} library.

28 | * 29 | * The {@code java.lang.classfile.attribute} package contains interfaces describing code instructions. 30 | * 31 | * @since 22 32 | */ 33 | package org.glavo.classfile.instruction; 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/LabelContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.Label; 28 | 29 | public sealed interface LabelContext 30 | permits BufferedCodeBuilder, CodeImpl, DirectCodeBuilder { 31 | Label newLabel(); 32 | Label getLabel(int bci); 33 | void setLabelTarget(Label label, int bci); 34 | int labelToBci(Label label); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constant/ModuleDescImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constant; 26 | 27 | /* 28 | * Implementation of {@code ModuleDesc} 29 | * @param name must have been validated 30 | */ 31 | record ModuleDescImpl(String name) implements ModuleDesc { 32 | 33 | @Override 34 | public String toString() { 35 | return String.format("ModuleDesc[%s]", name()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | /** 27 | *

Provides interfaces describing classfile constant pool entries for the {@link org.glavo.classfile} library.

28 | * 29 | * The {@code java.lang.classfile.constantpool} package contains interfaces describing classfile constant pool entries. 30 | * 31 | * @since 22 32 | */ 33 | package org.glavo.classfile.constantpool; 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/TerminalMethodBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.CodeModel; 28 | import org.glavo.classfile.MethodBuilder; 29 | 30 | public sealed interface TerminalMethodBuilder 31 | extends MethodBuilder 32 | permits BufferedMethodBuilder, DirectMethodBuilder { 33 | BufferedCodeBuilder bufferedCodeBuilder(CodeModel original); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constant/PackageDescImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constant; 26 | 27 | /* 28 | * Implementation of {@code PackageDesc} 29 | * @param internalName must have been validated 30 | */ 31 | record PackageDescImpl(String internalName) implements PackageDesc { 32 | 33 | @Override 34 | public String toString() { 35 | return String.format("PackageDesc[%s]", name()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | import java.lang.reflect.RecordComponent; 26 | import java.util.Optional; 27 | import java.util.Set; 28 | 29 | public class Pattern9 { 30 | 31 | @SuppressWarnings("rawtypes") 32 | private static void troublesCausingMethod(Object arg) { 33 | if (arg instanceof Record) { 34 | for (RecordComponent rc : arg.getClass().getRecordComponents()) {} 35 | } else if (arg instanceof Optional actualOpt) { 36 | } else { 37 | if (arg instanceof Set actualSet) {} 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public final class Pattern1 { 26 | 27 | static void troublesCausingMethod() { 28 | Object[] obj = null; 29 | boolean match; 30 | for (int i = 0; i < obj.length; i++) { 31 | match = false; 32 | for (int j = 0; j < obj.length; j++) { 33 | if (obj[i].equals(obj[j])) { 34 | match = true; 35 | break; 36 | } 37 | } 38 | if (!match) { 39 | return; 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | public class Pattern6 { 26 | 27 | static void troublesCausingMethod() { 28 | boolean b = true; 29 | String s1 = null; 30 | String s2; 31 | if (s1 != null) { 32 | s2 = s1; 33 | } else { 34 | try { 35 | s2 = null; 36 | if (b) {} 37 | s2.equals(null); 38 | } catch (Error ex) { 39 | throw ex; 40 | } 41 | } 42 | if (null == s2) {} 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/testdata/Lvt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | import java.util.*; 25 | 26 | public class Lvt { 27 | public void m(String a) { 28 | int b; 29 | Object c; 30 | char[] d = new char[27]; 31 | 32 | for (int j = 0; j < d.length; j++) { 33 | char x = d[j]; 34 | } 35 | } 36 | 37 | public List n(U u, Class > z) { 38 | var v = new ArrayList(); 39 | 40 | Set> s = new TreeSet<>(); 41 | 42 | for (List f : new ArrayList>()) { 43 | System.out.println(f); 44 | } 45 | 46 | return null; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/helpers/TestConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package helpers; 24 | 25 | import java.lang.constant.ClassDesc; 26 | import java.lang.constant.MethodTypeDesc; 27 | 28 | /** 29 | * TestConstants 30 | */ 31 | public class TestConstants { 32 | public static final ClassDesc CD_System = ClassDesc.of("java.lang.System"); 33 | public static final ClassDesc CD_PrintStream = ClassDesc.of("java.io.PrintStream"); 34 | public static final ClassDesc CD_ArrayList = ClassDesc.of("java.util.ArrayList"); 35 | 36 | public static final MethodTypeDesc MTD_INT_VOID = MethodTypeDesc.ofDescriptor("(I)V"); 37 | public static final MethodTypeDesc MTD_VOID = MethodTypeDesc.ofDescriptor("()V"); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/MethodInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import java.lang.constant.MethodTypeDesc; 28 | import org.glavo.classfile.constantpool.Utf8Entry; 29 | 30 | import static org.glavo.classfile.ClassFile.ACC_STATIC; 31 | 32 | public interface MethodInfo { 33 | Utf8Entry methodName(); 34 | Utf8Entry methodType(); 35 | MethodTypeDesc methodTypeSymbol(); 36 | int methodFlags(); 37 | 38 | default int receiverSlot() { 39 | if ((methodFlags() & ACC_STATIC) != 0) 40 | throw new IllegalStateException("not an instance method"); 41 | return 0; 42 | } 43 | 44 | int parameterSlot(int paramNo); 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/AbstractElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | public abstract class AbstractElement { 28 | public AbstractElement() { } 29 | 30 | public void writeTo(DirectCodeBuilder builder) { 31 | throw new UnsupportedOperationException(); 32 | } 33 | 34 | public void writeTo(DirectClassBuilder builder) { 35 | throw new UnsupportedOperationException(); 36 | } 37 | 38 | public void writeTo(DirectMethodBuilder builder) { 39 | throw new UnsupportedOperationException(); 40 | } 41 | 42 | public void writeTo(DirectFieldBuilder builder) { 43 | throw new UnsupportedOperationException(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/NameAndTypeEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | 29 | /** 30 | * Models a {@code CONSTANT_NameAndType_info} constant in the constant pool of a 31 | * classfile. 32 | * @jvms 4.4.6 The CONSTANT_NameAndType_info Structure 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface NameAndTypeEntry extends PoolEntry 37 | permits AbstractPoolEntry.NameAndTypeEntryImpl { 38 | 39 | /** 40 | * {@return the field or method name} 41 | */ 42 | Utf8Entry name(); 43 | 44 | /** 45 | * {@return the field or method descriptor} 46 | */ 47 | Utf8Entry type(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/ModuleEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.constant.ModuleDesc; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Module_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.11 The CONSTANT_Module_info Structure 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface ModuleEntry extends PoolEntry 38 | permits AbstractPoolEntry.ModuleEntryImpl { 39 | /** 40 | * {@return the name of the module} 41 | */ 42 | Utf8Entry name(); 43 | 44 | /** 45 | * {@return a symbolic descriptor for the module} 46 | */ 47 | ModuleDesc asSymbol(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/StringEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | 29 | /** 30 | * Models a {@code CONSTANT_String_info} constant in the constant pool of a 31 | * classfile. 32 | * @jvms 4.4.3 The CONSTANT_String_info Structure 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface StringEntry 37 | extends ConstantValueEntry 38 | permits AbstractPoolEntry.StringEntryImpl { 39 | /** 40 | * {@return the UTF constant pool entry describing the string contents} 41 | */ 42 | Utf8Entry utf8(); 43 | 44 | /** 45 | * {@return the string value for this entry} 46 | */ 47 | String stringValue(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/PackageEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.constant.PackageDesc; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Package_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.12 The CONSTANT_Package_info Structure 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface PackageEntry extends PoolEntry 38 | permits AbstractPoolEntry.PackageEntryImpl { 39 | /** 40 | * {@return the package name} 41 | */ 42 | Utf8Entry name(); 43 | 44 | /** 45 | * {@return a symbolic descriptor for the package name} 46 | */ 47 | PackageDesc asSymbol(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/AnnotationConstantValueEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ConstantDesc; 28 | 29 | /** 30 | * A constant pool entry that may be used as an annotation constant, 31 | * which includes the four kinds of primitive constants, and UTF8 constants. 32 | * 33 | * @sealedGraph 34 | * @since 22 35 | */ 36 | public sealed interface AnnotationConstantValueEntry extends PoolEntry 37 | permits DoubleEntry, FloatEntry, IntegerEntry, LongEntry, Utf8Entry { 38 | 39 | /** 40 | * {@return the constant value} The constant value will be an {@link Integer}, 41 | * {@link Long}, {@link Float}, {@link Double}, or {@link String}. 42 | */ 43 | ConstantDesc constantValue(); 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/testdata/Pattern2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | package testdata; 24 | 25 | import java.util.List; 26 | import java.util.Map; 27 | 28 | public final class Pattern2 { 29 | 30 | static void troublesCausingMethod() { 31 | String s = null; 32 | Object o; 33 | Map map = null; 34 | List list = null; 35 | if (s == null) return; 36 | for (int i = 0; i < 10; i++) { 37 | String key = ""; 38 | if (map.containsKey(key)) { 39 | o = map.get(key); 40 | } else { 41 | o = new Object(); 42 | map.put(key, o); 43 | } 44 | Object bais = new Object(); 45 | try { 46 | list.add(o.toString()); 47 | } catch (Exception ce) { 48 | throw ce; 49 | } 50 | bais.toString(); 51 | } 52 | if (list != null) {} 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/UnknownAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile.attribute; 27 | 28 | import org.glavo.classfile.Attribute; 29 | import org.glavo.classfile.ClassElement; 30 | import org.glavo.classfile.FieldElement; 31 | import org.glavo.classfile.MethodElement; 32 | import org.glavo.classfile.impl.BoundAttribute; 33 | 34 | /** 35 | * Models an unknown attribute on a class, method, or field. 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface UnknownAttribute 40 | extends Attribute, 41 | ClassElement, MethodElement, FieldElement 42 | permits BoundAttribute.BoundUnknownAttribute { 43 | 44 | /** 45 | * {@return the uninterpreted contents of the attribute payload} 46 | */ 47 | byte[] contents(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/Superclass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.constantpool.ClassEntry; 28 | import org.glavo.classfile.impl.SuperclassImpl; 29 | 30 | /** 31 | * Models the superclass of a class. Delivered as a {@link 32 | * ClassElement} when traversing a {@link ClassModel}. 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface Superclass 37 | extends ClassElement 38 | permits SuperclassImpl { 39 | 40 | /** {@return the superclass} */ 41 | ClassEntry superclassEntry(); 42 | 43 | /** 44 | * {@return a {@linkplain Superclass} element} 45 | * @param superclassEntry the superclass 46 | */ 47 | static Superclass of(ClassEntry superclassEntry) { 48 | return new SuperclassImpl(superclassEntry); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/ClassFileElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | 28 | /** 29 | * Immutable model for a portion of (or the entirety of) a classfile. Elements 30 | * that model parts of the classfile that have attributes will implement {@link 31 | * AttributedElement}; elements that model complex parts of the classfile that 32 | * themselves contain their own child elements will implement {@link 33 | * CompoundElement}. Elements specific to various locations in the classfile 34 | * will implement {@link ClassElement}, {@link MethodElement}, etc. 35 | * 36 | * @sealedGraph 37 | * @since 22 38 | */ 39 | public sealed interface ClassFileElement 40 | permits AttributedElement, CompoundElement, WritableElement, 41 | ClassElement, CodeElement, FieldElement, MethodElement { 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/ConstantValueEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ConstantDesc; 28 | 29 | /** 30 | * Models a constant pool entry that can be used as the constant in a 31 | * {@code ConstantValue} attribute; this includes the four primitive constant 32 | * types and {@linkplain String} constants. 33 | * 34 | * @sealedGraph 35 | * @since 22 36 | */ 37 | public sealed interface ConstantValueEntry extends LoadableConstantEntry 38 | permits DoubleEntry, FloatEntry, IntegerEntry, LongEntry, StringEntry { 39 | 40 | /** 41 | * {@return the constant value} The constant value will be an {@link Integer}, 42 | * {@link Long}, {@link Float}, {@link Double}, or {@link String}. 43 | */ 44 | @Override 45 | ConstantDesc constantValue(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/FieldRefEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | import org.glavo.classfile.impl.Util; 29 | import java.lang.constant.ClassDesc; 30 | 31 | /** 32 | * Models a {@code CONSTANT_Fieldref_info} constant in the constant pool of a 33 | * classfile. 34 | * @jvms 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures 35 | * 36 | * @since 22 37 | */ 38 | public sealed interface FieldRefEntry extends MemberRefEntry 39 | permits AbstractPoolEntry.FieldRefEntryImpl { 40 | 41 | /** 42 | * {@return a symbolic descriptor for the field's type} 43 | */ 44 | default ClassDesc typeSymbol() { 45 | return Util.fieldTypeSymbol(nameAndType()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/Utf8Entry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | 29 | /** 30 | * Models a {@code CONSTANT_UTF8_info} constant in the constant pool of a 31 | * classfile. 32 | * @jvms 4.4.7 The CONSTANT_Utf8_info Structure 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface Utf8Entry 37 | extends CharSequence, AnnotationConstantValueEntry 38 | permits AbstractPoolEntry.Utf8EntryImpl { 39 | 40 | /** 41 | * {@return the string value for this entry} 42 | */ 43 | String stringValue(); 44 | 45 | /** 46 | * {@return whether this entry describes the same string as the provided string} 47 | * 48 | * @param s the string to compare to 49 | */ 50 | boolean equalsString(String s); 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/LoadableConstantEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ConstantDesc; 28 | import org.glavo.classfile.TypeKind; 29 | 30 | /** 31 | * Marker interface for constant pool entries suitable for loading via the 32 | * {@code LDC} instructions. 33 | * 34 | * @sealedGraph 35 | * @since 22 36 | */ 37 | public sealed interface LoadableConstantEntry extends PoolEntry 38 | permits ClassEntry, ConstantDynamicEntry, ConstantValueEntry, MethodHandleEntry, MethodTypeEntry { 39 | 40 | /** 41 | * {@return the constant described by this entry} 42 | */ 43 | ConstantDesc constantValue(); 44 | 45 | /** 46 | * {@return the type of the constant} 47 | */ 48 | default TypeKind typeKind() { 49 | return TypeKind.ReferenceType; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/MethodRefEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | import org.glavo.classfile.impl.Util; 29 | import java.lang.constant.MethodTypeDesc; 30 | 31 | /** 32 | * Models a {@code CONSTANT_MethodRef_info} constant in the constant pool of a 33 | * classfile. 34 | * @jvms 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures 35 | * 36 | * @since 22 37 | */ 38 | public sealed interface MethodRefEntry extends MemberRefEntry 39 | permits AbstractPoolEntry.MethodRefEntryImpl { 40 | 41 | /** 42 | * {@return a symbolic descriptor for the method's type} 43 | */ 44 | default MethodTypeDesc typeSymbol() { 45 | return Util.methodTypeSymbol(nameAndType()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/LongEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.TypeKind; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Long_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.5 The CONSTANT_Long_info and CONSTANT_Double_info Structures 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface LongEntry 38 | extends AnnotationConstantValueEntry, ConstantValueEntry 39 | permits AbstractPoolEntry.LongEntryImpl { 40 | 41 | /** 42 | * {@return the long value} 43 | */ 44 | long longValue(); 45 | 46 | /** 47 | * {@return the type of the constant} 48 | */ 49 | @Override 50 | default TypeKind typeKind() { 51 | return TypeKind.LongType; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/NopInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.impl.AbstractInstruction; 31 | 32 | /** 33 | * Models a {@code nop} invocation instruction in the {@code code} 34 | * array of a {@code Code} attribute. Delivered as a {@link CodeElement} 35 | * when traversing the elements of a {@link CodeModel}. 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface NopInstruction extends Instruction 40 | permits AbstractInstruction.UnboundNopInstruction { 41 | /** 42 | * {@return a no-op instruction} 43 | */ 44 | static NopInstruction of() { 45 | return new AbstractInstruction.UnboundNopInstruction(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/LabelTarget.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Label; 30 | import org.glavo.classfile.PseudoInstruction; 31 | import org.glavo.classfile.impl.LabelImpl; 32 | 33 | /** 34 | * A pseudo-instruction which indicates that the specified label corresponds to 35 | * the current position in the {@code Code} attribute. Delivered as a {@link 36 | * CodeElement} during traversal of the elements of a {@link CodeModel}. 37 | * 38 | * @see PseudoInstruction 39 | * 40 | * @since 22 41 | */ 42 | public sealed interface LabelTarget extends PseudoInstruction 43 | permits LabelImpl { 44 | 45 | /** 46 | * {@return the label corresponding to this target} 47 | */ 48 | Label label(); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/ThrowInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.impl.AbstractInstruction; 31 | 32 | /** 33 | * Models an {@code athrow} instruction in the {@code code} array of a 34 | * {@code Code} attribute. Delivered as a {@link CodeElement} when traversing 35 | * the elements of a {@link CodeModel}. 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface ThrowInstruction extends Instruction 40 | permits AbstractInstruction.UnboundThrowInstruction { 41 | 42 | /** 43 | * {@return a throw instruction} 44 | */ 45 | static ThrowInstruction of() { 46 | return new AbstractInstruction.UnboundThrowInstruction(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/DoubleEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.TypeKind; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Double_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.5 The CONSTANT_Long_info and CONSTANT_Double_info Structures 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface DoubleEntry 38 | extends AnnotationConstantValueEntry, ConstantValueEntry 39 | permits AbstractPoolEntry.DoubleEntryImpl { 40 | 41 | /** 42 | * {@return the double value} 43 | */ 44 | double doubleValue(); 45 | 46 | /** 47 | * {@return the type of the constant} 48 | */ 49 | @Override 50 | default TypeKind typeKind() { 51 | return TypeKind.DoubleType; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/FloatEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.TypeKind; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Float_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface FloatEntry 38 | extends AnnotationConstantValueEntry, ConstantValueEntry 39 | permits AbstractPoolEntry.FloatEntryImpl { 40 | 41 | /** 42 | * {@return the float value} 43 | */ 44 | 45 | float floatValue(); 46 | 47 | /** 48 | * {@return the type of the constant} 49 | */ 50 | @Override 51 | default TypeKind typeKind() { 52 | return TypeKind.FloatType; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/IntegerEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.TypeKind; 28 | import org.glavo.classfile.impl.AbstractPoolEntry; 29 | 30 | /** 31 | * Models a {@code CONSTANT_Integer_info} constant in the constant pool of a 32 | * classfile. 33 | * @jvms 4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface IntegerEntry 38 | extends AnnotationConstantValueEntry, ConstantValueEntry 39 | permits AbstractPoolEntry.IntegerEntryImpl { 40 | 41 | /** 42 | * {@return the integer value} 43 | */ 44 | int intValue(); 45 | 46 | /** 47 | * {@return the type of the constant} 48 | */ 49 | @Override 50 | default TypeKind typeKind() { 51 | return TypeKind.IntType; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/InterfaceMethodRefEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | import org.glavo.classfile.impl.Util; 29 | import java.lang.constant.MethodTypeDesc; 30 | 31 | /** 32 | * Models a {@code CONSTANT_InterfaceMethodRef_info} constant in the constant pool of a 33 | * classfile. 34 | * @jvms 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures 35 | * 36 | * @since 22 37 | */ 38 | public sealed interface InterfaceMethodRefEntry 39 | extends MemberRefEntry 40 | permits AbstractPoolEntry.InterfaceMethodRefEntryImpl { 41 | 42 | /** 43 | * {@return a symbolic descriptor for the interface method's type} 44 | */ 45 | default MethodTypeDesc typeSymbol() { 46 | return Util.methodTypeSymbol(nameAndType()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/MethodTypeEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ConstantDesc; 28 | import java.lang.constant.MethodTypeDesc; 29 | 30 | import org.glavo.classfile.impl.AbstractPoolEntry; 31 | 32 | /** 33 | * Models a {@code CONSTANT_MethodType_info} constant in the constant pool of a 34 | * classfile. 35 | * @jvms 4.4.9 The CONSTANT_MethodType_info Structure 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface MethodTypeEntry 40 | extends LoadableConstantEntry 41 | permits AbstractPoolEntry.MethodTypeEntryImpl { 42 | 43 | @Override 44 | default ConstantDesc constantValue() { 45 | return asSymbol(); 46 | } 47 | 48 | /** 49 | * {@return the constant pool entry describing the method type} 50 | */ 51 | Utf8Entry descriptor(); 52 | 53 | /** 54 | * {@return a symbolic descriptor for the method type} 55 | */ 56 | MethodTypeDesc asSymbol(); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/WritableElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.constantpool.ConstantPoolBuilder; 28 | import org.glavo.classfile.constantpool.PoolEntry; 29 | import org.glavo.classfile.impl.DirectFieldBuilder; 30 | import org.glavo.classfile.impl.DirectMethodBuilder; 31 | 32 | /** 33 | * A classfile element that can encode itself as a stream of bytes in the 34 | * encoding expected by the classfile format. 35 | * 36 | * @param the type of the entity 37 | * 38 | * @sealedGraph 39 | * @since 22 40 | */ 41 | public sealed interface WritableElement extends ClassFileElement 42 | permits Annotation, AnnotationElement, AnnotationValue, Attribute, 43 | PoolEntry, BootstrapMethodEntry, FieldModel, MethodModel, 44 | ConstantPoolBuilder, DirectFieldBuilder, DirectMethodBuilder { 45 | /** 46 | * Writes the element to the specified writer 47 | * 48 | * @param buf the writer 49 | */ 50 | void writeTo(BufWriter buf); 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/SuperclassImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.constantpool.ClassEntry; 28 | import org.glavo.classfile.Superclass; 29 | 30 | import static java.util.Objects.requireNonNull; 31 | 32 | public final class SuperclassImpl 33 | extends AbstractElement 34 | implements Superclass { 35 | private final ClassEntry superclassEntry; 36 | 37 | public SuperclassImpl(ClassEntry superclassEntry) { 38 | requireNonNull(superclassEntry); 39 | this.superclassEntry = superclassEntry; 40 | } 41 | 42 | @Override 43 | public ClassEntry superclassEntry() { 44 | return superclassEntry; 45 | } 46 | 47 | @Override 48 | public void writeTo(DirectClassBuilder builder) { 49 | builder.setSuperclass(superclassEntry); 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return String.format("Superclass[superclassEntry=%s]", superclassEntry.name().stringValue()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/BoundLocalVariableType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.attribute.LocalVariableTypeInfo; 28 | import org.glavo.classfile.constantpool.Utf8Entry; 29 | import org.glavo.classfile.instruction.LocalVariableType; 30 | 31 | public final class BoundLocalVariableType 32 | extends AbstractBoundLocalVariable 33 | implements LocalVariableTypeInfo, 34 | LocalVariableType { 35 | 36 | public BoundLocalVariableType(CodeImpl code, int offset) { 37 | super(code, offset); 38 | } 39 | 40 | @Override 41 | public Utf8Entry signature() { 42 | return secondaryEntry(); 43 | } 44 | 45 | @Override 46 | public void writeTo(DirectCodeBuilder writer) { 47 | writer.addLocalVariableType(this); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.format("LocalVariableType[name=%s, slot=%d, signature=%s]", name().stringValue(), slot(), signature().stringValue()); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/ClassFileVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.impl.ClassFileVersionImpl; 28 | 29 | /** 30 | * Models the classfile version information for a class. Delivered as a {@link 31 | * ClassElement} when traversing the elements of a {@link 32 | * ClassModel}. 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface ClassFileVersion 37 | extends ClassElement 38 | permits ClassFileVersionImpl { 39 | /** 40 | * {@return the major classfile version} 41 | */ 42 | int majorVersion(); 43 | 44 | /** 45 | * {@return the minor classfile version} 46 | */ 47 | int minorVersion(); 48 | 49 | /** 50 | * {@return a {@link ClassFileVersion} element} 51 | * @param majorVersion the major classfile version 52 | * @param minorVersion the minor classfile version 53 | */ 54 | static ClassFileVersion of(int majorVersion, int minorVersion) { 55 | return new ClassFileVersionImpl(majorVersion, minorVersion); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/LineNumberInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.attribute; 26 | 27 | import org.glavo.classfile.impl.UnboundAttribute; 28 | 29 | /** 30 | * Models a single line number in the {@link LineNumberTableAttribute}. 31 | * 32 | * @since 22 33 | */ 34 | public sealed interface LineNumberInfo 35 | permits UnboundAttribute.UnboundLineNumberInfo { 36 | 37 | /** 38 | * {@return the index into the code array at which the code for this line 39 | * begins} 40 | */ 41 | int startPc(); 42 | 43 | /** 44 | * {@return the line number within the original source file} 45 | */ 46 | int lineNumber(); 47 | 48 | /** 49 | * {@return a line number description} 50 | * @param startPc the starting index of the code array for this line 51 | * @param lineNumber the line number within the original source file 52 | */ 53 | public static LineNumberInfo of(int startPc, int lineNumber) { 54 | return new UnboundAttribute.UnboundLineNumberInfo(startPc, lineNumber); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/ClassFileVersionImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.ClassFileVersion; 28 | 29 | public final class ClassFileVersionImpl 30 | extends AbstractElement 31 | implements ClassFileVersion { 32 | private final int majorVersion, minorVersion; 33 | 34 | public ClassFileVersionImpl(int majorVersion, int minorVersion) { 35 | this.majorVersion = majorVersion; 36 | this.minorVersion = minorVersion; 37 | } 38 | 39 | @Override 40 | public int majorVersion() { 41 | return majorVersion; 42 | } 43 | 44 | @Override 45 | public int minorVersion() { 46 | return minorVersion; 47 | } 48 | 49 | @Override 50 | public void writeTo(DirectClassBuilder builder) { 51 | builder.setVersion(majorVersion, minorVersion); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return String.format("ClassFileVersion[majorVersion=%d, minorVersion=%d]", majorVersion, minorVersion); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/ClassEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ClassDesc; 28 | import java.lang.constant.ConstantDesc; 29 | import org.glavo.classfile.impl.AbstractPoolEntry; 30 | 31 | /** 32 | * Models a {@code CONSTANT_Class_info} constant in the constant pool of a 33 | * classfile. 34 | * @jvms 4.4.1 The CONSTANT_Class_info Structure 35 | * 36 | * @since 22 37 | */ 38 | public sealed interface ClassEntry 39 | extends LoadableConstantEntry 40 | permits AbstractPoolEntry.ClassEntryImpl { 41 | 42 | @Override 43 | default ConstantDesc constantValue() { 44 | return asSymbol(); 45 | } 46 | 47 | /** 48 | * {@return the UTF8 constant pool entry for the class name} 49 | */ 50 | Utf8Entry name(); 51 | 52 | /** 53 | * {@return the class name, as an internal binary name} 54 | */ 55 | String asInternalName(); 56 | 57 | /** 58 | * {@return the class name, as a symbolic descriptor} 59 | */ 60 | ClassDesc asSymbol(); 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/PoolEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.WritableElement; 28 | 29 | /** 30 | * Models an entry in the constant pool of a classfile. 31 | * 32 | * @sealedGraph 33 | * @since 22 34 | */ 35 | public sealed interface PoolEntry extends WritableElement 36 | permits AnnotationConstantValueEntry, DynamicConstantPoolEntry, 37 | LoadableConstantEntry, MemberRefEntry, ModuleEntry, NameAndTypeEntry, 38 | PackageEntry { 39 | 40 | /** 41 | * {@return the constant pool this entry is from} 42 | */ 43 | ConstantPool constantPool(); 44 | 45 | /** 46 | * {@return the constant pool tag that describes the type of this entry} 47 | */ 48 | byte tag(); 49 | 50 | /** 51 | * {@return the index within the constant pool corresponding to this entry} 52 | */ 53 | int index(); 54 | 55 | /** 56 | * {@return the number of constant pool slots this entry consumes} 57 | */ 58 | int width(); 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/InterfacesImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import java.util.List; 28 | import java.util.stream.Collectors; 29 | 30 | import org.glavo.classfile.constantpool.ClassEntry; 31 | import org.glavo.classfile.Interfaces; 32 | 33 | public final class InterfacesImpl 34 | extends AbstractElement 35 | implements Interfaces { 36 | private final List interfaces; 37 | 38 | public InterfacesImpl(List interfaces) { 39 | this.interfaces = List.copyOf(interfaces); 40 | } 41 | 42 | @Override 43 | public List interfaces() { 44 | return interfaces; 45 | } 46 | 47 | @Override 48 | public void writeTo(DirectClassBuilder builder) { 49 | builder.setInterfaces(interfaces); 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return String.format("Interfaces[interfaces=%s]", interfaces.stream() 55 | .map(iface -> iface.name().stringValue()) 56 | .collect(Collectors.joining(", "))); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/CodeElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute; 28 | import org.glavo.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; 29 | import org.glavo.classfile.attribute.StackMapTableAttribute; 30 | 31 | /** 32 | * A marker interface for elements that can appear when traversing 33 | * a {@link CodeModel} or be presented to a {@link CodeBuilder}. Code elements 34 | * are either an {@link Instruction}, which models an instruction in the body 35 | * of a method, or a {@link PseudoInstruction}, which models metadata from 36 | * the code attribute, such as line number metadata, local variable metadata, 37 | * exception metadata, label target metadata, etc. 38 | * 39 | * @sealedGraph 40 | * @since 22 41 | */ 42 | public sealed interface CodeElement extends ClassFileElement 43 | permits Instruction, PseudoInstruction, 44 | CustomAttribute, RuntimeVisibleTypeAnnotationsAttribute, RuntimeInvisibleTypeAnnotationsAttribute, 45 | StackMapTableAttribute { 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/SwitchCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.Label; 28 | import org.glavo.classfile.impl.AbstractInstruction; 29 | 30 | /** 31 | * Models a single case in a {@code lookupswitch} or {@code tableswitch} 32 | * instruction. 33 | * 34 | * @see LookupSwitchInstruction 35 | * @see TableSwitchInstruction 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface SwitchCase 40 | permits AbstractInstruction.SwitchCaseImpl { 41 | 42 | /** {@return the integer value corresponding to this case} */ 43 | int caseValue(); 44 | 45 | /** {@return the branch target corresponding to this case} */ 46 | Label target(); 47 | 48 | /** 49 | * Create a {@linkplain SwitchCase} 50 | * 51 | * @param caseValue the integer value for the case 52 | * @param target the branch target for the case 53 | * @return the {@linkplain SwitchCase} 54 | */ 55 | static SwitchCase of(int caseValue, Label target) { 56 | return new AbstractInstruction.SwitchCaseImpl(caseValue, target); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/PreviewMinorVersionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 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 | import org.glavo.classfile.ClassFile; 24 | import org.junit.jupiter.api.Test; 25 | 26 | import java.lang.constant.ClassDesc; 27 | 28 | import static java.lang.constant.ConstantDescs.*; 29 | import static org.glavo.classfile.ClassFile.*; 30 | import static org.junit.jupiter.api.Assertions.*; 31 | 32 | /* 33 | * @test 34 | * @bug 8311172 35 | * @run junit PreviewMinorVersionTest 36 | * @summary Ensures ClassFile.PREVIEW_MINOR_VERSION equals that of classes with 37 | * preview minor version from ClassModel::minorVersion 38 | */ 39 | public class PreviewMinorVersionTest { 40 | 41 | @Test 42 | public void testMinorVersionMatches() { 43 | // compile a class with --enable-preview 44 | // uses Record feature to trigger forcePreview 45 | var cf = ClassFile.of(); 46 | var cd = ClassDesc.of("Test"); 47 | var bytes = cf.build(cd, cb -> cb 48 | .withSuperclass(CD_Object) 49 | // old preview minor version, 50 | // with all bits set to 1 51 | .withVersion(JAVA_17_VERSION, -1) 52 | ); 53 | 54 | var cm = ClassFile.of().parse(bytes); 55 | assertEquals(ClassFile.PREVIEW_MINOR_VERSION, cm.minorVersion()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/Label.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.impl.LabelImpl; 28 | 29 | /** 30 | * A marker for a position within the instructions of a method body. The 31 | * association between a label's identity and the position it represents is 32 | * managed by the entity managing the method body (a {@link CodeModel} or {@link 33 | * CodeBuilder}), not the label itself; this allows the same label to have a 34 | * meaning both in an existing method (as managed by a {@linkplain CodeModel}) 35 | * and in the transformation of that method (as managed by a {@linkplain 36 | * CodeBuilder}), while corresponding to different positions in each. When 37 | * traversing the elements of a {@linkplain CodeModel}, {@linkplain Label} 38 | * markers will be delivered at the position to which they correspond. A label 39 | * can be bound to the current position within a {@linkplain CodeBuilder} via 40 | * {@link CodeBuilder#labelBinding(Label)} or {@link CodeBuilder#with(ClassFileElement)}. 41 | * 42 | * @since 22 43 | */ 44 | public sealed interface Label 45 | permits LabelImpl { 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/MemberRefEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.impl.AbstractPoolEntry; 28 | 29 | /** 30 | * Models a member reference constant in the constant pool of a classfile, 31 | * which includes references to fields, methods, and interface methods. 32 | * 33 | * @sealedGraph 34 | * @since 22 35 | */ 36 | public sealed interface MemberRefEntry extends PoolEntry 37 | permits FieldRefEntry, InterfaceMethodRefEntry, MethodRefEntry, AbstractPoolEntry.AbstractMemberRefEntry { 38 | /** 39 | * {@return the class in which this member ref lives} 40 | */ 41 | ClassEntry owner(); 42 | 43 | /** 44 | * {@return the name and type of the member} 45 | */ 46 | NameAndTypeEntry nameAndType(); 47 | 48 | /** 49 | * {@return the name of the member} 50 | */ 51 | default Utf8Entry name() { 52 | return nameAndType().name(); 53 | } 54 | 55 | /** 56 | * {@return the type of the member} 57 | */ 58 | default Utf8Entry type() { 59 | return nameAndType().type(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/AnnotationModelTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | * @test 26 | * @summary Testing ClassFile annotation model. 27 | * @run junit AnnotationModelTest 28 | */ 29 | import org.glavo.classfile.ClassFile; 30 | import org.glavo.classfile.Attributes; 31 | import org.junit.jupiter.api.Test; 32 | 33 | import java.io.IOException; 34 | import java.net.URI; 35 | import java.nio.file.FileSystem; 36 | import java.nio.file.FileSystems; 37 | import java.nio.file.Files; 38 | 39 | import static org.junit.jupiter.api.Assertions.*; 40 | 41 | class AnnotationModelTest { 42 | private static final FileSystem JRT = FileSystems.getFileSystem(URI.create("jrt:/")); 43 | private static final String testClass = "modules/java.base/java/lang/annotation/Target.class"; 44 | static byte[] fileBytes; 45 | 46 | static { 47 | try { 48 | fileBytes = Files.readAllBytes(JRT.getPath(testClass)); 49 | } catch (IOException e) { 50 | throw new ExceptionInInitializerError(e); 51 | } 52 | } 53 | 54 | @Test 55 | void readAnnos() { 56 | var model = ClassFile.of().parse(fileBytes); 57 | var annotations = model.findAttribute(Attributes.RUNTIME_VISIBLE_ANNOTATIONS).get().annotations(); 58 | 59 | assertEquals(annotations.size(), 3); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/AbstractDirectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import java.util.Optional; 28 | 29 | import org.glavo.classfile.Attribute; 30 | 31 | public class AbstractDirectBuilder { 32 | protected final SplitConstantPool constantPool; 33 | protected final ClassFileImpl context; 34 | protected final AttributeHolder attributes = new AttributeHolder(); 35 | protected M original; 36 | 37 | public AbstractDirectBuilder(SplitConstantPool constantPool, ClassFileImpl context) { 38 | this.constantPool = constantPool; 39 | this.context = context; 40 | } 41 | 42 | public SplitConstantPool constantPool() { 43 | return constantPool; 44 | } 45 | 46 | public Optional original() { 47 | return Optional.ofNullable(original); 48 | } 49 | 50 | public void setOriginal(M original) { 51 | this.original = original; 52 | } 53 | 54 | public void writeAttribute(Attribute a) { 55 | if (Util.isAttributeAllowed(a, context.attributesProcessingOption())) { 56 | attributes.withAttribute(a); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/BoundLocalVariable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile.impl; 27 | 28 | import java.lang.constant.ClassDesc; 29 | import org.glavo.classfile.attribute.LocalVariableInfo; 30 | import org.glavo.classfile.constantpool.Utf8Entry; 31 | import org.glavo.classfile.instruction.LocalVariable; 32 | 33 | public final class BoundLocalVariable 34 | extends AbstractBoundLocalVariable 35 | implements LocalVariableInfo, 36 | LocalVariable { 37 | 38 | public BoundLocalVariable(CodeImpl code, int offset) { 39 | super(code, offset); 40 | } 41 | 42 | @Override 43 | public Utf8Entry type() { 44 | return secondaryEntry(); 45 | } 46 | 47 | @Override 48 | public ClassDesc typeSymbol() { 49 | return ClassDesc.ofDescriptor(type().stringValue()); 50 | } 51 | 52 | @Override 53 | public void writeTo(DirectCodeBuilder writer) { 54 | writer.addLocalVariable(this); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return String.format("LocalVariable[name=%s, slot=%d, type=%s]", name().stringValue(), slot(), type().stringValue()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/LineNumber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.ClassFile; 28 | import org.glavo.classfile.CodeElement; 29 | import org.glavo.classfile.CodeModel; 30 | import org.glavo.classfile.PseudoInstruction; 31 | import org.glavo.classfile.attribute.LineNumberTableAttribute; 32 | import org.glavo.classfile.impl.LineNumberImpl; 33 | 34 | /** 35 | * A pseudo-instruction which models a single entry in the 36 | * {@link LineNumberTableAttribute}. Delivered as a {@link CodeElement} 37 | * during traversal of the elements of a {@link CodeModel}, according to 38 | * the setting of the {@link ClassFile.LineNumbersOption} option. 39 | * 40 | * @see PseudoInstruction 41 | * 42 | * @since 22 43 | */ 44 | public sealed interface LineNumber extends PseudoInstruction 45 | permits LineNumberImpl { 46 | 47 | /** 48 | * {@return the line number} 49 | */ 50 | int line(); 51 | 52 | /** 53 | * {@return a line number pseudo-instruction} 54 | * 55 | * @param line the line number 56 | */ 57 | static LineNumber of(int line) { 58 | return LineNumberImpl.of(line); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/MethodHandleEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import java.lang.constant.ConstantDesc; 28 | import java.lang.constant.DirectMethodHandleDesc; 29 | 30 | import org.glavo.classfile.impl.AbstractPoolEntry; 31 | 32 | /** 33 | * Models a {@code CONSTANT_MethodHandle_info} constant in the constant pool of a 34 | * classfile. 35 | * @jvms 4.4.8 The CONSTANT_MethodHandle_info Structure 36 | * 37 | * @since 22 38 | */ 39 | public sealed interface MethodHandleEntry 40 | extends LoadableConstantEntry 41 | permits AbstractPoolEntry.MethodHandleEntryImpl { 42 | 43 | @Override 44 | default ConstantDesc constantValue() { 45 | return asSymbol(); 46 | } 47 | 48 | /** 49 | * {@return the reference kind of this method handle {@jvms 4.4.8}} 50 | * @see java.lang.invoke.MethodHandleInfo 51 | */ 52 | int kind(); 53 | 54 | /** 55 | * {@return the constant pool entry describing the method} 56 | */ 57 | MemberRefEntry reference(); 58 | 59 | /** 60 | * {@return a symbolic descriptor for this method handle} 61 | */ 62 | DirectMethodHandleDesc asSymbol(); 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/NewObjectInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.constantpool.ClassEntry; 30 | import org.glavo.classfile.Instruction; 31 | import org.glavo.classfile.impl.AbstractInstruction; 32 | 33 | /** 34 | * Models a {@code new} instruction in the {@code code} array of a {@code Code} 35 | * attribute. Delivered as a {@link CodeElement} when traversing the elements 36 | * of a {@link CodeModel}. 37 | * 38 | * @since 22 39 | */ 40 | public sealed interface NewObjectInstruction extends Instruction 41 | permits AbstractInstruction.BoundNewObjectInstruction, AbstractInstruction.UnboundNewObjectInstruction { 42 | 43 | /** 44 | * {@return the type of object to create} 45 | */ 46 | ClassEntry className(); 47 | 48 | /** 49 | * {@return a new object instruction} 50 | * 51 | * @param className the type of object to create 52 | */ 53 | static NewObjectInstruction of(ClassEntry className) { 54 | return new AbstractInstruction.UnboundNewObjectInstruction(className); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/LineNumberImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import org.glavo.classfile.instruction.LineNumber; 28 | 29 | public final class LineNumberImpl 30 | extends AbstractElement 31 | implements LineNumber { 32 | private static final int INTERN_LIMIT = 1000; 33 | private static final LineNumber[] internCache = new LineNumber[INTERN_LIMIT]; 34 | static { 35 | for (int i=0; i consumer; 38 | 39 | public ChainedCodeBuilder(CodeBuilder downstream, 40 | Consumer consumer) { 41 | super(downstream); 42 | this.consumer = consumer; 43 | } 44 | 45 | @Override 46 | public Label startLabel() { 47 | return terminal.startLabel(); 48 | } 49 | 50 | @Override 51 | public Label endLabel() { 52 | return terminal.endLabel(); 53 | } 54 | 55 | @Override 56 | public int allocateLocal(TypeKind typeKind) { 57 | return parent.allocateLocal(typeKind); 58 | } 59 | 60 | @Override 61 | public CodeBuilder with(CodeElement element) { 62 | consumer.accept(element); 63 | return this; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/PseudoInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.attribute.CodeAttribute; 28 | import org.glavo.classfile.instruction.CharacterRange; 29 | import org.glavo.classfile.instruction.ExceptionCatch; 30 | import org.glavo.classfile.instruction.LabelTarget; 31 | import org.glavo.classfile.instruction.LineNumber; 32 | import org.glavo.classfile.instruction.LocalVariable; 33 | import org.glavo.classfile.instruction.LocalVariableType; 34 | import org.glavo.classfile.impl.AbstractPseudoInstruction; 35 | 36 | /** 37 | * Models metadata about a {@link CodeAttribute}, such as entries in the 38 | * exception table, line number table, local variable table, or the mapping 39 | * between instructions and labels. Pseudo-instructions are delivered as part 40 | * of the element stream of a {@link CodeModel}. Delivery of some 41 | * pseudo-instructions can be disabled by modifying the value of classfile 42 | * options (e.g., {@link ClassFile.DebugElementsOption}). 43 | * 44 | * @since 22 45 | */ 46 | public sealed interface PseudoInstruction 47 | extends CodeElement 48 | permits CharacterRange, ExceptionCatch, LabelTarget, LineNumber, LocalVariable, LocalVariableType, AbstractPseudoInstruction { 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/SyntheticAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile.attribute; 27 | 28 | import org.glavo.classfile.Attribute; 29 | import org.glavo.classfile.ClassElement; 30 | import org.glavo.classfile.FieldElement; 31 | import org.glavo.classfile.MethodElement; 32 | import org.glavo.classfile.impl.BoundAttribute; 33 | import org.glavo.classfile.impl.UnboundAttribute; 34 | 35 | /** 36 | * Models the {@code Synthetic} attribute {@jvms 4.7.8}, which can appear on 37 | * classes, methods, and fields. Delivered as a {@link ClassElement}, 38 | * {@link MethodElement}, or {@link FieldElement} when traversing the elements 39 | * of a corresponding model. 40 | *

41 | * The attribute permits multiple instances in a given location. 42 | * 43 | * @since 22 44 | */ 45 | public sealed interface SyntheticAttribute 46 | extends Attribute, 47 | ClassElement, MethodElement, FieldElement 48 | permits BoundAttribute.BoundSyntheticAttribute, UnboundAttribute.UnboundSyntheticAttribute { 49 | 50 | /** 51 | * {@return a {@code Synthetic} attribute} 52 | */ 53 | static SyntheticAttribute of() { 54 | return new UnboundAttribute.UnboundSyntheticAttribute(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/StackInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.Opcode; 31 | import org.glavo.classfile.impl.AbstractInstruction; 32 | import org.glavo.classfile.impl.Util; 33 | 34 | /** 35 | * Models a stack manipulation instruction in the {@code code} array of a 36 | * {@code Code} attribute. Corresponding opcodes will have a {@code kind} of 37 | * {@link Opcode.Kind#STACK}. Delivered as a {@link CodeElement} when 38 | * traversing the elements of a {@link CodeModel}. 39 | * 40 | * @since 22 41 | */ 42 | public sealed interface StackInstruction extends Instruction 43 | permits AbstractInstruction.UnboundStackInstruction { 44 | 45 | /** 46 | * {@return a stack manipulation instruction} 47 | * 48 | * @param op the opcode for the specific type of stack instruction, 49 | * which must be of kind {@link Opcode.Kind#STACK} 50 | */ 51 | static StackInstruction of(Opcode op) { 52 | Util.checkKind(op, Opcode.Kind.STACK); 53 | return new AbstractInstruction.UnboundStackInstruction(op); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/NewPrimitiveArrayInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.TypeKind; 31 | import org.glavo.classfile.impl.AbstractInstruction; 32 | 33 | /** 34 | * Models a {@code newarray} invocation instruction in the {@code code} 35 | * array of a {@code Code} attribute. Delivered as a {@link CodeElement} 36 | * when traversing the elements of a {@link CodeModel}. 37 | * 38 | * @since 22 39 | */ 40 | public sealed interface NewPrimitiveArrayInstruction extends Instruction 41 | permits AbstractInstruction.BoundNewPrimitiveArrayInstruction, 42 | AbstractInstruction.UnboundNewPrimitiveArrayInstruction { 43 | /** 44 | * {@return the component type of the array} 45 | */ 46 | TypeKind typeKind(); 47 | 48 | /** 49 | * {@return a new primitive array instruction} 50 | * 51 | * @param typeKind the component type of the array 52 | */ 53 | static NewPrimitiveArrayInstruction of(TypeKind typeKind) { 54 | return new AbstractInstruction.UnboundNewPrimitiveArrayInstruction(typeKind); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/DeprecatedAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.attribute; 26 | 27 | import org.glavo.classfile.Attribute; 28 | import org.glavo.classfile.ClassElement; 29 | import org.glavo.classfile.FieldElement; 30 | import org.glavo.classfile.MethodElement; 31 | import org.glavo.classfile.impl.BoundAttribute; 32 | import org.glavo.classfile.impl.UnboundAttribute; 33 | 34 | /** 35 | * Models the {@code Deprecated} attribute {@jvms 4.7.15}, which can appear on 36 | * classes, methods, and fields. Delivered as a {@link ClassElement}, 37 | * {@link MethodElement}, or {@link FieldElement} when traversing the elements 38 | * of a corresponding model. 39 | *

40 | * The attribute permits multiple instances in a given location. 41 | * 42 | * @since 22 43 | */ 44 | public sealed interface DeprecatedAttribute 45 | extends Attribute, 46 | ClassElement, MethodElement, FieldElement 47 | permits BoundAttribute.BoundDeprecatedAttribute, 48 | UnboundAttribute.UnboundDeprecatedAttribute { 49 | 50 | /** 51 | * {@return a {@code Deprecated} attribute} 52 | */ 53 | static DeprecatedAttribute of() { 54 | return new UnboundAttribute.UnboundDeprecatedAttribute(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/NewReferenceArrayInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.constantpool.ClassEntry; 30 | import org.glavo.classfile.Instruction; 31 | import org.glavo.classfile.impl.AbstractInstruction; 32 | 33 | /** 34 | * Models a {@code anewarray} invocation instruction in the {@code code} 35 | * array of a {@code Code} attribute. Delivered as a {@link CodeElement} 36 | * when traversing the elements of a {@link CodeModel}. 37 | * 38 | * @since 22 39 | */ 40 | public sealed interface NewReferenceArrayInstruction extends Instruction 41 | permits AbstractInstruction.BoundNewReferenceArrayInstruction, AbstractInstruction.UnboundNewReferenceArrayInstruction { 42 | /** 43 | * {@return the component type of the array} 44 | */ 45 | ClassEntry componentType(); 46 | 47 | /** 48 | * {@return a new reference array instruction} 49 | * 50 | * @param componentType the component type of the array 51 | */ 52 | static NewReferenceArrayInstruction of(ClassEntry componentType) { 53 | return new AbstractInstruction.UnboundNewReferenceArrayInstruction(componentType); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/LocalVariableTypeInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.attribute; 26 | 27 | import org.glavo.classfile.constantpool.Utf8Entry; 28 | import org.glavo.classfile.impl.BoundLocalVariableType; 29 | import org.glavo.classfile.impl.UnboundAttribute; 30 | 31 | /** 32 | * Models a single local variable in the {@link LocalVariableTypeTableAttribute}. 33 | * 34 | * @since 22 35 | */ 36 | public sealed interface LocalVariableTypeInfo 37 | permits UnboundAttribute.UnboundLocalVariableTypeInfo, BoundLocalVariableType { 38 | 39 | /** 40 | * {@return the index into the code array (inclusive) at which the scope of 41 | * this variable begins} 42 | */ 43 | int startPc(); 44 | 45 | /** 46 | * {@return the length of the region of the code array in which this 47 | * variable is in scope.} 48 | */ 49 | int length(); 50 | 51 | /** 52 | * {@return the name of the local variable} 53 | */ 54 | Utf8Entry name(); 55 | 56 | 57 | /** 58 | * {@return the field signature of the local variable} 59 | */ 60 | Utf8Entry signature(); 61 | 62 | /** 63 | * {@return the index into the local variable array of the current frame 64 | * which holds this local variable} 65 | */ 66 | int slot(); 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/BoundRecordComponentInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import java.util.List; 28 | 29 | import org.glavo.classfile.Attribute; 30 | import org.glavo.classfile.ClassReader; 31 | import org.glavo.classfile.attribute.RecordComponentInfo; 32 | import org.glavo.classfile.constantpool.Utf8Entry; 33 | 34 | public final class BoundRecordComponentInfo 35 | implements RecordComponentInfo { 36 | 37 | private final ClassReader reader; 38 | private final int startPos, attributesPos; 39 | private List> attributes; 40 | 41 | public BoundRecordComponentInfo(ClassReader reader, int startPos) { 42 | this.reader = reader; 43 | this.startPos = startPos; 44 | attributesPos = startPos + 4; 45 | } 46 | 47 | @Override 48 | public Utf8Entry name() { 49 | return reader.readUtf8Entry(startPos); 50 | } 51 | 52 | @Override 53 | public Utf8Entry descriptor() { 54 | return reader.readUtf8Entry(startPos + 2); 55 | } 56 | 57 | @Override 58 | public List> attributes() { 59 | if (attributes == null) { 60 | attributes = BoundAttribute.readAttributes(null, reader, attributesPos, reader.customAttributes()); 61 | } 62 | return attributes; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/constantpool/DynamicConstantPoolEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.constantpool; 26 | 27 | import org.glavo.classfile.BootstrapMethodEntry; 28 | 29 | /** 30 | * Models a dynamic constant pool entry, which is either {@link ConstantDynamicEntry} 31 | * or {@link InvokeDynamicEntry}. 32 | * @jvms 4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures 33 | * 34 | * @sealedGraph 35 | * @since 22 36 | */ 37 | public sealed interface DynamicConstantPoolEntry extends PoolEntry 38 | permits ConstantDynamicEntry, InvokeDynamicEntry { 39 | 40 | /** 41 | * {@return the entry in the bootstrap method table for this constant} 42 | */ 43 | BootstrapMethodEntry bootstrap(); 44 | 45 | /** 46 | * {@return index of the entry in the bootstrap method table for this constant} 47 | */ 48 | int bootstrapMethodIndex(); 49 | 50 | /** 51 | * {@return the invocation name and type} 52 | */ 53 | NameAndTypeEntry nameAndType(); 54 | 55 | /** 56 | * {@return the invocation name} 57 | */ 58 | default Utf8Entry name() { 59 | return nameAndType().name(); 60 | } 61 | 62 | /** 63 | * {@return the invocation type} 64 | */ 65 | default Utf8Entry type() { 66 | return nameAndType().type(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/FieldElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | import org.glavo.classfile.attribute.ConstantValueAttribute; 28 | import org.glavo.classfile.attribute.DeprecatedAttribute; 29 | import org.glavo.classfile.attribute.RuntimeInvisibleAnnotationsAttribute; 30 | import org.glavo.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute; 31 | import org.glavo.classfile.attribute.RuntimeVisibleAnnotationsAttribute; 32 | import org.glavo.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; 33 | import org.glavo.classfile.attribute.SignatureAttribute; 34 | import org.glavo.classfile.attribute.SyntheticAttribute; 35 | import org.glavo.classfile.attribute.UnknownAttribute; 36 | 37 | /** 38 | * A marker interface for elements that can appear when traversing 39 | * a {@link FieldModel} or be presented to a {@link FieldBuilder}. 40 | * 41 | * @sealedGraph 42 | * @since 22 43 | */ 44 | public sealed interface FieldElement extends ClassFileElement 45 | permits AccessFlags, 46 | CustomAttribute, ConstantValueAttribute, DeprecatedAttribute, 47 | RuntimeInvisibleAnnotationsAttribute, RuntimeInvisibleTypeAnnotationsAttribute, 48 | RuntimeVisibleAnnotationsAttribute, RuntimeVisibleTypeAnnotationsAttribute, 49 | SignatureAttribute, SyntheticAttribute, UnknownAttribute { 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/BootstrapMethodEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile; 27 | 28 | import java.util.List; 29 | 30 | import org.glavo.classfile.constantpool.ConstantPool; 31 | import org.glavo.classfile.constantpool.LoadableConstantEntry; 32 | import org.glavo.classfile.constantpool.MethodHandleEntry; 33 | import org.glavo.classfile.impl.BootstrapMethodEntryImpl; 34 | 35 | /** 36 | * Models an entry in the bootstrap method table. The bootstrap method table 37 | * is stored in the {@code BootstrapMethods} attribute, but is modeled by 38 | * the {@link ConstantPool}, since the bootstrap method table is logically 39 | * part of the constant pool. 40 | * 41 | * @since 22 42 | */ 43 | public sealed interface BootstrapMethodEntry 44 | extends WritableElement 45 | permits BootstrapMethodEntryImpl { 46 | 47 | /** 48 | * {@return the constant pool associated with this entry} 49 | */ 50 | ConstantPool constantPool(); 51 | 52 | /** 53 | * {@return the index into the bootstrap method table corresponding to this entry} 54 | */ 55 | int bsmIndex(); 56 | 57 | /** 58 | * {@return the bootstrap method} 59 | */ 60 | MethodHandleEntry bootstrapMethod(); 61 | 62 | /** 63 | * {@return the bootstrap arguments} 64 | */ 65 | List arguments(); 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/FieldModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile; 27 | 28 | import java.lang.constant.ClassDesc; 29 | import java.util.Optional; 30 | 31 | import org.glavo.classfile.constantpool.Utf8Entry; 32 | import org.glavo.classfile.impl.BufferedFieldBuilder; 33 | import org.glavo.classfile.impl.FieldImpl; 34 | 35 | /** 36 | * Models a field. The contents of the field can be traversed via 37 | * a streaming view (e.g., {@link #elements()}), or via random access (e.g., 38 | * {@link #flags()}), or by freely mixing the two. 39 | * 40 | * @since 22 41 | */ 42 | public sealed interface FieldModel 43 | extends WritableElement, CompoundElement, AttributedElement, ClassElement 44 | permits BufferedFieldBuilder.Model, FieldImpl { 45 | 46 | /** {@return the access flags} */ 47 | AccessFlags flags(); 48 | 49 | /** {@return the class model this field is a member of, if known} */ 50 | Optional parent(); 51 | 52 | /** {@return the name of this field} */ 53 | Utf8Entry fieldName(); 54 | 55 | /** {@return the field descriptor of this field} */ 56 | Utf8Entry fieldType(); 57 | 58 | /** {@return the field descriptor of this field, as a symbolic descriptor} */ 59 | default ClassDesc fieldTypeSymbol() { 60 | return ClassDesc.ofDescriptor(fieldType().stringValue()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/CodeModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile; 27 | 28 | import java.util.List; 29 | import java.util.Optional; 30 | 31 | import org.glavo.classfile.attribute.CodeAttribute; 32 | import org.glavo.classfile.impl.BufferedCodeBuilder; 33 | import org.glavo.classfile.impl.CodeImpl; 34 | import org.glavo.classfile.instruction.ExceptionCatch; 35 | 36 | /** 37 | * Models the body of a method (the {@code Code} attribute). The instructions 38 | * of the method body are accessed via a streaming view (e.g., {@link 39 | * #elements()}). 40 | * 41 | * @since 22 42 | */ 43 | public sealed interface CodeModel 44 | extends CompoundElement, AttributedElement, MethodElement 45 | permits CodeAttribute, BufferedCodeBuilder.Model, CodeImpl { 46 | 47 | /** 48 | * {@return the maximum size of the local variable table} 49 | */ 50 | int maxLocals(); 51 | 52 | /** 53 | * {@return the maximum size of the operand stack} 54 | */ 55 | int maxStack(); 56 | 57 | /** 58 | * {@return the enclosing method, if known} 59 | */ 60 | Optional parent(); 61 | 62 | /** 63 | * {@return the exception table of the method} The exception table is also 64 | * modeled by {@link ExceptionCatch} elements in the streaming view. 65 | */ 66 | List exceptionHandlers(); 67 | } 68 | -------------------------------------------------------------------------------- /src/examples/java/ExperimentalTransformExamples.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | * @test 26 | * @summary Testing ClassFile ExperimentalTransformExamples compilation. 27 | * @compile ExperimentalTransformExamples.java 28 | */ 29 | import java.net.URI; 30 | import java.nio.file.FileSystem; 31 | import java.nio.file.FileSystems; 32 | 33 | import org.glavo.classfile.*; 34 | import org.glavo.classfile.attribute.RuntimeInvisibleAnnotationsAttribute; 35 | import org.glavo.classfile.attribute.RuntimeVisibleAnnotationsAttribute; 36 | 37 | /** 38 | * ExperimentalTransformExamples 39 | * 40 | */ 41 | public class ExperimentalTransformExamples { 42 | private static final FileSystem JRT = FileSystems.getFileSystem(URI.create("jrt:/")); 43 | 44 | static MethodTransform dropMethodAnnos = (mb, me) -> { 45 | if (!(me instanceof RuntimeVisibleAnnotationsAttribute || me instanceof RuntimeInvisibleAnnotationsAttribute)) 46 | mb.with(me); 47 | }; 48 | 49 | static FieldTransform dropFieldAnnos = (fb, fe) -> { 50 | if (!(fe instanceof RuntimeVisibleAnnotationsAttribute || fe instanceof RuntimeInvisibleAnnotationsAttribute)) 51 | fb.with(fe); 52 | }; 53 | 54 | public byte[] deleteAnnotations(ClassModel cm) { 55 | return ClassFile.of().transform(cm, (cb, ce) -> { 56 | switch (ce) { 57 | case MethodModel m -> cb.transformMethod(m, dropMethodAnnos); 58 | case FieldModel f -> cb.transformField(f, dropFieldAnnos); 59 | default -> cb.with(ce); 60 | } 61 | }); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/impl/AttributeHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.impl; 26 | 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | import org.glavo.classfile.Attribute; 31 | import org.glavo.classfile.AttributeMapper; 32 | import org.glavo.classfile.BufWriter; 33 | 34 | public class AttributeHolder { 35 | private final List> attributes = new ArrayList<>(); 36 | 37 | public > void withAttribute(Attribute a) { 38 | if (a == null) 39 | return; 40 | 41 | @SuppressWarnings("unchecked") 42 | AttributeMapper am = (AttributeMapper) a.attributeMapper(); 43 | if (!am.allowMultiple() && isPresent(am)) { 44 | remove(am); 45 | } 46 | attributes.add(a); 47 | } 48 | 49 | public int size() { 50 | return attributes.size(); 51 | } 52 | 53 | public void writeTo(BufWriter buf) { 54 | buf.writeU2(attributes.size()); 55 | for (Attribute a : attributes) 56 | a.writeTo(buf); 57 | } 58 | 59 | boolean isPresent(AttributeMapper am) { 60 | for (Attribute a : attributes) 61 | if (a.attributeMapper() == am) 62 | return true; 63 | return false; 64 | } 65 | 66 | private void remove(AttributeMapper am) { 67 | attributes.removeIf(a -> a.attributeMapper() == am); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/SwapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | * @test 26 | * @summary Testing swap instruction 27 | * @run junit SwapTest 28 | */ 29 | 30 | import org.glavo.classfile.AccessFlags; 31 | import org.glavo.classfile.ClassFile; 32 | import static org.junit.jupiter.api.Assertions.*; 33 | import org.junit.jupiter.api.Test; 34 | 35 | import java.lang.constant.ClassDesc; 36 | import java.lang.constant.MethodTypeDesc; 37 | import java.lang.invoke.MethodHandle; 38 | import java.lang.invoke.MethodHandles; 39 | import java.lang.invoke.MethodType; 40 | 41 | import static org.glavo.classfile.AccessFlag.PUBLIC; 42 | import static org.glavo.classfile.AccessFlag.STATIC; 43 | 44 | class SwapTest { 45 | @Test 46 | void testTryCatchCatchAll() throws Throwable { 47 | MethodType mt = MethodType.methodType(String.class, String.class, String.class); 48 | MethodTypeDesc mtd = mt.describeConstable().get(); 49 | 50 | byte[] bytes = ClassFile.of().build(ClassDesc.of("C"), cb -> { 51 | cb.withMethodBody("m", mtd, AccessFlags.ofMethod(PUBLIC, STATIC).flagsMask(), xb -> { 52 | xb.aload(0); // 0 53 | xb.aload(1); // 1, 0 54 | xb.swap(); // 0, 1 55 | xb.pop(); // 1 56 | xb.areturn(); 57 | }); 58 | }); 59 | 60 | MethodHandles.Lookup lookup = MethodHandles.lookup().defineHiddenClass(bytes, true); 61 | MethodHandle m = lookup.findStatic(lookup.lookupClass(), "m", mt); 62 | assertEquals(m.invoke("A", "B"), "B"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/ArrayLoadInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.Opcode; 31 | import org.glavo.classfile.TypeKind; 32 | import org.glavo.classfile.impl.AbstractInstruction; 33 | import org.glavo.classfile.impl.Util; 34 | 35 | /** 36 | * Models an array load instruction in the {@code code} array of a {@code Code} 37 | * attribute. Corresponding opcodes will have a {@code kind} of {@link 38 | * Opcode.Kind#ARRAY_LOAD}. Delivered as a {@link CodeElement} when 39 | * traversing the elements of a {@link CodeModel}. 40 | * 41 | * @since 22 42 | */ 43 | public sealed interface ArrayLoadInstruction extends Instruction 44 | permits AbstractInstruction.UnboundArrayLoadInstruction { 45 | /** 46 | * {@return the component type of the array} 47 | */ 48 | TypeKind typeKind(); 49 | 50 | /** 51 | * {@return an array load instruction} 52 | * 53 | * @param op the opcode for the specific type of array load instruction, 54 | * which must be of kind {@link Opcode.Kind#ARRAY_LOAD} 55 | */ 56 | static ArrayLoadInstruction of(Opcode op) { 57 | Util.checkKind(op, Opcode.Kind.ARRAY_LOAD); 58 | return new AbstractInstruction.UnboundArrayLoadInstruction(op); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/OperatorInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.Opcode; 31 | import org.glavo.classfile.TypeKind; 32 | import org.glavo.classfile.impl.AbstractInstruction; 33 | import org.glavo.classfile.impl.Util; 34 | 35 | /** 36 | * Models an arithmetic operator instruction in the {@code code} array of a 37 | * {@code Code} attribute. Corresponding opcodes will have a {@code kind} of 38 | * {@link Opcode.Kind#OPERATOR}. Delivered as a {@link CodeElement} when 39 | * traversing the elements of a {@link CodeModel}. 40 | * 41 | * @since 22 42 | */ 43 | public sealed interface OperatorInstruction extends Instruction 44 | permits AbstractInstruction.UnboundOperatorInstruction { 45 | /** 46 | * {@return the operand type of the instruction} 47 | */ 48 | TypeKind typeKind(); 49 | 50 | /** 51 | * {@return an operator instruction} 52 | * 53 | * @param op the opcode for the specific type of array load instruction, 54 | * which must be of kind {@link Opcode.Kind#OPERATOR} 55 | */ 56 | static OperatorInstruction of(Opcode op) { 57 | Util.checkKind(op, Opcode.Kind.OPERATOR); 58 | return new AbstractInstruction.UnboundOperatorInstruction(op); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/StackMapTableAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile.attribute; 27 | 28 | import java.util.List; 29 | 30 | import org.glavo.classfile.Attribute; 31 | import org.glavo.classfile.CodeElement; 32 | import org.glavo.classfile.impl.BoundAttribute; 33 | import org.glavo.classfile.impl.UnboundAttribute; 34 | 35 | /** 36 | * Models the {@code StackMapTable} attribute {@jvms 4.7.4}, which can appear 37 | * on a {@code Code} attribute. 38 | *

39 | * The attribute does not permit multiple instances in a given location. 40 | * Subsequent occurrence of the attribute takes precedence during the attributed 41 | * element build or transformation. 42 | *

43 | * The attribute was introduced in the Java SE Platform version 6. 44 | * 45 | * @since 22 46 | */ 47 | public sealed interface StackMapTableAttribute 48 | extends Attribute, CodeElement 49 | permits BoundAttribute.BoundStackMapTableAttribute, UnboundAttribute.UnboundStackMapTableAttribute { 50 | 51 | /** 52 | * {@return the stack map frames} 53 | */ 54 | List entries(); 55 | 56 | /** 57 | * {@return a stack map table attribute} 58 | * @param entries the stack map frames 59 | */ 60 | public static StackMapTableAttribute of(List entries) { 61 | return new UnboundAttribute.UnboundStackMapTableAttribute(entries); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/ArrayStoreInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.Opcode; 31 | import org.glavo.classfile.TypeKind; 32 | import org.glavo.classfile.impl.AbstractInstruction; 33 | import org.glavo.classfile.impl.Util; 34 | 35 | /** 36 | * Models an array store instruction in the {@code code} array of a {@code Code} 37 | * attribute. Corresponding opcodes will have a {@code kind} of {@link 38 | * Opcode.Kind#ARRAY_STORE}. Delivered as a {@link CodeElement} when 39 | * traversing the elements of a {@link CodeModel}. 40 | * 41 | * @since 22 42 | */ 43 | public sealed interface ArrayStoreInstruction extends Instruction 44 | permits AbstractInstruction.UnboundArrayStoreInstruction { 45 | /** 46 | * {@return the component type of the array} 47 | */ 48 | TypeKind typeKind(); 49 | 50 | /** 51 | * {@return an array store instruction} 52 | * 53 | * @param op the opcode for the specific type of array store instruction, 54 | * which must be of kind {@link Opcode.Kind#ARRAY_STORE} 55 | */ 56 | static ArrayStoreInstruction of(Opcode op) { 57 | Util.checkKind(op, Opcode.Kind.ARRAY_STORE); 58 | return new AbstractInstruction.UnboundArrayStoreInstruction(op); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/IncrementInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import org.glavo.classfile.CodeElement; 28 | import org.glavo.classfile.CodeModel; 29 | import org.glavo.classfile.Instruction; 30 | import org.glavo.classfile.Opcode; 31 | import org.glavo.classfile.impl.AbstractInstruction; 32 | 33 | /** 34 | * Models a local variable increment instruction in the {@code code} array of a 35 | * {@code Code} attribute. Corresponding opcodes will have a {@code kind} of 36 | * {@link Opcode.Kind#INCREMENT}. Delivered as a {@link CodeElement} when 37 | * traversing the elements of a {@link CodeModel}. 38 | * 39 | * @since 22 40 | */ 41 | public sealed interface IncrementInstruction extends Instruction 42 | permits AbstractInstruction.BoundIncrementInstruction, 43 | AbstractInstruction.UnboundIncrementInstruction { 44 | /** 45 | * {@return the local variable slot to increment} 46 | */ 47 | int slot(); 48 | 49 | /** 50 | * {@return the value to increment by} 51 | */ 52 | int constant(); 53 | 54 | /** 55 | * {@return an increment instruction} 56 | * 57 | * @param slot the local variable slot to increment 58 | * @param constant the value to increment by 59 | */ 60 | static IncrementInstruction of(int slot, int constant) { 61 | return new AbstractInstruction.UnboundIncrementInstruction(slot, constant); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/instruction/LookupSwitchInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.instruction; 26 | 27 | import java.util.List; 28 | 29 | import org.glavo.classfile.CodeElement; 30 | import org.glavo.classfile.CodeModel; 31 | import org.glavo.classfile.Instruction; 32 | import org.glavo.classfile.Label; 33 | import org.glavo.classfile.impl.AbstractInstruction; 34 | 35 | /** 36 | * Models a {@code lookupswitch} instruction in the {@code code} array of a 37 | * {@code Code} attribute. Delivered as a {@link CodeElement} when traversing 38 | * the elements of a {@link CodeModel}. 39 | * 40 | * @since 22 41 | */ 42 | public sealed interface LookupSwitchInstruction extends Instruction 43 | permits AbstractInstruction.BoundLookupSwitchInstruction, 44 | AbstractInstruction.UnboundLookupSwitchInstruction { 45 | /** 46 | * {@return the target of the default case} 47 | */ 48 | Label defaultTarget(); 49 | 50 | /** 51 | * {@return the cases of the switch} 52 | */ 53 | List cases(); 54 | 55 | /** 56 | * {@return a lookup switch instruction} 57 | * 58 | * @param defaultTarget the default target of the switch 59 | * @param cases the cases of the switch 60 | */ 61 | static LookupSwitchInstruction of(Label defaultTarget, List cases) { 62 | return new AbstractInstruction.UnboundLookupSwitchInstruction(defaultTarget, cases); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/CustomAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile; 26 | 27 | 28 | /** 29 | * Models a non-standard attribute of a classfile. Clients should extend 30 | * this class to provide an implementation class for non-standard attributes, 31 | * and provide an {@link AttributeMapper} to mediate between the classfile 32 | * format and the {@linkplain CustomAttribute} representation. 33 | * @param the custom attribute type 34 | * 35 | * @since 22 36 | */ 37 | public abstract non-sealed class CustomAttribute> 38 | implements Attribute, CodeElement, ClassElement, MethodElement, FieldElement { 39 | 40 | private final AttributeMapper mapper; 41 | 42 | /** 43 | * Construct a {@linkplain CustomAttribute}. 44 | * @param mapper the attribute mapper 45 | */ 46 | protected CustomAttribute(AttributeMapper mapper) { 47 | this.mapper = mapper; 48 | } 49 | 50 | @Override 51 | public final AttributeMapper attributeMapper() { 52 | return mapper; 53 | } 54 | 55 | @Override 56 | public final String attributeName() { 57 | return mapper.name(); 58 | } 59 | 60 | @Override 61 | @SuppressWarnings("unchecked") 62 | public final void writeTo(BufWriter buf) { 63 | mapper.writeAttribute(buf, (T) this); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return String.format("CustomAttribute[name=%s]", mapper.name()); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/BuilderParamTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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 | * @test 26 | * @summary Testing ClassFile builder parameters. 27 | * @run junit BuilderParamTest 28 | */ 29 | import java.lang.constant.ClassDesc; 30 | import java.lang.constant.MethodTypeDesc; 31 | 32 | import org.glavo.classfile.ClassFile; 33 | import org.junit.jupiter.api.Test; 34 | 35 | import static java.lang.constant.ConstantDescs.CD_void; 36 | import static org.glavo.classfile.ClassFile.ACC_STATIC; 37 | import static org.junit.jupiter.api.Assertions.*; 38 | 39 | /** 40 | * BuilderParamTest 41 | */ 42 | class BuilderParamTest { 43 | @Test 44 | void testDirectBuilder() { 45 | var cc = ClassFile.of(); 46 | cc.build(ClassDesc.of("Foo"), cb -> { 47 | cb.withMethod("foo", MethodTypeDesc.ofDescriptor("(IJI)V"), 0, 48 | mb -> mb.withCode(xb -> { 49 | assertEquals(xb.receiverSlot(), 0); 50 | assertEquals(xb.parameterSlot(0), 1); 51 | assertEquals(xb.parameterSlot(1), 2); 52 | assertEquals(xb.parameterSlot(2), 4); 53 | xb.return_(); 54 | })); 55 | }); 56 | cc.build(ClassDesc.of("Foo"), cb -> { 57 | cb.withMethod("foo", MethodTypeDesc.ofDescriptor("(IJI)V"), ACC_STATIC, 58 | mb -> mb.withCode(xb -> { 59 | assertEquals(xb.parameterSlot(0), 0); 60 | assertEquals(xb.parameterSlot(1), 1); 61 | assertEquals(xb.parameterSlot(2), 3); 62 | xb.return_(); 63 | })); 64 | }); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Class-File API for Java 17 2 | 3 | [![Gradle Check](https://github.com/Glavo/classfile/actions/workflows/check.yml/badge.svg)](https://github.com/Glavo/classfile/actions/workflows/check.yml) 4 | [![codecov](https://codecov.io/gh/Glavo/classfile/branch/main/graph/badge.svg?token=O9EUO58YKZ)](https://codecov.io/gh/Glavo/classfile) 5 | [![Latest release](https://img.shields.io/maven-central/v/org.glavo/classfile)](https://github.com/Glavo/classfile/releases/latest) 6 | [![javadoc](https://javadoc.io/badge2/org.glavo/classfile/javadoc.svg)](https://javadoc.io/doc/org.glavo/classfile) 7 | 8 | This is a modern Java classfile manipulation and analysis library. 9 | This library is a modern replacement for [ASM](https://asm.ow2.io/), extracted from the latest implementation of JDK and backport to Java 17. 10 | 11 | ## Adding to your build 12 | 13 | Maven: 14 | ```xml 15 | 16 | org.glavo 17 | classfile 18 | 0.5.0 19 | 20 | ``` 21 | 22 | Gradle: 23 | ```kotlin 24 | implementation("org.glavo:classfile:0.5.0") 25 | ``` 26 | 27 | ## Get started 28 | 29 | * [Tutorial](https://javadoc.io/doc/org.glavo/classfile/latest/org.glavo.classfile/org/glavo/classfile/package-summary.html): The best way to learn how to use the Class-File API; 30 | * [Javadoc](https://javadoc.io/doc/org.glavo/classfile): Documentation for the current release; 31 | * [Examples](./src/examples/java): Some examples showing the usage of Class-File API; 32 | * [JDK Enhancement Proposal](https://openjdk.org/jeps/466): The JEP for Class-File API. 33 | 34 | ## Note 35 | 36 | This library is extracted from OpenJDK 23 and renamed package `jdk.internal.classfile` to `org.glavo.classfile`. 37 | 38 | Since the Class-File API is still in preview, this library may also undergo breaking changes until the Class-File API is generally available. 39 | 40 | In order to be compatible with Java 17, this library also copies some new APIs in Java 20/21: 41 | 42 | * `java.lang.reflect.AccessFlag` -> `org.glavo.classfile.AccessFlag` 43 | * `java.lang.reflect.ClassFileFormatVersion` -> `org.glavo.classfile.ClassFileFormatVersion` 44 | * `java.lang.constant.ModuleDesc` -> `org.glavo.classfile.constant.ModuleDesc` 45 | * `java.lang.constant.PackageDesc` -> `org.glavo.classfile.constant.PackageDesc` 46 | 47 | Since the Class-File API is still in preview, it is not yet stable 48 | 49 | [Here](CHANGELOG.md) is the change log. 50 | 51 | If you encounter problems when using this library, please feed back through [issue](https://github.com/Glavo/classfile/issues). 52 | 53 | If you want to discuss the design of the Class-File API, please go to the [Class-File API mailing list](https://mail.openjdk.org/mailman/listinfo/classfile-api-dev) (classfile-api-dev@openjdk.org) for discussion. 54 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/MethodModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | 26 | package org.glavo.classfile; 27 | 28 | import java.lang.constant.MethodTypeDesc; 29 | import java.util.Optional; 30 | 31 | import org.glavo.classfile.constantpool.Utf8Entry; 32 | import org.glavo.classfile.impl.BufferedMethodBuilder; 33 | import org.glavo.classfile.impl.MethodImpl; 34 | 35 | /** 36 | * Models a method. The contents of the method can be traversed via 37 | * a streaming view (e.g., {@link #elements()}), or via random access (e.g., 38 | * {@link #flags()}), or by freely mixing the two. 39 | * 40 | * @since 22 41 | */ 42 | public sealed interface MethodModel 43 | extends WritableElement, CompoundElement, AttributedElement, ClassElement 44 | permits BufferedMethodBuilder.Model, MethodImpl { 45 | 46 | /** {@return the access flags} */ 47 | AccessFlags flags(); 48 | 49 | /** {@return the class model this method is a member of, if known} */ 50 | Optional parent(); 51 | 52 | /** {@return the name of this method} */ 53 | Utf8Entry methodName(); 54 | 55 | /** {@return the method descriptor of this method} */ 56 | Utf8Entry methodType(); 57 | 58 | /** {@return the method descriptor of this method, as a symbolic descriptor} */ 59 | default MethodTypeDesc methodTypeSymbol() { 60 | return MethodTypeDesc.ofDescriptor(methodType().stringValue()); 61 | } 62 | 63 | /** {@return the body of this method, if there is one} */ 64 | Optional code(); 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/LocalVariableInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.attribute; 26 | 27 | import java.lang.constant.ClassDesc; 28 | import org.glavo.classfile.constantpool.Utf8Entry; 29 | import org.glavo.classfile.impl.BoundLocalVariable; 30 | import org.glavo.classfile.impl.UnboundAttribute; 31 | 32 | /** 33 | * Models a single local variable in the {@link LocalVariableTableAttribute}. 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface LocalVariableInfo 38 | permits UnboundAttribute.UnboundLocalVariableInfo, BoundLocalVariable { 39 | 40 | /** 41 | * {@return the index into the code array (inclusive) at which the scope of 42 | * this variable begins} 43 | */ 44 | int startPc(); 45 | 46 | /** 47 | * {@return the length of the region of the code array in which this 48 | * variable is in scope.} 49 | */ 50 | int length(); 51 | 52 | /** 53 | * {@return the name of the local variable} 54 | */ 55 | Utf8Entry name(); 56 | 57 | /** 58 | * {@return the field descriptor of the local variable} 59 | */ 60 | Utf8Entry type(); 61 | 62 | /** 63 | * {@return the field descriptor of the local variable} 64 | */ 65 | default ClassDesc typeSymbol() { 66 | return ClassDesc.ofDescriptor(type().stringValue()); 67 | } 68 | 69 | /** 70 | * {@return the index into the local variable array of the current frame 71 | * which holds this local variable} 72 | */ 73 | int slot(); 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/glavo/classfile/attribute/ModuleHashInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022, 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. Oracle designates this 8 | * particular file as subject to the "Classpath" exception as provided 9 | * by Oracle in the LICENSE file that accompanied this code. 10 | * 11 | * This code is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 | * version 2 for more details (a copy is included in the LICENSE file that 15 | * accompanied this code). 16 | * 17 | * You should have received a copy of the GNU General Public License version 18 | * 2 along with this work; if not, write to the Free Software Foundation, 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 | * 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 | * or visit www.oracle.com if you need additional information or have any 23 | * questions. 24 | */ 25 | package org.glavo.classfile.attribute; 26 | 27 | import org.glavo.classfile.constant.ModuleDesc; 28 | import org.glavo.classfile.constantpool.ModuleEntry; 29 | import org.glavo.classfile.impl.TemporaryConstantPool; 30 | import org.glavo.classfile.impl.UnboundAttribute; 31 | 32 | /** 33 | * Models hash information for a single module in the {@link ModuleHashesAttribute}. 34 | * 35 | * @since 22 36 | */ 37 | public sealed interface ModuleHashInfo 38 | permits UnboundAttribute.UnboundModuleHashInfo { 39 | 40 | /** 41 | * {@return the name of the related module} 42 | */ 43 | ModuleEntry moduleName(); 44 | 45 | /** 46 | * {@return the hash of the related module} 47 | */ 48 | byte[] hash(); 49 | 50 | /** 51 | * {@return a module hash description} 52 | * @param moduleName the module name 53 | * @param hash the hash value 54 | */ 55 | static ModuleHashInfo of(ModuleEntry moduleName, byte[] hash) { 56 | return new UnboundAttribute.UnboundModuleHashInfo(moduleName, hash); 57 | } 58 | 59 | /** 60 | * {@return a module hash description} 61 | * @param moduleDesc the module name 62 | * @param hash the hash value 63 | */ 64 | static ModuleHashInfo of(ModuleDesc moduleDesc, byte[] hash) { 65 | return new UnboundAttribute.UnboundModuleHashInfo(TemporaryConstantPool.INSTANCE.moduleEntry(TemporaryConstantPool.INSTANCE.utf8Entry(moduleDesc.name())), hash); 66 | } 67 | } 68 | --------------------------------------------------------------------------------