├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── [A] build and test, release if requested.yml │ └── [A] update documentation.yml ├── .gitignore ├── LICENSE ├── README.md ├── docs ├── _config.yml ├── _layouts │ └── default.html ├── assets │ ├── css │ │ └── style.scss │ └── fonts │ │ ├── CooperBlackStd.eot │ │ ├── CooperBlackStd.ttf │ │ ├── CooperBlackStd.woff │ │ └── CooperBlackStd.woff2 ├── favicon.png ├── index.md └── logo.png ├── java-hook ├── bin │ ├── io │ │ └── github │ │ │ └── toolfactory │ │ │ └── jvm │ │ │ └── function │ │ │ └── catalog │ │ │ └── ThrowExceptionFunction$ForJava7.class │ ├── java │ │ └── lang │ │ │ ├── ConsulterRetrieverForJDK9.class │ │ │ └── reflect │ │ │ └── AccessibleSetterInvokerForJDK9.class │ └── jdk │ │ └── internal │ │ └── loader │ │ ├── BuiltinClassLoader.class │ │ ├── ClassLoaderDelegateForJDK9.class │ │ └── URLClassPath.class ├── compileExternalClasses.cmd ├── compileExternalClasses.sh ├── pom.xml └── src │ └── main │ └── java │ ├── java │ └── lang │ │ ├── ConsulterRetrieverForJDK9.java │ │ └── reflect │ │ └── AccessibleSetterInvokerForJDK9.java │ └── jdk │ └── internal │ └── loader │ └── ClassLoaderDelegateForJDK9.java ├── java ├── pom.xml ├── sa-pom.xml └── src │ ├── main │ ├── java │ │ ├── io │ │ │ └── github │ │ │ │ └── toolfactory │ │ │ │ └── jvm │ │ │ │ ├── DefaultDriver.java │ │ │ │ ├── Driver.java │ │ │ │ ├── DriverAbst.java │ │ │ │ ├── DynamicDriver.java │ │ │ │ ├── HybridDriver.java │ │ │ │ ├── Info.java │ │ │ │ ├── InfoImpl.java │ │ │ │ ├── NativeDriver.java │ │ │ │ ├── function │ │ │ │ ├── InitializeException.java │ │ │ │ ├── catalog │ │ │ │ │ ├── AllocateInstanceFunction.java │ │ │ │ │ ├── BuiltinClassLoaderClassSupplier.java │ │ │ │ │ ├── ClassLoaderDelegateClassSupplier.java │ │ │ │ │ ├── ConstructorInvokeFunction.java │ │ │ │ │ ├── ConsulterSupplier.java │ │ │ │ │ ├── ConsulterSupplyFunction.java │ │ │ │ │ ├── ConvertToBuiltinClassLoaderFunction.java │ │ │ │ │ ├── DeepConsulterSupplyFunction.java │ │ │ │ │ ├── DefineHookClassFunction.java │ │ │ │ │ ├── GetClassByNameFunction.java │ │ │ │ │ ├── GetDeclaredConstructorsFunction.java │ │ │ │ │ ├── GetDeclaredFieldFunction.java │ │ │ │ │ ├── GetDeclaredFieldsFunction.java │ │ │ │ │ ├── GetDeclaredMethodFunction.java │ │ │ │ │ ├── GetDeclaredMethodsFunction.java │ │ │ │ │ ├── GetFieldValueFunction.java │ │ │ │ │ ├── GetLoadedClassesRetrieverFunction.java │ │ │ │ │ ├── GetLoadedPackagesFunction.java │ │ │ │ │ ├── GetPackageFunction.java │ │ │ │ │ ├── GetResourcesFunction.java │ │ │ │ │ ├── MethodInvokeFunction.java │ │ │ │ │ ├── PrivateLookupInMethodHandleSupplier.java │ │ │ │ │ ├── SetAccessibleFunction.java │ │ │ │ │ ├── SetFieldValueFunction.java │ │ │ │ │ ├── StopThreadFunction.java │ │ │ │ │ ├── ThrowExceptionFunction.java │ │ │ │ │ ├── UnsafeSupplier.java │ │ │ │ │ └── UnsafeWrapper.java │ │ │ │ └── template │ │ │ │ │ ├── BiFunction.java │ │ │ │ │ ├── Consumer.java │ │ │ │ │ ├── Function.java │ │ │ │ │ ├── Supplier.java │ │ │ │ │ ├── ThrowingBiConsumer.java │ │ │ │ │ ├── ThrowingBiFunction.java │ │ │ │ │ ├── ThrowingFunction.java │ │ │ │ │ ├── ThrowingQuadFunction.java │ │ │ │ │ ├── ThrowingTriFunction.java │ │ │ │ │ └── TriConsumer.java │ │ │ │ └── util │ │ │ │ ├── BufferHandler.java │ │ │ │ ├── Classes.java │ │ │ │ ├── CleanableSupplier.java │ │ │ │ ├── JavaClass.java │ │ │ │ ├── ObjectProvider.java │ │ │ │ ├── Properties.java │ │ │ │ ├── Resources.java │ │ │ │ ├── Streams.java │ │ │ │ ├── Strings.java │ │ │ │ ├── ThrowingBiConsumerAdapter.java │ │ │ │ └── ThrowingFunctionAdapter.java │ │ └── module-info.java │ └── resources │ │ └── jvm-driver.properties │ └── test │ ├── java │ └── io │ │ └── github │ │ └── toolfactory │ │ ├── jvm │ │ └── test │ │ │ ├── AllTestsSuite.java │ │ │ ├── BaseTest.java │ │ │ ├── DefaultDriverTest.java │ │ │ ├── DynamicDriverTest.java │ │ │ ├── HybridDriverTest.java │ │ │ └── NativeDriverTest.java │ │ └── util │ │ └── Reflection.java │ └── resources │ └── jvm-driver.properties ├── launcher └── eclipse │ ├── ToolFactory JVM Driver - Debug attacher.launch │ ├── ToolFactory JVM Driver - build and test with REMOTE DEBUG.launch │ ├── ToolFactory JVM Driver - clean package install.launch │ ├── ToolFactory JVM Driver - parent - clean package install.launch │ └── ToolFactory JVM Driver - test.launch └── pom.xml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://www.paypal.com/donate/?cmd=_donations&business=EY4TMTW8SWDAC&item_name=Support+maintenance+and+improvement+of+ToolFactory+JVM+Driver¤cy_code=EUR&source=url 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "maven" 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | time: "02:00" 8 | - package-ecosystem: "github-actions" 9 | directory: "/" 10 | schedule: 11 | interval: "monthly" 12 | time: "03:00" 13 | -------------------------------------------------------------------------------- /.github/workflows/[A] update documentation.yml: -------------------------------------------------------------------------------- 1 | # This file is part of ToolFactory JVM driver. 2 | # 3 | # Hosted at: https://github.com/toolfactory/jvm-driver 4 | # 5 | # - - 6 | # 7 | # The MIT License (MIT) 8 | # 9 | # Copyright (c) 2021 Luke Hutchison, Roberto Gentili 10 | # 11 | # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 12 | # documentation files (the "Software"), to deal in the Software without restriction, including without 13 | # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 14 | # the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 15 | # conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all copies or substantial 18 | # portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 21 | # LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 22 | # EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 24 | # OR OTHER DEALINGS IN THE SOFTWARE. 25 | # 26 | name: Update index page 27 | 28 | on: 29 | push: 30 | branches: 31 | - main 32 | paths: 33 | - "**README.md" 34 | 35 | jobs: 36 | update-index-page: 37 | runs-on: ubuntu-latest 38 | name: Update index page 39 | steps: 40 | - uses: actions/checkout@master 41 | - name: Overwrite the index.md 42 | run: | 43 | git config user.name "${{ github.event.head_commit.committer.name }}" 44 | git config user.email "${{ github.event.head_commit.committer.email }}" 45 | git pull origin ${{github.ref}} 46 | cp "./README.md" "./docs/index.md" 47 | git add . 48 | git commit -am "Update" --allow-empty 49 | git push -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Log file 2 | *.log 3 | 4 | # BlueJ files 5 | *.ctxt 6 | 7 | # Mobile Tools for Java (J2ME) 8 | .mtj.tmp/ 9 | 10 | # Package Files # 11 | *.jar 12 | *.war 13 | *.nar 14 | *.ear 15 | *.tar.gz 16 | *.rar 17 | 18 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 19 | hs_err_pid* 20 | 21 | /.project 22 | /.settings 23 | /.classpath 24 | /target/ 25 | 26 | /java/.project 27 | /java/.settings 28 | /java/.classpath 29 | /java/target/ 30 | 31 | /java-hook/.project 32 | /java-hook/.settings 33 | /java-hook/.classpath 34 | /java-hook/target/ 35 | 36 | /native/.project 37 | /native/.settings 38 | /native/.classpath 39 | /native/target/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Luke Hutchison, Roberto Gentili 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: ToolFactory JVM driver 3 | google_analytics: UA-154852845-4 4 | show_downloads: false -------------------------------------------------------------------------------- /docs/_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {% if site.google_analytics %} 6 | 7 | 13 | {% endif %} 14 | 15 | 16 | {% seo %} 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 36 | 37 |
38 | {{ content }} 39 | 40 | 45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/assets/css/style.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "{{ site.theme }}"; 5 | .page-header { 6 | background-image: linear-gradient(120deg, #183153, #158fcc); 7 | } 8 | 9 | h1.project-name { 10 | font-family: 'Cooper Std'; 11 | font-size: 3.25rem; 12 | line-height: 1.00em; 13 | } 14 | 15 | @font-face { 16 | font-family: 'Cooper Std'; 17 | src: url('CooperBlackStd.eot'); 18 | src: local('Cooper Std Black'), local('CooperBlackStd'), 19 | url('./../fonts/CooperBlackStd.eot?#iefix') format('embedded-opentype'), 20 | url('./../fonts/CooperBlackStd.woff2') format('woff2'), 21 | url('./../fonts/CooperBlackStd.woff') format('woff'), 22 | url('./../fonts/CooperBlackStd.ttf') format('truetype'); 23 | font-weight: 900; 24 | font-style: normal; 25 | font-display: swap; 26 | } 27 | 28 | .main-content { 29 | h1 { 30 | color: #158fcc; 31 | } 32 | h2 { 33 | color: #158fcc; 34 | } 35 | h3 { 36 | color: #158fcc; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /docs/assets/fonts/CooperBlackStd.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/assets/fonts/CooperBlackStd.eot -------------------------------------------------------------------------------- /docs/assets/fonts/CooperBlackStd.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/assets/fonts/CooperBlackStd.ttf -------------------------------------------------------------------------------- /docs/assets/fonts/CooperBlackStd.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/assets/fonts/CooperBlackStd.woff -------------------------------------------------------------------------------- /docs/assets/fonts/CooperBlackStd.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/assets/fonts/CooperBlackStd.woff2 -------------------------------------------------------------------------------- /docs/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/favicon.png -------------------------------------------------------------------------------- /docs/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/docs/logo.png -------------------------------------------------------------------------------- /java-hook/bin/io/github/toolfactory/jvm/function/catalog/ThrowExceptionFunction$ForJava7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/io/github/toolfactory/jvm/function/catalog/ThrowExceptionFunction$ForJava7.class -------------------------------------------------------------------------------- /java-hook/bin/java/lang/ConsulterRetrieverForJDK9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/java/lang/ConsulterRetrieverForJDK9.class -------------------------------------------------------------------------------- /java-hook/bin/java/lang/reflect/AccessibleSetterInvokerForJDK9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/java/lang/reflect/AccessibleSetterInvokerForJDK9.class -------------------------------------------------------------------------------- /java-hook/bin/jdk/internal/loader/BuiltinClassLoader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/jdk/internal/loader/BuiltinClassLoader.class -------------------------------------------------------------------------------- /java-hook/bin/jdk/internal/loader/ClassLoaderDelegateForJDK9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/jdk/internal/loader/ClassLoaderDelegateForJDK9.class -------------------------------------------------------------------------------- /java-hook/bin/jdk/internal/loader/URLClassPath.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toolfactory/jvm-driver/a346bf6798d32b8e7ad765707c765cc86c8c7aff/java-hook/bin/jdk/internal/loader/URLClassPath.class -------------------------------------------------------------------------------- /java-hook/compileExternalClasses.cmd: -------------------------------------------------------------------------------- 1 | :: This file is part of ToolFactory JVM driver. 2 | :: 3 | :: Hosted at: https://github.com/toolfactory/jvm-driver 4 | :: 5 | :: - - 6 | :: 7 | :: The MIT License (MIT) 8 | :: 9 | :: Copyright (c) 2021 Luke Hutchison, Roberto Gentili 10 | :: 11 | :: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 12 | :: documentation files (the "Software"), to deal in the Software without restriction, including without 13 | :: limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 14 | :: the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 15 | :: conditions: 16 | :: 17 | :: The above copyright notice and this permission notice shall be included in all copies or substantial 18 | :: portions of the Software. 19 | :: 20 | :: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 21 | :: LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 22 | :: EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 | :: AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 24 | :: OR OTHER DEALINGS IN THE SOFTWARE. 25 | :: 26 | @echo off 27 | call "%1/bin/javac.exe" -cp "%2;%3;" --release 9 "%4/jdk/internal/loader/ClassLoaderDelegateForJDK9.java" -d "%5" 28 | call "%1/bin/javac.exe" -cp "%2;%3;" --release 8 "%4/java/lang/reflect/AccessibleSetterInvokerForJDK9.java" -d "%5" 29 | call "%1/bin/javac.exe" -cp "%2;%3;" --release 8 "%4/java/lang/ConsulterRetrieverForJDK9.java" -d "%5" 30 | -------------------------------------------------------------------------------- /java-hook/compileExternalClasses.sh: -------------------------------------------------------------------------------- 1 | # This file is part of ToolFactory JVM driver. 2 | # 3 | # Hosted at: https://github.com/toolfactory/jvm-driver 4 | # 5 | # - - 6 | # 7 | # The MIT License (MIT) 8 | # 9 | # Copyright (c) 2021 Luke Hutchison, Roberto Gentili 10 | # 11 | # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 12 | # documentation files (the "Software"), to deal in the Software without restriction, including without 13 | # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 14 | # the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 15 | # conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all copies or substantial 18 | # portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 21 | # LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 22 | # EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 24 | # OR OTHER DEALINGS IN THE SOFTWARE. 25 | # 26 | #!bin/bash/ 27 | 28 | $1/bin/javac -cp "$2:$3:" --release 9 "$4/jdk/internal/loader/ClassLoaderDelegateForJDK9.java" -d "$5" 29 | $1/bin/javac -cp "$2:$3:" --release 8 "$4/java/lang/reflect/AccessibleSetterInvokerForJDK9.java" -d "$5" 30 | $1/bin/javac -cp "$2:$3:" --release 8 "$4/java/lang/ConsulterRetrieverForJDK9.java" -d "$5" 31 | -------------------------------------------------------------------------------- /java-hook/pom.xml: -------------------------------------------------------------------------------- 1 | 27 | 28 | 4.0.0 29 | 30 | 31 | io.github.toolfactory 32 | jvm-driver-parent 33 | ${revision} 34 | 35 | 36 | jvm-driver-hook 37 | 38 | pom 39 | 40 | 41 | UTF-8 42 | UTF-8 43 | ${project_jdk_version} 44 | ${project_jdk_version} 45 | 3.0.0 46 | 47 | 48 | 49 | 50 | 51 | org.codehaus.mojo 52 | exec-maven-plugin 53 | ${exec-maven-plugin.version} 54 | 55 | 56 | compile 57 | compile external classes 58 | 59 | exec 60 | 61 | 62 | ${project.basedir}/compileExternalClasses.${org.codehaus.mojo.exec-maven-plugin.exec-extension} 63 | 64 | ${java.home} 65 | ${project.basedir}/bin 66 | ${project.basedir}/../java/target/classes 67 | ${project.basedir}/src/main/java 68 | ${project.basedir}/bin 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | windows-profile 80 | 81 | 82 | Windows 83 | 84 | 85 | 86 | cmd 87 | 88 | 89 | 90 | unix-profile 91 | 92 | 93 | unix 94 | Linux 95 | 96 | 97 | 98 | sh 99 | 100 | 101 | 102 | mac-profile 103 | 104 | 105 | mac 106 | 107 | 108 | 109 | sh 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /java-hook/src/main/java/java/lang/ConsulterRetrieverForJDK9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package java.lang; 28 | 29 | import java.lang.invoke.MethodHandle; 30 | import java.lang.invoke.MethodHandles; 31 | import java.lang.invoke.MethodHandles.Lookup; 32 | import java.lang.invoke.MethodType; 33 | import java.util.function.Function; 34 | 35 | @SuppressWarnings("unchecked") 36 | public class ConsulterRetrieverForJDK9 implements Function, MethodHandles.Lookup> { 37 | private static MethodHandle consulterRetriever; 38 | public final static MethodHandles.Lookup mainConsulter; 39 | 40 | static { 41 | mainConsulter = MethodHandles.lookup(); 42 | } 43 | 44 | @Override 45 | public Lookup apply(Class cls) { 46 | try { 47 | return (MethodHandles.Lookup)consulterRetriever.invokeWithArguments(cls, mainConsulter); 48 | } catch (Throwable exc) { 49 | return throwExceptionWithReturn(exc); 50 | } 51 | } 52 | 53 | private static T throwExceptionWithReturn(Throwable exc) { 54 | throwException(exc); 55 | return null; 56 | } 57 | 58 | private static void throwException(Throwable exc) throws E{ 59 | throw (E)exc; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /java-hook/src/main/java/java/lang/reflect/AccessibleSetterInvokerForJDK9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package java.lang.reflect; 28 | 29 | import java.lang.reflect.AccessibleObject; 30 | import java.lang.reflect.Method; 31 | import java.lang.invoke.MethodHandle; 32 | import java.lang.invoke.MethodHandles; 33 | import java.util.function.BiConsumer; 34 | 35 | @SuppressWarnings("unchecked") 36 | public class AccessibleSetterInvokerForJDK9 implements BiConsumer { 37 | private static MethodHandle accessibleSetterMethodHandle; 38 | // Lazy initialized by io.github.toolfactory.jvm.function.catalog.SetAccessibleFunction 39 | public static MethodHandles.Lookup mainConsulter; 40 | 41 | static { 42 | try { 43 | Method accessibleSetterMethod = AccessibleObject.class.getDeclaredMethod("setAccessible0", boolean.class); 44 | accessibleSetterMethodHandle = mainConsulter.unreflect(accessibleSetterMethod); 45 | } catch (Throwable exc) { 46 | throwException(exc); 47 | } 48 | 49 | } 50 | 51 | private static void throwException(Throwable exc) throws E{ 52 | throw (E)exc; 53 | } 54 | 55 | @Override 56 | public void accept(AccessibleObject accessibleObject, Boolean flag) { 57 | try { 58 | accessibleSetterMethodHandle.invokeWithArguments(accessibleObject, flag); 59 | } catch (Throwable exc) { 60 | throwException(exc); 61 | } 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /java-hook/src/main/java/jdk/internal/loader/ClassLoaderDelegateForJDK9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package jdk.internal.loader; 28 | 29 | import java.io.IOException; 30 | import java.io.InputStream; 31 | import java.net.URL; 32 | import java.util.Enumeration; 33 | import java.lang.invoke.MethodHandle; 34 | 35 | 36 | public class ClassLoaderDelegateForJDK9 extends BuiltinClassLoader { 37 | 38 | private ClassLoader classLoader; 39 | private MethodHandle loadClassMethod; 40 | 41 | static { 42 | ClassLoader.registerAsParallelCapable(); 43 | } 44 | 45 | ClassLoaderDelegateForJDK9(BuiltinClassLoader parent, ClassLoader classLoader, MethodHandle loadClassMethodHandle) { 46 | super("ClassLoaderDelegateOf" + classLoader.toString(), parent, null); 47 | this.classLoader = classLoader; 48 | this.loadClassMethod = loadClassMethodHandle; 49 | } 50 | 51 | @Override 52 | protected Class loadClassOrNull(String className, boolean resolve) { 53 | try { 54 | return (Class)loadClassMethod.invokeWithArguments(classLoader, className, resolve); 55 | } catch (Throwable exc) { 56 | exc.printStackTrace(); 57 | return null; 58 | } 59 | } 60 | 61 | @Override 62 | protected Class loadClass(String className, boolean resolve) throws ClassNotFoundException { 63 | try { 64 | return (Class)loadClassMethod.invokeWithArguments(classLoader, className, resolve); 65 | } catch (ClassNotFoundException exc) { 66 | throw exc; 67 | } catch (Throwable exc) { 68 | throw new ClassNotFoundException(className, exc); 69 | } 70 | } 71 | 72 | @Override 73 | public URL getResource(String name) { 74 | return classLoader.getResource(name); 75 | } 76 | 77 | @Override 78 | public Enumeration getResources(String name) throws IOException { 79 | return classLoader.getResources(name); 80 | } 81 | 82 | @Override 83 | public InputStream getResourceAsStream(String name) { 84 | return classLoader.getResourceAsStream(name); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/DefaultDriver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm; 28 | 29 | 30 | import io.github.toolfactory.jvm.function.catalog.AllocateInstanceFunction; 31 | import io.github.toolfactory.jvm.function.catalog.BuiltinClassLoaderClassSupplier; 32 | import io.github.toolfactory.jvm.function.catalog.ClassLoaderDelegateClassSupplier; 33 | import io.github.toolfactory.jvm.function.catalog.ConstructorInvokeFunction; 34 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplyFunction; 35 | import io.github.toolfactory.jvm.function.catalog.ConvertToBuiltinClassLoaderFunction; 36 | import io.github.toolfactory.jvm.function.catalog.DeepConsulterSupplyFunction; 37 | import io.github.toolfactory.jvm.function.catalog.DefineHookClassFunction; 38 | import io.github.toolfactory.jvm.function.catalog.GetClassByNameFunction; 39 | import io.github.toolfactory.jvm.function.catalog.GetDeclaredConstructorsFunction; 40 | import io.github.toolfactory.jvm.function.catalog.GetDeclaredFieldsFunction; 41 | import io.github.toolfactory.jvm.function.catalog.GetDeclaredMethodsFunction; 42 | import io.github.toolfactory.jvm.function.catalog.GetFieldValueFunction; 43 | import io.github.toolfactory.jvm.function.catalog.GetLoadedClassesRetrieverFunction; 44 | import io.github.toolfactory.jvm.function.catalog.GetLoadedPackagesFunction; 45 | import io.github.toolfactory.jvm.function.catalog.GetPackageFunction; 46 | import io.github.toolfactory.jvm.function.catalog.GetResourcesFunction; 47 | import io.github.toolfactory.jvm.function.catalog.MethodInvokeFunction; 48 | import io.github.toolfactory.jvm.function.catalog.SetAccessibleFunction; 49 | import io.github.toolfactory.jvm.function.catalog.SetFieldValueFunction; 50 | import io.github.toolfactory.jvm.function.catalog.StopThreadFunction; 51 | import io.github.toolfactory.jvm.function.catalog.ThrowExceptionFunction; 52 | 53 | 54 | public class DefaultDriver extends DriverAbst { 55 | 56 | @Override 57 | protected Class getThrowExceptionFunctionClass() { 58 | return ThrowExceptionFunction.class; 59 | } 60 | 61 | 62 | @Override 63 | protected Class getAllocateInstanceFunctionClass() { 64 | return AllocateInstanceFunction.class; 65 | } 66 | 67 | 68 | @Override 69 | protected Class getGetFieldValueFunctionClass() { 70 | return GetFieldValueFunction.class; 71 | } 72 | 73 | 74 | @Override 75 | protected Class getSetFieldValueFunctionClass() { 76 | return SetFieldValueFunction.class; 77 | } 78 | 79 | 80 | @Override 81 | protected Class getDefineHookClassFunctionClass() { 82 | return DefineHookClassFunction.class; 83 | } 84 | 85 | 86 | @Override 87 | protected Class getConsulterSupplyFunctionClass() { 88 | return ConsulterSupplyFunction.class; 89 | } 90 | 91 | 92 | @Override 93 | protected Class getGetDeclaredFieldsFunctionClass() { 94 | return GetDeclaredFieldsFunction.class; 95 | } 96 | 97 | 98 | @Override 99 | protected Class getGetDeclaredMethodsFunctionClass() { 100 | return GetDeclaredMethodsFunction.class; 101 | } 102 | 103 | 104 | @Override 105 | protected Class getGetDeclaredConstructorsFunctionClass() { 106 | return GetDeclaredConstructorsFunction.class; 107 | } 108 | 109 | 110 | @Override 111 | protected Class getSetAccessibleFunctionClass() { 112 | return SetAccessibleFunction.class; 113 | } 114 | 115 | 116 | @Override 117 | protected Class getConstructorInvokeFunctionClass() { 118 | return ConstructorInvokeFunction.class; 119 | } 120 | 121 | 122 | @Override 123 | protected Class getMethodInvokeFunctionClass() { 124 | return MethodInvokeFunction.class; 125 | } 126 | 127 | 128 | @Override 129 | protected Class getGetPackageFunctionClass() { 130 | return GetPackageFunction.class; 131 | } 132 | 133 | 134 | @Override 135 | protected Class getGetResourcesFunctionClass() { 136 | return GetResourcesFunction.class; 137 | } 138 | 139 | 140 | @Override 141 | protected Class getGetClassByNameFunctionClass() { 142 | return GetClassByNameFunction.class; 143 | } 144 | 145 | 146 | @Override 147 | protected Class getBuiltinClassLoaderClassSupplierClass() { 148 | return BuiltinClassLoaderClassSupplier.class; 149 | } 150 | 151 | 152 | @Override 153 | protected Class getClassLoaderDelegateClassSupplierClass() { 154 | return ClassLoaderDelegateClassSupplier.class; 155 | } 156 | 157 | 158 | @Override 159 | protected Class getDeepConsulterSupplyFunctionClass() { 160 | return DeepConsulterSupplyFunction.class; 161 | } 162 | 163 | 164 | @Override 165 | protected Class getGetLoadedClassesRetrieverFunctionClass() { 166 | return GetLoadedClassesRetrieverFunction.class; 167 | } 168 | 169 | 170 | @Override 171 | protected Class getGetLoadedPackagesFunctionClass() { 172 | return GetLoadedPackagesFunction.class; 173 | } 174 | 175 | 176 | @Override 177 | protected Class getConvertToBuiltinClassLoaderFunctionClass() { 178 | return ConvertToBuiltinClassLoaderFunction.class; 179 | } 180 | 181 | 182 | @Override 183 | protected Class getStopThreadFunctionClass() { 184 | return StopThreadFunction.class; 185 | } 186 | 187 | 188 | } 189 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/DynamicDriver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm; 28 | 29 | 30 | import java.util.Map; 31 | 32 | import io.github.toolfactory.jvm.function.catalog.AllocateInstanceFunction; 33 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplier; 34 | import io.github.toolfactory.jvm.function.catalog.GetFieldValueFunction; 35 | import io.github.toolfactory.jvm.function.catalog.GetLoadedClassesRetrieverFunction; 36 | import io.github.toolfactory.jvm.function.catalog.GetLoadedPackagesFunction; 37 | import io.github.toolfactory.jvm.function.catalog.SetAccessibleFunction; 38 | import io.github.toolfactory.jvm.function.catalog.SetFieldValueFunction; 39 | import io.github.toolfactory.jvm.util.ObjectProvider; 40 | import io.github.toolfactory.jvm.util.ObjectProvider.BuildingException; 41 | 42 | 43 | public class DynamicDriver extends DefaultDriver { 44 | 45 | @Override 46 | protected Map functionsToMap() { 47 | Map context = super.functionsToMap(); 48 | ObjectProvider.setExceptionHandler( 49 | context, 50 | new ObjectProvider.ExceptionHandler() { 51 | @Override 52 | public T handle(ObjectProvider objectProvider, Class clazz, Map context, 53 | BuildingException exc) { 54 | if (clazz.equals(ConsulterSupplier.class)) { 55 | return objectProvider.getOrBuildObject(ConsulterSupplier.Native.class, context); 56 | } 57 | if (clazz.equals(SetFieldValueFunction.class)) { 58 | return objectProvider.getOrBuildObject(SetFieldValueFunction.Native.class, context); 59 | } 60 | if (clazz.equals(AllocateInstanceFunction.class)) { 61 | return objectProvider.getOrBuildObject(AllocateInstanceFunction.Native.class, context); 62 | } 63 | if (clazz.equals(SetAccessibleFunction.class)) { 64 | return objectProvider.getOrBuildObject(SetAccessibleFunction.Native.class, context); 65 | } 66 | if (clazz.equals(GetFieldValueFunction.class)) { 67 | return objectProvider.getOrBuildObject(GetFieldValueFunction.Native.class, context); 68 | } 69 | if (clazz.equals(GetLoadedClassesRetrieverFunction.class)) { 70 | return objectProvider.getOrBuildObject(GetLoadedClassesRetrieverFunction.Native.class, context); 71 | } 72 | if (clazz.equals(GetLoadedPackagesFunction.class)) { 73 | return objectProvider.getOrBuildObject(GetLoadedPackagesFunction.Native.class, context); 74 | } 75 | throw exc; 76 | } 77 | } 78 | ); 79 | return context; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/HybridDriver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm; 28 | 29 | 30 | import java.util.Map; 31 | 32 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplier; 33 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplyFunction; 34 | import io.github.toolfactory.jvm.util.ObjectProvider; 35 | import io.github.toolfactory.jvm.util.ObjectProvider.BuildingException; 36 | 37 | 38 | @SuppressWarnings("unchecked") 39 | public class HybridDriver extends DefaultDriver { 40 | 41 | 42 | @Override 43 | protected Map functionsToMap() { 44 | Map context = super.functionsToMap(); 45 | ObjectProvider.get(context).markToBeInitializedViaExceptionHandler(ConsulterSupplier.class, context); 46 | ObjectProvider.get(context).markToBeInitializedViaExceptionHandler(ConsulterSupplyFunction.class, context); 47 | ObjectProvider.setExceptionHandler( 48 | context, 49 | new ObjectProvider.ExceptionHandler() { 50 | 51 | @Override 52 | public T handle(ObjectProvider objectProvider, Class clazz, Map context, 53 | BuildingException exception) { 54 | if (objectProvider.isMarkedToBeInitializedViaExceptionHandler(exception)) { 55 | if (clazz.isAssignableFrom(getConsulterSupplierFunctionClass())) { 56 | return (T)objectProvider.getOrBuildObject(getConsulterSupplierFunctionClass(), context); 57 | } 58 | if (clazz.isAssignableFrom(getConsulterSupplyFunctionClass())) { 59 | return (T)objectProvider.getOrBuildObject(getConsulterSupplyFunctionClass(), context); 60 | } 61 | } 62 | throw exception; 63 | } 64 | } 65 | ); 66 | return context; 67 | } 68 | 69 | 70 | protected Class getConsulterSupplierFunctionClass() { 71 | return ConsulterSupplier.Hybrid.class; 72 | } 73 | 74 | 75 | @Override 76 | protected Class getConsulterSupplyFunctionClass() { 77 | return ConsulterSupplyFunction.Hybrid.class; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/Info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is derived from ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * Modified by: Roberto Gentili 7 | * 8 | * Modifications hosted at: https://github.com/burningwave/jvm-driver 9 | * 10 | * -- 11 | * 12 | * The MIT License (MIT) 13 | * 14 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 17 | * documentation files (the "Software"), to deal in the Software without restriction, including without 18 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 19 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 20 | * conditions: 21 | * 22 | * The above copyright notice and this permission notice shall be included in all copies or substantial 23 | * portions of the Software. 24 | * 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 26 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 27 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 28 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 29 | * OR OTHER DEALINGS IN THE SOFTWARE. 30 | */ 31 | package io.github.toolfactory.jvm; 32 | 33 | public interface Info { 34 | public final static int[] CRITICAL_VERSIONS = {7, 9, 14, 17, 20, 21, 22, 24, 25}; 35 | 36 | 37 | public boolean isCompressedOopsOffOn64BitHotspot(); 38 | 39 | public boolean isCompressedOopsOffOn64Bit(); 40 | 41 | public boolean is32Bit(); 42 | 43 | public boolean is64Bit(); 44 | 45 | public int getVersion(); 46 | 47 | 48 | public static class Provider { 49 | 50 | public static Info getInfoInstance() { 51 | return InfoImpl.getInstance(); 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/InfoImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm; 28 | 29 | 30 | import java.lang.reflect.Method; 31 | 32 | 33 | public class InfoImpl implements Info { 34 | 35 | public String osArch; 36 | public String operatingSystemName; 37 | public String sunArchDataModel; 38 | public boolean is64Bit; 39 | public boolean is64BitHotspot; 40 | public boolean is32Bit; 41 | public boolean compressedRefsEnabled; 42 | public int version; 43 | 44 | public InfoImpl() { 45 | init(); 46 | } 47 | 48 | public static InfoImpl getInstance() { 49 | return Holder.getWithinInstance(); 50 | } 51 | 52 | public static InfoImpl create() { 53 | return new InfoImpl(); 54 | } 55 | 56 | private void init() { 57 | String version = System.getProperty("java.version"); 58 | osArch = System.getProperty("os.arch"); 59 | operatingSystemName = System.getProperty("os.name"); 60 | sunArchDataModel = System.getProperty("sun.arch.data.model"); 61 | if(version.startsWith("1.")) { 62 | version = version.substring(2, 3); 63 | } else { 64 | int separatorIdx = version.indexOf("."); 65 | if(separatorIdx != -1) { 66 | version = version.substring(0, separatorIdx); 67 | } else { 68 | separatorIdx = version.indexOf("-"); 69 | if(separatorIdx != -1) { 70 | version = version.substring(0, separatorIdx); 71 | } 72 | } 73 | } 74 | this.version = Integer.parseInt(version); 75 | boolean is64Bit = false; 76 | boolean is32Bit = false; 77 | if (sunArchDataModel != null) { 78 | is64Bit = sunArchDataModel.contains("64"); 79 | is32Bit = sunArchDataModel.contains("32"); 80 | } else { 81 | if (osArch != null && osArch.contains("64")) { 82 | is64Bit = true; 83 | } else { 84 | is64Bit = false; 85 | } 86 | } 87 | boolean compressedOops = false; 88 | boolean is64BitHotspot = false; 89 | 90 | if (is64Bit) { 91 | try { 92 | final Class beanClazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean"); 93 | final Object hotSpotBean = Class.forName("java.lang.management.ManagementFactory").getMethod("getPlatformMXBean", Class.class) 94 | .invoke(null, beanClazz); 95 | if (hotSpotBean != null) { 96 | is64BitHotspot = true; 97 | final Method getVMOptionMethod = beanClazz.getMethod("getVMOption", String.class); 98 | try { 99 | final Object vmOption = getVMOptionMethod.invoke(hotSpotBean, "UseCompressedOops"); 100 | compressedOops = Boolean.parseBoolean(vmOption.getClass().getMethod("getValue").invoke(vmOption).toString()); 101 | } catch (ReflectiveOperationException | RuntimeException e) { 102 | is64BitHotspot = false; 103 | } 104 | } 105 | } catch (ReflectiveOperationException | RuntimeException e) { 106 | is64BitHotspot = false; 107 | } 108 | } 109 | this.is64Bit = is64Bit; 110 | this.is64BitHotspot = is64BitHotspot; 111 | this.is32Bit = is32Bit; 112 | this.compressedRefsEnabled = compressedOops; 113 | } 114 | 115 | @Override 116 | public boolean isCompressedOopsOffOn64BitHotspot() { 117 | return is64BitHotspot && !compressedRefsEnabled; 118 | } 119 | 120 | @Override 121 | public boolean isCompressedOopsOffOn64Bit() { 122 | return is64Bit && !compressedRefsEnabled; 123 | } 124 | 125 | @Override 126 | public boolean is32Bit() { 127 | return is32Bit; 128 | } 129 | 130 | @Override 131 | public boolean is64Bit() { 132 | return is64Bit; 133 | } 134 | 135 | @Override 136 | public int getVersion() { 137 | return version; 138 | } 139 | 140 | private static class Holder { 141 | private static final InfoImpl INSTANCE = InfoImpl.create(); 142 | 143 | private static InfoImpl getWithinInstance() { 144 | return INSTANCE; 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/NativeDriver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm; 28 | 29 | 30 | import java.util.Map; 31 | 32 | import io.github.toolfactory.jvm.function.catalog.AllocateInstanceFunction; 33 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplier; 34 | import io.github.toolfactory.jvm.function.catalog.ConsulterSupplyFunction; 35 | import io.github.toolfactory.jvm.function.catalog.GetFieldValueFunction; 36 | import io.github.toolfactory.jvm.function.catalog.GetLoadedClassesRetrieverFunction; 37 | import io.github.toolfactory.jvm.function.catalog.GetLoadedPackagesFunction; 38 | import io.github.toolfactory.jvm.function.catalog.SetAccessibleFunction; 39 | import io.github.toolfactory.jvm.function.catalog.SetFieldValueFunction; 40 | import io.github.toolfactory.jvm.util.ObjectProvider; 41 | import io.github.toolfactory.jvm.util.ObjectProvider.BuildingException; 42 | 43 | 44 | @SuppressWarnings({"unchecked"}) 45 | public class NativeDriver extends DefaultDriver { 46 | 47 | 48 | @Override 49 | protected Map functionsToMap() { 50 | Map context = super.functionsToMap(); 51 | ObjectProvider objectProvider = ObjectProvider.get(context); 52 | objectProvider.markToBeInitializedViaExceptionHandler(ConsulterSupplier.class, context); 53 | ObjectProvider.get(context).markToBeInitializedViaExceptionHandler(ConsulterSupplyFunction.class, context); 54 | objectProvider.markToBeInitializedViaExceptionHandler(SetFieldValueFunction.class, context); 55 | objectProvider.markToBeInitializedViaExceptionHandler(io.github.toolfactory.jvm.function.catalog.AllocateInstanceFunction.class, context); 56 | objectProvider.markToBeInitializedViaExceptionHandler(GetFieldValueFunction.class, context); 57 | objectProvider.markToBeInitializedViaExceptionHandler(SetAccessibleFunction.class, context); 58 | objectProvider.markToBeInitializedViaExceptionHandler(GetLoadedPackagesFunction.class, context); 59 | objectProvider.markToBeInitializedViaExceptionHandler(GetLoadedClassesRetrieverFunction.class, context); 60 | ObjectProvider.setExceptionHandler( 61 | context, 62 | new ObjectProvider.ExceptionHandler() { 63 | @Override 64 | public T handle(ObjectProvider objectProvider, Class clazz, Map context, 65 | BuildingException exception) { 66 | if (objectProvider.isMarkedToBeInitializedViaExceptionHandler(exception)) { 67 | if (clazz.isAssignableFrom(getConsulterSupplierFunctionClass())) { 68 | return (T)objectProvider.getOrBuildObject(getConsulterSupplierFunctionClass(), context); 69 | } 70 | if (clazz.isAssignableFrom(getConsulterSupplyFunctionClass())) { 71 | return (T)objectProvider.getOrBuildObject(getConsulterSupplyFunctionClass(), context); 72 | } 73 | if (clazz.isAssignableFrom(getThrowExceptionFunctionClass())) { 74 | return (T)objectProvider.getOrBuildObject(getThrowExceptionFunctionClass(), context); 75 | } 76 | if (clazz.isAssignableFrom(getSetFieldValueFunctionClass())) { 77 | return (T)objectProvider.getOrBuildObject(getSetFieldValueFunctionClass(), context); 78 | } 79 | if (clazz.isAssignableFrom(getAllocateInstanceFunctionClass())) { 80 | return (T)objectProvider.getOrBuildObject(getAllocateInstanceFunctionClass(), context); 81 | } 82 | if (clazz.isAssignableFrom(getSetAccessibleFunctionClass())) { 83 | return (T)objectProvider.getOrBuildObject(getSetAccessibleFunctionClass(), context); 84 | } 85 | if (clazz.isAssignableFrom(getGetFieldValueFunctionClass())) { 86 | return (T)objectProvider.getOrBuildObject(getGetFieldValueFunctionClass(), context); 87 | } 88 | if (clazz.isAssignableFrom(getGetLoadedClassesRetrieverFunctionClass())) { 89 | return (T)objectProvider.getOrBuildObject(getGetLoadedClassesRetrieverFunctionClass(), context); 90 | } 91 | if (clazz.isAssignableFrom(getGetLoadedPackagesFunctionClass())) { 92 | return (T)objectProvider.getOrBuildObject(getGetLoadedPackagesFunctionClass(), context); 93 | } 94 | } 95 | throw exception; 96 | } 97 | } 98 | ); 99 | return context; 100 | } 101 | 102 | 103 | protected Class getConsulterSupplierFunctionClass() { 104 | return ConsulterSupplier.Native.class; 105 | } 106 | 107 | 108 | @Override 109 | protected Class getConsulterSupplyFunctionClass() { 110 | return ConsulterSupplyFunction.Native.class; 111 | } 112 | 113 | 114 | @Override 115 | protected Class getGetLoadedPackagesFunctionClass() { 116 | return GetLoadedPackagesFunction.Native.class; 117 | } 118 | 119 | 120 | @Override 121 | protected Class getGetLoadedClassesRetrieverFunctionClass() { 122 | return GetLoadedClassesRetrieverFunction.Native.class; 123 | } 124 | 125 | 126 | @Override 127 | protected Class getSetFieldValueFunctionClass() { 128 | return SetFieldValueFunction.Native.class; 129 | } 130 | 131 | 132 | @Override 133 | protected Class getGetFieldValueFunctionClass() { 134 | return GetFieldValueFunction.Native.class; 135 | } 136 | 137 | 138 | @Override 139 | protected Class getAllocateInstanceFunctionClass() { 140 | return AllocateInstanceFunction.Native.class; 141 | } 142 | 143 | 144 | @Override 145 | protected Class getSetAccessibleFunctionClass() { 146 | return SetAccessibleFunction.Native.class; 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/InitializeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function; 28 | 29 | public class InitializeException extends Exception { 30 | 31 | private static final long serialVersionUID = 5712614840976686057L; 32 | 33 | public InitializeException(String message, Throwable cause) { 34 | super(message, cause); 35 | } 36 | 37 | public InitializeException(String message) { 38 | super(message); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/AllocateInstanceFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.util.Map; 31 | 32 | import io.github.toolfactory.jvm.function.InitializeException; 33 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 34 | import io.github.toolfactory.jvm.util.ObjectProvider; 35 | import io.github.toolfactory.jvm.util.Strings; 36 | import io.github.toolfactory.narcissus.Narcissus; 37 | 38 | 39 | @SuppressWarnings("all") 40 | public interface AllocateInstanceFunction extends ThrowingFunction, Object, Throwable> { 41 | 42 | public static class ForJava7 implements AllocateInstanceFunction { 43 | final sun.misc.Unsafe unsafe; 44 | 45 | public ForJava7(Map context) { 46 | ObjectProvider functionProvider = ObjectProvider.get(context); 47 | unsafe = functionProvider.getOrBuildObject(UnsafeSupplier.class, context).get(); 48 | } 49 | 50 | @Override 51 | public Object apply(Class input) throws InstantiationException { 52 | return unsafe.allocateInstance(input); 53 | } 54 | 55 | } 56 | 57 | public static interface Native extends AllocateInstanceFunction { 58 | 59 | public static class ForJava7 implements Native { 60 | 61 | public ForJava7(Map context) throws InitializeException { 62 | checkNativeEngine(); 63 | } 64 | 65 | protected void checkNativeEngine() throws InitializeException { 66 | if (!Narcissus.libraryLoaded) { 67 | throw new InitializeException( 68 | Strings.compile( 69 | "Could not initialize the native engine {}", 70 | io.github.toolfactory.narcissus.Narcissus.class.getName() 71 | ) 72 | ); 73 | } 74 | } 75 | 76 | @Override 77 | public Object apply(Class cls) { 78 | return io.github.toolfactory.narcissus.Narcissus.allocateInstance(cls); 79 | } 80 | 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/BuiltinClassLoaderClassSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.util.Map; 31 | 32 | import io.github.toolfactory.jvm.function.template.Supplier; 33 | import io.github.toolfactory.jvm.util.ObjectProvider; 34 | 35 | 36 | public interface BuiltinClassLoaderClassSupplier extends Supplier> { 37 | 38 | public static class ForJava7 implements BuiltinClassLoaderClassSupplier{ 39 | 40 | public ForJava7(Map context) {} 41 | 42 | @Override 43 | public Class get() { 44 | return null; 45 | } 46 | 47 | } 48 | 49 | public static class ForJava9 implements BuiltinClassLoaderClassSupplier{ 50 | protected Class cls; 51 | 52 | public ForJava9(Map context) throws Throwable { 53 | ObjectProvider functionProvider = ObjectProvider.get(context); 54 | cls = functionProvider.getOrBuildObject(GetClassByNameFunction.class, context).apply( 55 | "jdk.internal.loader.BuiltinClassLoader", false, ForJava9.class.getClassLoader(), ForJava9.class 56 | ); 57 | } 58 | 59 | @Override 60 | public Class get() { 61 | return cls; 62 | } 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/ClassLoaderDelegateClassSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | import java.lang.reflect.Method; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.Supplier; 36 | import io.github.toolfactory.jvm.util.Classes; 37 | import io.github.toolfactory.jvm.util.ObjectProvider; 38 | import io.github.toolfactory.jvm.util.Streams; 39 | 40 | 41 | public interface ClassLoaderDelegateClassSupplier extends Supplier> { 42 | 43 | public static class ForJava7 implements ClassLoaderDelegateClassSupplier { 44 | 45 | public ForJava7(Map context) {} 46 | 47 | @Override 48 | public Class get() { 49 | return null; 50 | } 51 | 52 | } 53 | 54 | public static class ForJava9 implements ClassLoaderDelegateClassSupplier { 55 | protected Class cls; 56 | 57 | public ForJava9(Map context) throws Throwable { 58 | loadClass(context); 59 | } 60 | 61 | protected void loadClass(Map context) throws Throwable, IOException { 62 | try ( 63 | InputStream inputStream = Classes.class.getResourceAsStream( 64 | "ClassLoaderDelegateForJDK9.bwc" 65 | ); 66 | ) { 67 | ObjectProvider functionProvider = ObjectProvider.get(context); 68 | cls = functionProvider.getOrBuildObject( 69 | DefineHookClassFunction.class, context 70 | ).apply( 71 | functionProvider.getOrBuildObject(BuiltinClassLoaderClassSupplier.class, context).get(), 72 | Streams.toByteArray(inputStream) 73 | ); 74 | } 75 | } 76 | 77 | @Override 78 | public Class get() { 79 | return cls; 80 | } 81 | 82 | } 83 | 84 | public static interface ForJava17 extends ClassLoaderDelegateClassSupplier { 85 | 86 | public static class ForSemeru extends ForJava9 implements ForJava17 { 87 | 88 | public ForSemeru(Map context) throws Throwable { 89 | super(context); 90 | } 91 | 92 | @Override 93 | protected void loadClass(Map context) throws Throwable, IOException { 94 | ObjectProvider functionProvider = ObjectProvider.get(context); 95 | Class thisClass = getClass(); 96 | ClassLoader thisClassClassLoader = thisClass.getClassLoader(); 97 | GetClassByNameFunction getClassByNameFunction = functionProvider.getOrBuildObject(GetClassByNameFunction.class, context); 98 | Class sharedSecretsClass = getClassByNameFunction.apply("jdk.internal.access.SharedSecrets", false, thisClassClassLoader, thisClass); 99 | Method getJavaLangAccessMethod = sharedSecretsClass.getDeclaredMethod("getJavaLangAccess"); 100 | Class javaLangAccessClass = getClassByNameFunction.apply("jdk.internal.access.JavaLangAccess", false, thisClassClassLoader, thisClass); 101 | MethodInvokeFunction methodInvoker = functionProvider.getOrBuildObject(MethodInvokeFunction.class, context); 102 | Object javaLangAccess = methodInvoker.apply(getJavaLangAccessMethod, null, null); 103 | methodInvoker.apply( 104 | javaLangAccessClass.getDeclaredMethod("addExportsToAllUnnamed", Module.class, String.class), 105 | javaLangAccess, 106 | new Object[] { 107 | java.lang.ModuleLayer.boot().findModule("java.base").get(), "jdk.internal.loader" 108 | } 109 | ); 110 | super.loadClass(context); 111 | } 112 | 113 | } 114 | 115 | 116 | } 117 | 118 | 119 | } 120 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConstructorInvokeFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.reflect.Constructor; 33 | import java.lang.reflect.Method; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.template.ThrowingBiFunction; 37 | import io.github.toolfactory.jvm.util.ObjectProvider; 38 | 39 | 40 | public interface ConstructorInvokeFunction extends ThrowingBiFunction, Object[], Object, Throwable> { 41 | 42 | public abstract static class Abst implements ConstructorInvokeFunction { 43 | 44 | protected MethodHandle methodHandle; 45 | 46 | @Override 47 | public Object apply(Constructor ctor, Object[] params) throws Throwable { 48 | return methodHandle.invokeWithArguments(ctor, params); 49 | } 50 | } 51 | 52 | public static class ForJava7 extends Abst { 53 | 54 | public ForJava7(Map context) throws Throwable { 55 | ObjectProvider functionProvider = ObjectProvider.get(context); 56 | Class accessorImplClass = Class.forName("sun.reflect.NativeConstructorAccessorImpl"); 57 | Method method = accessorImplClass.getDeclaredMethod("newInstance0", Constructor.class, Object[].class); 58 | ConsulterSupplyFunction getConsulterFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 59 | MethodHandles.Lookup consulter = getConsulterFunction.apply(accessorImplClass); 60 | method.setAccessible(true); 61 | methodHandle = consulter.unreflect(method); 62 | } 63 | 64 | } 65 | 66 | public static class ForJava9 extends Abst { 67 | 68 | public ForJava9(Map context) throws Throwable { 69 | ObjectProvider functionProvider = ObjectProvider.get(context); 70 | Class accessorImplClass = Class.forName(retrieveNativeAccessorClassName()); 71 | Method method = accessorImplClass.getDeclaredMethod("newInstance0", Constructor.class, Object[].class); 72 | ConsulterSupplyFunction getConsulterFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 73 | MethodHandles.Lookup consulter = getConsulterFunction.apply(accessorImplClass); 74 | methodHandle = consulter.unreflect(method); 75 | } 76 | 77 | protected String retrieveNativeAccessorClassName() { 78 | return "jdk.internal.reflect.NativeConstructorAccessorImpl"; 79 | } 80 | 81 | } 82 | 83 | public static class ForJava22 extends ForJava9 { 84 | 85 | public ForJava22(Map context) throws Throwable { 86 | super(context); 87 | } 88 | 89 | @Override 90 | protected String retrieveNativeAccessorClassName() { 91 | return "jdk.internal.reflect.DirectConstructorHandleAccessor$NativeAccessor"; 92 | } 93 | 94 | } 95 | 96 | } -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConsulterSupplyFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.io.InputStream; 31 | import java.lang.invoke.MethodHandle; 32 | import java.lang.invoke.MethodHandles; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | import io.github.toolfactory.jvm.util.Streams; 38 | import io.github.toolfactory.jvm.util.ThrowingFunctionAdapter; 39 | 40 | 41 | @SuppressWarnings("unchecked") 42 | public interface ConsulterSupplyFunction extends ThrowingFunction, MethodHandles.Lookup, Throwable> { 43 | 44 | public abstract static class Abst extends ThrowingFunctionAdapter, MethodHandles.Lookup, Throwable> implements ConsulterSupplyFunction { 45 | 46 | } 47 | 48 | public static class ForJava7 extends Abst, MethodHandles.Lookup, Throwable>> { 49 | public ForJava7(Map context) { 50 | ObjectProvider functionProvider = ObjectProvider.get(context); 51 | final MethodHandles.Lookup consulter = functionProvider.getOrBuildObject(ConsulterSupplier.class, context).get(); 52 | final MethodHandle privateLookupInMethodHandle = functionProvider.getOrBuildObject(PrivateLookupInMethodHandleSupplier.class, context).get(); 53 | setFunction( 54 | new ThrowingFunction, MethodHandles.Lookup, Throwable>() { 55 | @Override 56 | public MethodHandles.Lookup apply(Class cls) throws Throwable { 57 | return (MethodHandles.Lookup) privateLookupInMethodHandle.invokeWithArguments(consulter, cls); 58 | } 59 | } 60 | ); 61 | } 62 | 63 | @Override 64 | public MethodHandles.Lookup apply(Class input) throws Throwable { 65 | return function.apply(input); 66 | } 67 | 68 | } 69 | 70 | public static class ForJava9 extends Abst, MethodHandles.Lookup>> { 71 | 72 | public ForJava9(Map context) throws Throwable { 73 | ObjectProvider functionProvider = ObjectProvider.get(context); 74 | try ( 75 | InputStream inputStream = ConsulterSupplyFunction.class.getResourceAsStream( 76 | "ConsulterRetrieverForJDK9.bwc" 77 | ); 78 | ) { 79 | MethodHandle privateLookupInMethodHandle = functionProvider.getOrBuildObject(PrivateLookupInMethodHandleSupplier.class, context).get(); 80 | Class methodHandleWrapperClass = functionProvider.getOrBuildObject( 81 | DefineHookClassFunction.class, context 82 | ).apply(Class.class, Streams.toByteArray(inputStream)); 83 | functionProvider.getOrBuildObject(SetFieldValueFunction.class, context).accept( 84 | methodHandleWrapperClass, methodHandleWrapperClass.getDeclaredField("consulterRetriever"), 85 | privateLookupInMethodHandle 86 | ); 87 | empowerMainConsulter((MethodHandles.Lookup)methodHandleWrapperClass.getDeclaredField("mainConsulter").get(null), context); 88 | setFunction((java.util.function.Function, MethodHandles.Lookup>) 89 | functionProvider.getOrBuildObject(AllocateInstanceFunction.class, context).apply(methodHandleWrapperClass)); 90 | } 91 | } 92 | 93 | protected void empowerMainConsulter(MethodHandles.Lookup consulter, Map context) throws Throwable { 94 | 95 | } 96 | 97 | @Override 98 | public MethodHandles.Lookup apply(Class input) { 99 | return function.apply(input); 100 | } 101 | 102 | } 103 | 104 | public static interface ForJava17 extends ConsulterSupplyFunction { 105 | 106 | public static class ForSemeru extends ConsulterSupplyFunction.ForJava9 implements ForJava17 { 107 | 108 | public ForSemeru(Map context) throws Throwable { 109 | super(context); 110 | } 111 | 112 | @Override 113 | protected void empowerMainConsulter(MethodHandles.Lookup consulter, Map context) throws Throwable { 114 | sun.misc.Unsafe unsafe = ObjectProvider.get(context).getOrBuildObject(UnsafeSupplier.class, context).get(); 115 | unsafe.putInt(consulter, 20, -1); 116 | } 117 | 118 | 119 | @Override 120 | public MethodHandles.Lookup apply(Class input) { 121 | return function.apply(input); 122 | } 123 | } 124 | 125 | } 126 | 127 | public static interface Native extends ConsulterSupplyFunction { 128 | 129 | public static interface ForJava17 extends Native { 130 | 131 | public static class ForSemeru extends ConsulterSupplyFunction.ForJava17.ForSemeru implements ForJava17 { 132 | 133 | public ForSemeru(Map context) throws Throwable { 134 | super(context); 135 | } 136 | 137 | @Override 138 | protected void empowerMainConsulter(MethodHandles.Lookup consulter, Map context) throws Throwable { 139 | io.github.toolfactory.narcissus.Narcissus.setField( 140 | consulter, 141 | io.github.toolfactory.narcissus.Narcissus.findField(consulter.getClass(), "allowedModes"), 142 | -1 143 | ); 144 | } 145 | 146 | } 147 | 148 | } 149 | 150 | } 151 | 152 | public static interface Hybrid extends ConsulterSupplyFunction { 153 | 154 | public static interface ForJava17 extends Hybrid { 155 | 156 | public static class ForSemeru extends Native.ForJava17.ForSemeru implements Hybrid.ForJava17 { 157 | 158 | public ForSemeru(Map context) throws Throwable { 159 | super(context); 160 | } 161 | } 162 | } 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConvertToBuiltinClassLoaderFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | 38 | 39 | public interface ConvertToBuiltinClassLoaderFunction extends ThrowingFunction { 40 | 41 | public static class ForJava7 implements ConvertToBuiltinClassLoaderFunction { 42 | 43 | public ForJava7(Map context) {} 44 | 45 | @Override 46 | public ClassLoader apply(ClassLoader classLoader) { 47 | return null; 48 | } 49 | 50 | } 51 | 52 | public static class ForJava9 implements ConvertToBuiltinClassLoaderFunction { 53 | protected MethodHandles.Lookup consulter; 54 | protected Class builtinClassLoaderClass; 55 | protected MethodHandle classLoaderDelegateConstructor; 56 | 57 | public ForJava9(Map context) throws Throwable { 58 | ObjectProvider functionProvider = ObjectProvider.get(context); 59 | Class classLoaderDelegateClass = functionProvider.getOrBuildObject(ClassLoaderDelegateClassSupplier.class, context).get(); 60 | consulter = functionProvider.getOrBuildObject(DeepConsulterSupplyFunction.class, context).apply(classLoaderDelegateClass); 61 | Class builtinClassLoaderDelegateClass = functionProvider.getOrBuildObject(BuiltinClassLoaderClassSupplier.class, context).get(); 62 | classLoaderDelegateConstructor = consulter.findConstructor( 63 | classLoaderDelegateClass, 64 | MethodType.methodType( 65 | void.class, builtinClassLoaderDelegateClass, ClassLoader.class, MethodHandle.class 66 | ) 67 | ); 68 | builtinClassLoaderClass = functionProvider.getOrBuildObject(BuiltinClassLoaderClassSupplier.class, context).get(); 69 | } 70 | 71 | @Override 72 | public ClassLoader apply(ClassLoader classLoader) throws Throwable { 73 | if (builtinClassLoaderClass.isAssignableFrom(classLoader.getClass())) { 74 | return classLoader; 75 | } 76 | MethodHandle loadClassMethodHandle = consulter.findVirtual( 77 | classLoader.getClass(), 78 | "loadClass", 79 | MethodType.methodType( 80 | Class.class, String.class, boolean.class 81 | ) 82 | ); 83 | return (ClassLoader)classLoaderDelegateConstructor.invokeWithArguments(null, classLoader, loadClassMethodHandle); 84 | } 85 | 86 | } 87 | 88 | 89 | } 90 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/DefineHookClassFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.nio.ByteBuffer; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.template.ThrowingBiFunction; 37 | import io.github.toolfactory.jvm.util.JavaClass; 38 | import io.github.toolfactory.jvm.util.ObjectProvider; 39 | 40 | 41 | @SuppressWarnings("all") 42 | public interface DefineHookClassFunction extends ThrowingBiFunction, byte[], Class, Throwable> { 43 | 44 | public abstract static class Abst implements DefineHookClassFunction { 45 | protected MethodHandle defineHookClassMethodHandle; 46 | 47 | public Abst(Map context) { 48 | ObjectProvider functionProvider = ObjectProvider.get(context); 49 | } 50 | } 51 | 52 | 53 | public static class ForJava7 extends Abst { 54 | protected sun.misc.Unsafe unsafe; 55 | 56 | public ForJava7(Map context) throws NoSuchMethodException, IllegalAccessException, Throwable { 57 | super(context); 58 | ObjectProvider functionProvider = ObjectProvider.get(context); 59 | unsafe = functionProvider.getOrBuildObject(UnsafeSupplier.class, context).get(); 60 | defineHookClassMethodHandle = retrieveConsulter( 61 | functionProvider.getOrBuildObject(ConsulterSupplier.class, context).get(), 62 | functionProvider.getOrBuildObject(PrivateLookupInMethodHandleSupplier.class, context).get() 63 | ).findSpecial( 64 | unsafe.getClass(), 65 | "defineAnonymousClass", 66 | MethodType.methodType(Class.class, Class.class, byte[].class, Object[].class), 67 | unsafe.getClass() 68 | ); 69 | } 70 | 71 | public MethodHandles.Lookup retrieveConsulter(MethodHandles.Lookup consulter, MethodHandle privateLookupInMethodHandle) throws Throwable { 72 | return (MethodHandles.Lookup)privateLookupInMethodHandle.invokeWithArguments(consulter, unsafe.getClass()); 73 | } 74 | 75 | @Override 76 | public Class apply(Class clientClass, byte[] byteCode) throws Throwable { 77 | return (Class) defineHookClassMethodHandle.invokeWithArguments(unsafe, clientClass, byteCode, null); 78 | } 79 | 80 | } 81 | 82 | 83 | public static class ForJava9 extends ForJava7 { 84 | 85 | public ForJava9(Map context) throws NoSuchMethodException, IllegalAccessException, Throwable { 86 | super(context); 87 | } 88 | 89 | @Override 90 | public MethodHandles.Lookup retrieveConsulter(MethodHandles.Lookup consulter, MethodHandle lookupMethod) throws Throwable { 91 | return (MethodHandles.Lookup)lookupMethod.invokeWithArguments(unsafe.getClass(), consulter); 92 | } 93 | 94 | } 95 | 96 | 97 | public static class ForJava17 extends Abst { 98 | protected MethodHandle privateLookupInMethodHandle; 99 | protected MethodHandles.Lookup consulter; 100 | 101 | public ForJava17(Map context) throws NoSuchMethodException, IllegalAccessException { 102 | super(context); 103 | ObjectProvider functionProvider = ObjectProvider.get(context); 104 | consulter = functionProvider.getOrBuildObject(ConsulterSupplier.class, context).get(); 105 | defineHookClassMethodHandle = consulter.findSpecial( 106 | MethodHandles.Lookup.class, 107 | "defineClass", 108 | MethodType.methodType(Class.class, byte[].class), 109 | MethodHandles.Lookup.class 110 | ); 111 | privateLookupInMethodHandle = functionProvider.getOrBuildObject(PrivateLookupInMethodHandleSupplier.class, context).get(); 112 | } 113 | 114 | 115 | @Override 116 | public Class apply(Class clientClass, byte[] byteCode) throws Throwable { 117 | MethodHandles.Lookup lookup = (MethodHandles.Lookup)privateLookupInMethodHandle.invokeWithArguments(clientClass, consulter); 118 | try { 119 | return (Class) defineHookClassMethodHandle.invokeWithArguments(lookup, byteCode); 120 | } catch (LinkageError exc) { 121 | try { 122 | return Class.forName( 123 | JavaClass.create(ByteBuffer.wrap(byteCode)).getName() 124 | ); 125 | } catch (Throwable excTwo) { 126 | throw exc; 127 | } 128 | } 129 | } 130 | 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetClassByNameFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingQuadFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | 38 | 39 | public interface GetClassByNameFunction extends ThrowingQuadFunction, Class, Throwable> { 40 | 41 | public abstract class Abst implements GetClassByNameFunction { 42 | protected MethodHandle classFinder; 43 | 44 | public Abst(Map context) throws Throwable { 45 | classFinder = retrieveClassFinder(context); 46 | } 47 | 48 | protected abstract MethodHandle retrieveClassFinder(final Map context) throws NoSuchMethodException, IllegalAccessException, Throwable; 49 | 50 | } 51 | 52 | public static class ForJava7 extends Abst { 53 | 54 | public ForJava7(Map context) throws Throwable { 55 | super(context); 56 | } 57 | 58 | @Override 59 | protected MethodHandle retrieveClassFinder(Map context) throws Throwable { 60 | ObjectProvider functionProvider = ObjectProvider.get(context); 61 | MethodHandles.Lookup consulter = functionProvider.getOrBuildObject(DeepConsulterSupplyFunction.class, context).apply(Class.class); 62 | return consulter.findStatic( 63 | Class.class, "forName0", 64 | MethodType.methodType(Class.class, String.class, boolean.class, ClassLoader.class, Class.class) 65 | ); 66 | } 67 | 68 | @Override 69 | public Class apply(String className, Boolean initialize, ClassLoader classLoader, Class caller) throws Throwable { 70 | return (Class)classFinder.invokeWithArguments(className, initialize.booleanValue(), classLoader, caller); 71 | } 72 | 73 | public static class ForSemeru extends Abst { 74 | 75 | public ForSemeru(Map context) throws Throwable { 76 | super(context); 77 | } 78 | 79 | @Override 80 | protected MethodHandle retrieveClassFinder(Map context) throws Throwable { 81 | ObjectProvider functionProvider = ObjectProvider.get(context); 82 | MethodHandles.Lookup consulter = functionProvider.getOrBuildObject(DeepConsulterSupplyFunction.class, context).apply(Class.class); 83 | return consulter.findStatic( 84 | Class.class, "forNameImpl", 85 | MethodType.methodType(Class.class, String.class, boolean.class, ClassLoader.class) 86 | ); 87 | } 88 | 89 | @Override 90 | public Class apply(String className, Boolean initialize, ClassLoader classLoader, Class caller) throws Throwable { 91 | return (Class)classFinder.invokeWithArguments(className, initialize, classLoader); 92 | } 93 | 94 | } 95 | 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetDeclaredConstructorsFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.lang.reflect.Constructor; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 37 | import io.github.toolfactory.jvm.util.ObjectProvider; 38 | 39 | 40 | public interface GetDeclaredConstructorsFunction extends ThrowingFunction, Constructor[], Throwable> { 41 | 42 | public abstract static class Abst implements GetDeclaredConstructorsFunction { 43 | protected MethodHandle methodHandle; 44 | 45 | protected Abst(Map context) {} 46 | } 47 | 48 | public static class ForJava7 extends Abst { 49 | 50 | public ForJava7(Map context) throws Throwable { 51 | super(context); 52 | ObjectProvider functionProvider = ObjectProvider.get(context); 53 | ConsulterSupplyFunction getConsulterFunction = 54 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 55 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 56 | methodHandle = consulter.findSpecial( 57 | Class.class, 58 | "getDeclaredConstructors0", 59 | MethodType.methodType(Constructor[].class, boolean.class), 60 | Class.class 61 | ); 62 | } 63 | 64 | @Override 65 | public Constructor[] apply(Class input) throws Throwable { 66 | return (Constructor[]) methodHandle.invokeWithArguments(input, false); 67 | } 68 | 69 | 70 | public static class ForSemeru extends Abst { 71 | 72 | public ForSemeru(Map context) throws Throwable { 73 | super(context); 74 | ObjectProvider functionProvider = ObjectProvider.get(context); 75 | ConsulterSupplyFunction getConsulterFunction = 76 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 77 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 78 | methodHandle = consulter.findSpecial( 79 | Class.class, 80 | "getDeclaredConstructorsImpl", 81 | MethodType.methodType(Constructor[].class), 82 | Class.class 83 | ); 84 | } 85 | 86 | @Override 87 | public Constructor[] apply(Class cls) throws Throwable { 88 | return (Constructor[])methodHandle.invokeWithArguments(cls); 89 | } 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetDeclaredFieldFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.reflect.Field; 31 | import java.util.Map; 32 | 33 | import io.github.toolfactory.jvm.function.template.ThrowingBiFunction; 34 | import io.github.toolfactory.jvm.util.ObjectProvider; 35 | import io.github.toolfactory.jvm.util.Strings; 36 | 37 | 38 | public interface GetDeclaredFieldFunction extends ThrowingBiFunction, String, Field, Throwable> { 39 | 40 | public static class ForJava7 implements GetDeclaredFieldFunction { 41 | protected GetDeclaredFieldsFunction getDeclaredFields; 42 | 43 | public ForJava7(Map context) { 44 | ObjectProvider functionProvider = ObjectProvider.get(context); 45 | getDeclaredFields = functionProvider.getOrBuildObject(GetDeclaredFieldsFunction.class, context); 46 | } 47 | 48 | @Override 49 | public Field apply(Class cls, String name) throws Throwable { 50 | for (Field field : getDeclaredFields.apply(cls)) { 51 | if (field.getName().equals(name)) { 52 | return field; 53 | } 54 | } 55 | throw new NoSuchFieldException( 56 | Strings.compile("Field named {} not found in the class {}", name, cls.getName()) 57 | ); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetDeclaredFieldsFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.lang.reflect.Field; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 37 | import io.github.toolfactory.jvm.util.ObjectProvider; 38 | 39 | 40 | public interface GetDeclaredFieldsFunction extends ThrowingFunction, Field[], Throwable> { 41 | 42 | public abstract static class Abst implements GetDeclaredFieldsFunction{ 43 | protected MethodHandle methodHandle; 44 | 45 | protected Abst(Map context) {} 46 | 47 | } 48 | 49 | public static class ForJava7 extends Abst { 50 | 51 | public ForJava7(Map context) throws Throwable { 52 | super(context); 53 | ObjectProvider functionProvider = ObjectProvider.get(context); 54 | ConsulterSupplyFunction getConsulterFunction = 55 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 56 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 57 | methodHandle = consulter.findSpecial( 58 | Class.class, 59 | "getDeclaredFields0", 60 | MethodType.methodType(Field[].class, boolean.class), 61 | Class.class 62 | ); 63 | } 64 | 65 | @Override 66 | public Field[] apply(Class cls) throws Throwable { 67 | return (Field[])methodHandle.invokeWithArguments(cls, false); 68 | } 69 | 70 | public static class ForSemeru extends Abst { 71 | 72 | public ForSemeru(Map context) throws Throwable { 73 | super(context); 74 | ObjectProvider functionProvider = ObjectProvider.get(context); 75 | ConsulterSupplyFunction getConsulterFunction = 76 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 77 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 78 | methodHandle = consulter.findSpecial( 79 | Class.class, 80 | "getDeclaredFieldsImpl", 81 | MethodType.methodType(Field[].class), 82 | Class.class 83 | ); 84 | } 85 | 86 | @Override 87 | public Field[] apply(Class cls) throws Throwable { 88 | return (Field[])methodHandle.invokeWithArguments(cls); 89 | } 90 | } 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetDeclaredMethodFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.reflect.Method; 31 | import java.util.ArrayList; 32 | import java.util.Collection; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingTriFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | import io.github.toolfactory.jvm.util.Strings; 38 | 39 | 40 | public interface GetDeclaredMethodFunction extends ThrowingTriFunction, String, Class[], Method, Throwable> { 41 | 42 | public static class ForJava7 implements GetDeclaredMethodFunction { 43 | protected GetDeclaredMethodsFunction getDeclaredMethodsFunction; 44 | 45 | public ForJava7(Map context) { 46 | ObjectProvider functionProvider = ObjectProvider.get(context); 47 | getDeclaredMethodsFunction = functionProvider.getOrBuildObject(GetDeclaredMethodsFunction.class, context); 48 | } 49 | 50 | @Override 51 | public Method apply(Class cls, String name, Class[] paramTypes) throws Throwable { 52 | if (paramTypes == null) { 53 | paramTypes = new Class[0]; 54 | } 55 | for (Method method : getDeclaredMethodsFunction.apply(cls)) { 56 | if (method.getName().equals(name)) { 57 | if (paramTypes.length == method.getParameterTypes().length) { 58 | Method toRet = method; 59 | Class[] parameterTypes = method.getParameterTypes(); 60 | for (int i = 0; i < parameterTypes.length; i++) { 61 | if (!parameterTypes[i].equals(paramTypes[i])) { 62 | toRet = null; 63 | break; 64 | } 65 | } 66 | if (toRet != null) { 67 | return toRet; 68 | } 69 | } 70 | } 71 | } 72 | Collection classNames = new ArrayList<>(); 73 | for (Class paramType : paramTypes) { 74 | classNames.add(paramType.getName()); 75 | } 76 | throw new NoSuchMethodException( 77 | Strings.compile("Method {}({}) not found in the class {}", name, Strings.join(", ", classNames), cls.getName()) 78 | ); 79 | } 80 | 81 | 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetDeclaredMethodsFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.lang.reflect.Method; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 37 | import io.github.toolfactory.jvm.util.ObjectProvider; 38 | 39 | 40 | public interface GetDeclaredMethodsFunction extends ThrowingFunction, Method[], Throwable> { 41 | 42 | public abstract static class Abst implements GetDeclaredMethodsFunction { 43 | protected MethodHandle methodHandle; 44 | 45 | protected Abst(Map context) {} 46 | 47 | } 48 | 49 | public static class ForJava7 extends Abst { 50 | 51 | public ForJava7(Map context) throws Throwable { 52 | super(context); 53 | ObjectProvider functionProvider = ObjectProvider.get(context); 54 | ConsulterSupplyFunction getConsulterFunction = 55 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 56 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 57 | methodHandle = consulter.findSpecial( 58 | Class.class, 59 | "getDeclaredMethods0", 60 | MethodType.methodType(Method[].class, boolean.class), 61 | Class.class 62 | ); 63 | } 64 | 65 | @Override 66 | public Method[] apply(Class input) throws Throwable { 67 | return (Method[]) methodHandle.invokeWithArguments(input, false); 68 | } 69 | 70 | public static class ForSemeru extends Abst { 71 | 72 | public ForSemeru(Map context) throws Throwable { 73 | super(context); 74 | ObjectProvider functionProvider = ObjectProvider.get(context); 75 | ConsulterSupplyFunction getConsulterFunction = 76 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 77 | MethodHandles.Lookup consulter = getConsulterFunction.apply(Class.class); 78 | methodHandle = consulter.findSpecial( 79 | Class.class, 80 | "getDeclaredMethodsImpl", 81 | MethodType.methodType(Method[].class), 82 | Class.class 83 | ); 84 | } 85 | 86 | @Override 87 | public Method[] apply(Class cls) throws Throwable { 88 | return (Method[])methodHandle.invokeWithArguments(cls); 89 | } 90 | } 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetLoadedPackagesFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.reflect.Field; 31 | import java.util.Map; 32 | 33 | import io.github.toolfactory.jvm.function.InitializeException; 34 | import io.github.toolfactory.jvm.function.template.Function; 35 | import io.github.toolfactory.jvm.util.ObjectProvider; 36 | import io.github.toolfactory.jvm.util.Strings; 37 | import io.github.toolfactory.narcissus.Narcissus; 38 | 39 | 40 | @SuppressWarnings("all") 41 | public interface GetLoadedPackagesFunction extends Function> { 42 | 43 | public static class ForJava7 implements GetLoadedPackagesFunction { 44 | protected sun.misc.Unsafe unsafe; 45 | protected Long fieldOffset; 46 | 47 | public ForJava7(Map context) throws Throwable { 48 | ObjectProvider functionProvider = ObjectProvider.get(context); 49 | unsafe = functionProvider.getOrBuildObject(UnsafeSupplier.class, context).get(); 50 | GetDeclaredFieldFunction getDeclaredFieldFunction = functionProvider.getOrBuildObject(GetDeclaredFieldFunction.class, context); 51 | fieldOffset = unsafe.objectFieldOffset( 52 | getDeclaredFieldFunction.apply(ClassLoader.class, "packages") 53 | ); 54 | } 55 | 56 | @Override 57 | public Map apply(ClassLoader classLoader) { 58 | return (Map)unsafe.getObject(classLoader, fieldOffset); 59 | } 60 | 61 | } 62 | 63 | 64 | public static interface Native extends GetLoadedPackagesFunction { 65 | 66 | public static class ForJava7 implements Native { 67 | Field packagesField; 68 | 69 | public ForJava7(Map context) throws Throwable { 70 | checkNativeEngine(); 71 | ObjectProvider functionProvider = ObjectProvider.get(context); 72 | GetDeclaredFieldFunction getDeclaredFieldFunction = functionProvider.getOrBuildObject(GetDeclaredFieldFunction.class, context); 73 | packagesField = getDeclaredFieldFunction.apply(ClassLoader.class, "packages"); 74 | } 75 | 76 | protected void checkNativeEngine() throws InitializeException { 77 | if (!Narcissus.libraryLoaded) { 78 | throw new InitializeException( 79 | Strings.compile( 80 | "Could not initialize the native engine {}", 81 | io.github.toolfactory.narcissus.Narcissus.class.getName() 82 | ) 83 | ); 84 | } 85 | } 86 | 87 | @Override 88 | public Map apply(ClassLoader classLoader) { 89 | return (Map)io.github.toolfactory.narcissus.Narcissus.getField(classLoader, packagesField); 90 | } 91 | } 92 | 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/GetPackageFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingBiFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | 38 | 39 | @SuppressWarnings("all") 40 | public interface GetPackageFunction extends ThrowingBiFunction { 41 | 42 | 43 | public static class ForJava7 implements GetPackageFunction{ 44 | 45 | public ForJava7(Map context) {} 46 | 47 | 48 | @Override 49 | public Package apply(ClassLoader inputOne, String packageName) { 50 | return Package.getPackage(packageName); 51 | } 52 | 53 | } 54 | 55 | public static class ForJava9 implements GetPackageFunction{ 56 | protected MethodHandle methodHandle; 57 | 58 | public ForJava9(Map context) throws Throwable { 59 | ObjectProvider functionProvider = ObjectProvider.get(context); 60 | ConsulterSupplyFunction consulterSupplyFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 61 | MethodHandles.Lookup classLoaderConsulter = consulterSupplyFunction.apply(ClassLoader.class); 62 | MethodType methodType = MethodType.methodType(Package.class, String.class); 63 | methodHandle = classLoaderConsulter.findSpecial(ClassLoader.class, "getDefinedPackage", methodType, ClassLoader.class); 64 | } 65 | 66 | @Override 67 | public Package apply(ClassLoader classLoader, String packageName) throws Throwable { 68 | return (Package)methodHandle.invokeExact(classLoader, packageName); 69 | } 70 | 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/MethodInvokeFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.reflect.Method; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.ThrowingTriFunction; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | 38 | 39 | public interface MethodInvokeFunction extends ThrowingTriFunction { 40 | 41 | public static class Abst implements MethodInvokeFunction { 42 | protected MethodHandle methodHandle; 43 | 44 | 45 | @Override 46 | public Object apply(Method method, Object target, Object[] params) throws Throwable { 47 | return methodHandle.invokeWithArguments(method, target, params); 48 | } 49 | } 50 | 51 | public static class ForJava7 extends Abst { 52 | 53 | public ForJava7(Map context) throws Throwable { 54 | Class accessorImplClass = Class.forName("sun.reflect.NativeMethodAccessorImpl"); 55 | Method invoker = accessorImplClass.getDeclaredMethod("invoke0", Method.class, Object.class, Object[].class); 56 | ObjectProvider functionProvider = ObjectProvider.get(context); 57 | ConsulterSupplyFunction consulterSupplyFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 58 | MethodHandles.Lookup consulter = consulterSupplyFunction.apply(accessorImplClass); 59 | functionProvider.getOrBuildObject(SetAccessibleFunction.class, context).accept(invoker, true); 60 | methodHandle = consulter.unreflect(invoker); 61 | } 62 | 63 | } 64 | 65 | public static class ForJava9 extends Abst { 66 | 67 | public ForJava9(Map context) throws Throwable { 68 | Class accessorImplClass = Class.forName(retrieveNativeAccessorClassName()); 69 | Method invoker = accessorImplClass.getDeclaredMethod("invoke0", Method.class, Object.class, Object[].class); 70 | ObjectProvider functionProvider = ObjectProvider.get(context); 71 | ConsulterSupplyFunction consulterSupplyFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); 72 | MethodHandles.Lookup consulter = consulterSupplyFunction.apply(accessorImplClass); 73 | functionProvider.getOrBuildObject(SetAccessibleFunction.class, context).accept(invoker, true); 74 | methodHandle = consulter.unreflect(invoker); 75 | } 76 | 77 | protected String retrieveNativeAccessorClassName() { 78 | return "jdk.internal.reflect.NativeMethodAccessorImpl"; 79 | } 80 | 81 | } 82 | 83 | public static class ForJava22 extends ForJava9 { 84 | 85 | public ForJava22(Map context) throws Throwable { 86 | super(context); 87 | } 88 | 89 | @Override 90 | protected String retrieveNativeAccessorClassName() { 91 | return "jdk.internal.reflect.DirectMethodHandleAccessor$NativeAccessor"; 92 | } 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/PrivateLookupInMethodHandleSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.invoke.MethodHandle; 31 | import java.lang.invoke.MethodHandles; 32 | import java.lang.invoke.MethodType; 33 | import java.util.Map; 34 | 35 | import io.github.toolfactory.jvm.function.template.Supplier; 36 | import io.github.toolfactory.jvm.util.ObjectProvider; 37 | 38 | 39 | public interface PrivateLookupInMethodHandleSupplier extends Supplier { 40 | 41 | public abstract static class Abst implements PrivateLookupInMethodHandleSupplier { 42 | 43 | protected MethodHandle methodHandle; 44 | 45 | @Override 46 | public MethodHandle get() { 47 | return methodHandle; 48 | } 49 | } 50 | 51 | public static class ForJava7 extends Abst { 52 | 53 | public ForJava7(Map context) throws NoSuchMethodException, IllegalAccessException { 54 | MethodHandles.Lookup consulter = ObjectProvider.get(context).getOrBuildObject(ConsulterSupplier.class, context).get(); 55 | methodHandle = consulter.findSpecial( 56 | MethodHandles.Lookup.class, "in", 57 | MethodType.methodType(MethodHandles.Lookup.class, Class.class), 58 | MethodHandles.Lookup.class 59 | ); 60 | } 61 | 62 | } 63 | 64 | 65 | public static class ForJava9 extends Abst { 66 | 67 | public ForJava9(Map context) throws NoSuchMethodException, IllegalAccessException { 68 | MethodHandles.Lookup consulter = ObjectProvider.get(context).getOrBuildObject(ConsulterSupplier.class, context).get(); 69 | methodHandle = consulter.findStatic( 70 | MethodHandles.class, "privateLookupIn", 71 | MethodType.methodType(MethodHandles.Lookup.class, Class.class, MethodHandles.Lookup.class) 72 | ); 73 | } 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/SetAccessibleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.io.InputStream; 31 | import java.lang.invoke.MethodHandle; 32 | import java.lang.reflect.AccessibleObject; 33 | import java.lang.reflect.Method; 34 | import java.util.Map; 35 | 36 | import io.github.toolfactory.jvm.function.InitializeException; 37 | import io.github.toolfactory.jvm.function.template.ThrowingBiConsumer; 38 | import io.github.toolfactory.jvm.util.ObjectProvider; 39 | import io.github.toolfactory.jvm.util.Streams; 40 | import io.github.toolfactory.jvm.util.Strings; 41 | import io.github.toolfactory.jvm.util.ThrowingBiConsumerAdapter; 42 | import io.github.toolfactory.narcissus.Narcissus; 43 | 44 | 45 | @SuppressWarnings("unchecked") 46 | public interface SetAccessibleFunction extends ThrowingBiConsumer { 47 | 48 | public abstract static class Abst extends ThrowingBiConsumerAdapter implements SetAccessibleFunction { 49 | 50 | public Abst(Map context) {} 51 | } 52 | 53 | public static class ForJava7 extends Abst> { 54 | 55 | public ForJava7(Map context) throws NoSuchMethodException, SecurityException, IllegalAccessException { 56 | super(context); 57 | final Method accessibleSetterMethod = AccessibleObject.class.getDeclaredMethod("setAccessible0", AccessibleObject.class, boolean.class); 58 | ObjectProvider functionProvider = ObjectProvider.get(context); 59 | final MethodHandle accessibleSetterMethodHandle = functionProvider.getOrBuildObject( 60 | ConsulterSupplier.class, context 61 | ).get().unreflect(accessibleSetterMethod); 62 | setFunction( 63 | new ThrowingBiConsumer() { 64 | @Override 65 | public void accept(AccessibleObject accessibleObject, Boolean flag) throws Throwable { 66 | accessibleSetterMethodHandle.invokeWithArguments(accessibleObject, flag); 67 | } 68 | } 69 | ); 70 | } 71 | 72 | @Override 73 | public void accept(AccessibleObject accessibleObject, Boolean flag) throws Throwable { 74 | function.accept(accessibleObject, flag); 75 | } 76 | 77 | } 78 | 79 | 80 | public static class ForJava9 extends Abst> { 81 | 82 | public ForJava9(Map context) throws Throwable { 83 | super(context); 84 | try ( 85 | InputStream inputStream = SetAccessibleFunction.class.getResourceAsStream( 86 | "AccessibleSetterInvokerForJDK9.bwc" 87 | ); 88 | ) { 89 | ObjectProvider functionProvider = ObjectProvider.get(context); 90 | Class methodHandleWrapperClass = functionProvider.getOrBuildObject( 91 | DefineHookClassFunction.class, context 92 | ).apply(AccessibleObject.class, Streams.toByteArray(inputStream)); 93 | functionProvider.getOrBuildObject(SetFieldValueFunction.class, context).accept( 94 | methodHandleWrapperClass, methodHandleWrapperClass.getDeclaredField("mainConsulter"), 95 | functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context).apply(methodHandleWrapperClass) 96 | ); 97 | setFunction((java.util.function.BiConsumer) 98 | functionProvider.getOrBuildObject(AllocateInstanceFunction.class, context).apply(methodHandleWrapperClass)); 99 | } 100 | } 101 | 102 | @Override 103 | public void accept(AccessibleObject accessibleObject, Boolean flag) { 104 | function.accept(accessibleObject, flag); 105 | } 106 | 107 | } 108 | 109 | public static interface Native extends SetAccessibleFunction { 110 | 111 | public static class ForJava7 extends Abst> implements Native { 112 | 113 | public ForJava7(Map context) throws Throwable { 114 | super(context); 115 | ObjectProvider functionProvider = ObjectProvider.get(context); 116 | final GetDeclaredMethodFunction getDeclaredMethodFunction = functionProvider.getOrBuildObject(GetDeclaredMethodFunction.class, context); 117 | Method accessibleSetterMethod = getDeclaredMethodFunction.apply(AccessibleObject.class, "setAccessible0", new Class[]{AccessibleObject.class, boolean.class}); 118 | final MethodHandle accessibleSetterMethodHandle = functionProvider.getOrBuildObject( 119 | ConsulterSupplier.class, context 120 | ).get().unreflect(accessibleSetterMethod); 121 | setFunction( 122 | new ThrowingBiConsumer() { 123 | @Override 124 | public void accept(AccessibleObject accessibleObject, Boolean flag) throws Throwable { 125 | accessibleSetterMethodHandle.invokeWithArguments(accessibleObject, flag); 126 | } 127 | } 128 | ); 129 | } 130 | 131 | @Override 132 | public void accept(AccessibleObject accessibleObject, Boolean flag) throws Throwable { 133 | function.accept(accessibleObject, flag); 134 | } 135 | } 136 | 137 | public static class ForJava9 extends Abst> implements Native { 138 | 139 | public ForJava9(Map context) throws Throwable { 140 | super(context); 141 | checkNativeEngine(); 142 | ObjectProvider functionProvider = ObjectProvider.get(context); 143 | GetDeclaredMethodFunction getDeclaredMethodFunction = functionProvider.getOrBuildObject(GetDeclaredMethodFunction.class, context); 144 | final Method accessibleSetterMethod = getDeclaredMethodFunction.apply(AccessibleObject.class, "setAccessible0", new Class[]{boolean.class}); 145 | setFunction( 146 | new ThrowingBiConsumer() { 147 | @Override 148 | public void accept(AccessibleObject target, Boolean flag) { 149 | io.github.toolfactory.narcissus.Narcissus.invokeMethod(target, accessibleSetterMethod, flag); 150 | } 151 | } 152 | ); 153 | } 154 | 155 | protected void checkNativeEngine() throws InitializeException { 156 | if (!Narcissus.libraryLoaded) { 157 | throw new InitializeException( 158 | Strings.compile( 159 | "Could not initialize the native engine {}", 160 | io.github.toolfactory.narcissus.Narcissus.class.getName() 161 | ) 162 | ); 163 | } 164 | } 165 | 166 | @Override 167 | public void accept(AccessibleObject accessibleObject, Boolean flag) throws Throwable { 168 | function.accept(accessibleObject, flag); 169 | } 170 | } 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/StopThreadFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | import java.lang.invoke.MethodHandle; 30 | import java.lang.reflect.Method; 31 | import java.util.Map; 32 | 33 | import io.github.toolfactory.jvm.function.template.ThrowingBiConsumer; 34 | import io.github.toolfactory.jvm.util.ObjectProvider; 35 | 36 | public interface StopThreadFunction extends ThrowingBiConsumer { 37 | 38 | public abstract static class Abst implements StopThreadFunction { 39 | 40 | protected MethodHandle methodHandle; 41 | 42 | @Override 43 | public void accept(Thread thread, Throwable threadDeath) throws Throwable { 44 | methodHandle.invokeWithArguments(thread, threadDeath); 45 | } 46 | 47 | } 48 | 49 | public static class ForJava7 extends Abst { 50 | 51 | public ForJava7(Map context) throws Throwable { 52 | final Method stopThreadMethod = retrieveStopThreadMethod(); 53 | ObjectProvider functionProvider = ObjectProvider.get(context); 54 | functionProvider.getOrBuildObject(SetAccessibleFunction.class, context).accept (stopThreadMethod, true); 55 | methodHandle = functionProvider.getOrBuildObject( 56 | ConsulterSupplier.class, context 57 | ).get().unreflect(stopThreadMethod); 58 | } 59 | 60 | protected Method retrieveStopThreadMethod() throws NoSuchMethodException, SecurityException { 61 | return Thread.class.getDeclaredMethod("stop0", Object.class); 62 | } 63 | 64 | public static class ForSemeru extends ForJava7 { 65 | 66 | public ForSemeru(Map context) throws Throwable { 67 | super(context); 68 | } 69 | 70 | @Override 71 | protected Method retrieveStopThreadMethod() throws NoSuchMethodException, SecurityException { 72 | return Thread.class.getDeclaredMethod("stopImpl", Throwable.class); 73 | } 74 | 75 | } 76 | 77 | } 78 | 79 | public static class ForJava20 extends ForJava7 { 80 | 81 | public ForJava20(Map context) throws Throwable { 82 | super(context); 83 | } 84 | 85 | @Override 86 | protected Method retrieveStopThreadMethod() throws NoSuchMethodException, SecurityException { 87 | return Thread.class.getDeclaredMethod("interrupt0"); 88 | } 89 | 90 | @Override 91 | public void accept(Thread thread, Throwable threadDeath) throws Throwable { 92 | methodHandle.invokeWithArguments(thread); 93 | } 94 | 95 | public static class ForSemeru extends ForJava20 { 96 | 97 | public ForSemeru(Map context) throws Throwable { 98 | super(context); 99 | } 100 | 101 | } 102 | 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/ThrowExceptionFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import io.github.toolfactory.jvm.function.template.Consumer; 31 | import io.github.toolfactory.jvm.util.Strings; 32 | 33 | 34 | @SuppressWarnings("all") 35 | public interface ThrowExceptionFunction extends Consumer { 36 | 37 | public T apply(Throwable exception); 38 | 39 | public T apply(String message, Object... placeHolderReplacements); 40 | 41 | public T apply(int startingLevel, String message, Object... placeHolderReplacements); 42 | 43 | public abstract static class Abst implements ThrowExceptionFunction { 44 | 45 | @Override 46 | public T apply(Throwable exception) { 47 | if (exception == null) { 48 | throw new NullPointerException("Input exception is null"); 49 | } 50 | accept(exception); 51 | return null; 52 | } 53 | 54 | @Override 55 | public T apply(String message, Object... placeHolderReplacements) { 56 | return apply(3, message, placeHolderReplacements); 57 | } 58 | 59 | @Override 60 | public T apply(int stackTraceStartingLevel, String message, Object... placeHolderReplacements) { 61 | Throwable exception = null; 62 | StackTraceElement[] stackTraceOfException = null; 63 | if (placeHolderReplacements == null || placeHolderReplacements.length == 0) { 64 | exception = new Exception(message); 65 | } else { 66 | exception = new Exception(Strings.compile(message, placeHolderReplacements)); 67 | } 68 | StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 69 | stackTraceOfException = new StackTraceElement[stackTrace.length - stackTraceStartingLevel]; 70 | System.arraycopy(stackTrace, stackTraceStartingLevel, stackTraceOfException, 0, stackTraceOfException.length); 71 | exception.setStackTrace(stackTraceOfException); 72 | accept(exception); 73 | return null; 74 | } 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/catalog/UnsafeSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.catalog; 28 | 29 | 30 | import java.lang.reflect.Field; 31 | import java.util.Map; 32 | 33 | import io.github.toolfactory.jvm.function.template.Supplier; 34 | import sun.misc.Unsafe; 35 | 36 | 37 | @SuppressWarnings("all") 38 | public interface UnsafeSupplier extends Supplier { 39 | 40 | public static class ForJava7 implements UnsafeSupplier { 41 | protected sun.misc.Unsafe unsafe; 42 | 43 | public ForJava7(Map context) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { 44 | Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); 45 | theUnsafeField.setAccessible(true); 46 | this.unsafe = (sun.misc.Unsafe)theUnsafeField.get(null); 47 | } 48 | 49 | @Override 50 | public sun.misc.Unsafe get() { 51 | return unsafe; 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/BiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface BiFunction { 31 | 32 | public R apply(I inputOne, J inputTwo); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/Consumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface Consumer { 31 | 32 | public void accept(I inputOne); 33 | 34 | } -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/Function.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface Function { 31 | 32 | public R apply(I input); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/Supplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface Supplier { 31 | 32 | public T get(); 33 | } 34 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/ThrowingBiConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface ThrowingBiConsumer { 31 | 32 | void accept(P0 p0, P1 p1) throws E; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/ThrowingBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface ThrowingBiFunction { 31 | 32 | public R apply(I inputOne, J inputTwo) throws E; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/ThrowingFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface ThrowingFunction { 31 | 32 | public R apply(I input) throws E; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/ThrowingQuadFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface ThrowingQuadFunction { 31 | 32 | public R apply(I inputOne, J inputTwo, K inputThree, L inputFour) throws E; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/ThrowingTriFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface ThrowingTriFunction { 31 | 32 | public R apply(I inputOne, J inputTwo, K inputThree) throws E; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/function/template/TriConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.function.template; 28 | 29 | 30 | public interface TriConsumer { 31 | 32 | public void accept(I inputOne, J inputTwo, K inputThree); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/BufferHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | 30 | import java.nio.Buffer; 31 | import java.nio.ByteBuffer; 32 | 33 | 34 | @SuppressWarnings("unchecked") 35 | public class BufferHandler { 36 | 37 | public static ByteBuffer shareContent(ByteBuffer byteBuffer) { 38 | ByteBuffer duplicated = duplicate(byteBuffer); 39 | if (position(byteBuffer) > 0) { 40 | flip(duplicated); 41 | } 42 | return duplicated; 43 | } 44 | 45 | public static T flip(T buffer) { 46 | return (T)((Buffer)buffer).flip(); 47 | } 48 | 49 | public static int position(T buffer) { 50 | return ((Buffer)buffer).position(); 51 | } 52 | 53 | public static ByteBuffer duplicate(ByteBuffer buffer) { 54 | return buffer.duplicate(); 55 | } 56 | 57 | public static int limit(T buffer) { 58 | return ((Buffer)buffer).limit(); 59 | } 60 | 61 | public static byte[] toByteArray(ByteBuffer byteBuffer) { 62 | byteBuffer = shareContent(byteBuffer); 63 | byte[] result = new byte[limit(byteBuffer)]; 64 | byteBuffer.get(result, 0, result.length); 65 | return result; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/CleanableSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | import io.github.toolfactory.jvm.function.template.Supplier; 30 | 31 | public interface CleanableSupplier extends Supplier { 32 | 33 | public void clear(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/JavaClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | 30 | import java.nio.ByteBuffer; 31 | 32 | 33 | public class JavaClass { 34 | protected int modifiers; 35 | protected String name; 36 | protected String simpleName; 37 | protected String packageName; 38 | protected String superClassName; 39 | protected String[] interfaceNames; 40 | 41 | protected JavaClass(ByteBuffer byteCode) { 42 | this(Classes.File.Reader.retrieveInfo(BufferHandler.shareContent(byteCode))); 43 | } 44 | 45 | protected JavaClass(byte[] byteCode) { 46 | this(Classes.File.Reader.retrieveInfo(byteCode)); 47 | } 48 | 49 | protected JavaClass(Classes.RawInfo rawInfo) { 50 | this.modifiers = rawInfo.modifiers; 51 | String rawName = rawInfo.getName(); 52 | String[] classNames = retrieveNames(rawName); 53 | packageName = classNames[0]; 54 | simpleName = classNames[1]; 55 | name = classNames[2]; 56 | superClassName = retrieveName(rawInfo.getSuperClassName()); 57 | String[] interfaceRawNames = rawInfo.getInterfaceNames(); 58 | interfaceNames = new String[interfaceRawNames.length]; 59 | for (int i = 0; i < interfaceRawNames.length; i++) { 60 | interfaceNames[i] = retrieveName(interfaceRawNames[i]); 61 | } 62 | 63 | } 64 | 65 | public static JavaClass create(ByteBuffer byteCode) { 66 | return new JavaClass(byteCode); 67 | } 68 | 69 | public static JavaClass create(byte[] byteCode) { 70 | return new JavaClass(byteCode); 71 | } 72 | 73 | private String retrieveName(String rawName) { 74 | try { 75 | return rawName.replace("/", "."); 76 | } catch (NullPointerException exc) { 77 | //for module-info.class 78 | return null; 79 | } 80 | } 81 | 82 | private String[] retrieveNames(String rawName) { 83 | String[] names = new String[3]; 84 | String rawPackageName = rawName.contains("/") ? 85 | rawName.substring(0, rawName.lastIndexOf("/")) : 86 | null; 87 | if (rawPackageName != null) { 88 | //packageName 89 | names[0] = rawPackageName.replace("/", "."); 90 | } 91 | //simpleName 92 | names[1] = rawName.contains("/") ? 93 | rawName.substring(rawName.lastIndexOf("/") + 1) : 94 | rawName; 95 | names[2] = (names[0] != null? names[0] + "." : "") + names[1]; 96 | return names; 97 | } 98 | 99 | public String getName() { 100 | return name; 101 | } 102 | 103 | public String getSimpleName() { 104 | return simpleName; 105 | } 106 | 107 | public String getPackageName() { 108 | return packageName; 109 | } 110 | 111 | public String getSuperClassName() { 112 | return superClassName; 113 | } 114 | 115 | public String[] getInterfaceNames() { 116 | return interfaceNames; 117 | } 118 | 119 | public boolean isPublic() { 120 | return (modifiers & 0x0001) != 0; 121 | } 122 | 123 | public boolean isFinal() { 124 | return (modifiers & 0x0010) != 0; 125 | } 126 | 127 | public boolean isInterface() { 128 | return (modifiers & 0x0200) != 0; 129 | } 130 | 131 | public boolean isAbstract() { 132 | return (modifiers & 0x0400) != 0; 133 | } 134 | 135 | public boolean isAnnotation() { 136 | return (modifiers & 0x2000) != 0; 137 | } 138 | 139 | public boolean isEnum() { 140 | return (modifiers & 0x4000) != 0; 141 | } 142 | 143 | } -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/Resources.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | import java.net.URL; 33 | import java.util.Enumeration; 34 | import java.util.LinkedHashMap; 35 | import java.util.Map; 36 | 37 | 38 | public class Resources { 39 | 40 | public static Map getAsInputStreams(String resourceRelativePath, ClassLoader... resourceClassLoaders) throws IOException { 41 | if (resourceClassLoaders == null || resourceClassLoaders.length == 0) { 42 | resourceClassLoaders = new ClassLoader[]{Thread.currentThread().getContextClassLoader()}; 43 | } 44 | Map streams = new LinkedHashMap<>(); 45 | for (ClassLoader classLoader : resourceClassLoaders) { 46 | Enumeration resources = classLoader.getResources(resourceRelativePath); 47 | while (resources.hasMoreElements()) { 48 | URL resourceURL = resources.nextElement(); 49 | streams.put( 50 | resourceURL, 51 | resourceURL.openStream() 52 | ); 53 | } 54 | } 55 | return streams; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/Streams.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | 30 | import java.io.ByteArrayOutputStream; 31 | import java.io.IOException; 32 | import java.io.InputStream; 33 | import java.io.OutputStream; 34 | 35 | 36 | public class Streams { 37 | 38 | public static byte[] toByteArray(InputStream inputStream) throws IOException { 39 | try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { 40 | copy(inputStream, outputStream); 41 | return outputStream.toByteArray(); 42 | } 43 | } 44 | 45 | public static void copy(InputStream input, OutputStream output) throws IOException { 46 | byte[] buffer = new byte[1024]; 47 | int bytesRead = 0; 48 | while (-1 != (bytesRead = input.read(buffer))) { 49 | output.write(buffer, 0, bytesRead); 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/Strings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | import java.util.Collection; 30 | import java.util.Iterator; 31 | 32 | public class Strings { 33 | 34 | public static String compile(String message, Object... arguments) { 35 | for (Object obj : arguments) { 36 | message = message.replaceFirst("\\{\\}", obj == null ? "null" : clear(obj.toString())); 37 | } 38 | return message; 39 | } 40 | 41 | private static String clear(String text) { 42 | return text 43 | .replace("\\", "\\\\\\") 44 | .replace("{", "\\{") 45 | .replace("}", "\\}") 46 | .replace("(", "\\(") 47 | .replace(")", "\\)") 48 | .replace(".", "\\.") 49 | .replace("$", "\\$"); 50 | } 51 | 52 | public static String join(String delimiter, Collection objs) { 53 | String joinedString = ""; 54 | Iterator objsItr = objs.iterator(); 55 | while (objsItr.hasNext()) { 56 | joinedString += objsItr.next().toString(); 57 | if (objsItr.hasNext()) { 58 | joinedString += delimiter; 59 | } 60 | } 61 | return joinedString; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/ThrowingBiConsumerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | import io.github.toolfactory.jvm.function.template.ThrowingBiConsumer; 30 | 31 | public abstract class ThrowingBiConsumerAdapter implements ThrowingBiConsumer { 32 | 33 | protected F function; 34 | 35 | public ThrowingBiConsumerAdapter(){} 36 | 37 | public ThrowingBiConsumerAdapter(F function) { 38 | this.function = function; 39 | } 40 | 41 | public ThrowingBiConsumerAdapter setFunction(F function) { 42 | this.function = function; 43 | return this; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /java/src/main/java/io/github/toolfactory/jvm/util/ThrowingFunctionAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.jvm.util; 28 | 29 | import io.github.toolfactory.jvm.function.template.ThrowingFunction; 30 | 31 | public abstract class ThrowingFunctionAdapter implements ThrowingFunction { 32 | 33 | protected F function; 34 | 35 | public ThrowingFunctionAdapter() {} 36 | 37 | public ThrowingFunctionAdapter(F function) { 38 | this.function = function; 39 | } 40 | 41 | public ThrowingFunctionAdapter setFunction(F function) { 42 | this.function = function; 43 | return this; 44 | } 45 | 46 | public F getFunction() { 47 | return this.function; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /java/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | module io.github.toolfactory.jvm { 28 | 29 | requires java.base; 30 | requires static jdk.unsupported; 31 | requires static io.github.toolfactory.narcissus; 32 | 33 | exports io.github.toolfactory.jvm; 34 | exports io.github.toolfactory.jvm.function.catalog; 35 | exports io.github.toolfactory.jvm.function.template; 36 | exports io.github.toolfactory.jvm.util; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/src/main/resources/jvm-driver.properties: -------------------------------------------------------------------------------- 1 | # 2 | # This file is derived from ToolFactory JVM driver. 3 | # 4 | # Hosted at: https://github.com/toolfactory/jvm-driver 5 | # 6 | # -- 7 | # 8 | # The MIT License (MIT) 9 | # 10 | # Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | # 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | # documentation files (the "Software"), to deal in the Software without restriction, including without 14 | # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | # the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | # conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be included in all copies or substantial 19 | # portions of the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | # LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | # EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | # OR OTHER DEALINGS IN THE SOFTWARE. 26 | # 27 | priority-of-this-configuration=0 28 | driver-factory.default-driver.class=io.github.toolfactory.jvm.DefaultDriver 29 | driver-factory.hybrid-driver.class=io.github.toolfactory.jvm.HybridDriver 30 | driver-factory.dynamic-driver.class=io.github.toolfactory.jvm.DynamicDriver 31 | driver-factory.native-driver.class=io.github.toolfactory.jvm.NativeDriver -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/jvm/test/AllTestsSuite.java: -------------------------------------------------------------------------------- 1 | package io.github.toolfactory.jvm.test; 2 | 3 | 4 | import org.junit.runner.RunWith; 5 | import org.junit.runners.Suite; 6 | 7 | 8 | @RunWith(Suite.class) 9 | @Suite.SuiteClasses({ 10 | DefaultDriverTest.class, 11 | HybridDriverTest.class, 12 | NativeDriverTest.class, 13 | DynamicDriverTest.class 14 | }) 15 | public class AllTestsSuite { 16 | 17 | //For JDK 7 testing 18 | public static void main(String[] args) { 19 | new DefaultDriverTest().executeTests(); 20 | new DynamicDriverTest().executeTests(); 21 | new HybridDriverTest().executeTests(); 22 | new NativeDriverTest().executeTests(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/jvm/test/DefaultDriverTest.java: -------------------------------------------------------------------------------- 1 | package io.github.toolfactory.jvm.test; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import io.github.toolfactory.util.Reflection; 7 | 8 | 9 | public class DefaultDriverTest extends BaseTest { 10 | 11 | //For JDK 7 testing 12 | public static void main(String[] args) { 13 | new DefaultDriverTest().executeTests(); 14 | } 15 | 16 | @Override 17 | Reflection getReflection() { 18 | if (reflection == null) { 19 | try { 20 | reflection = Reflection.Factory.getNewWithDefaultDriver(); 21 | } catch (Throwable exc) { 22 | exc.printStackTrace(); 23 | throw new RuntimeException(exc); 24 | } 25 | } 26 | return reflection; 27 | } 28 | 29 | 30 | @Override 31 | @Test 32 | public void getConsulterTestOne() { 33 | super.getConsulterTestOne(); 34 | } 35 | 36 | 37 | @Override 38 | @Test 39 | public void getAndSetDirectVolatileTestOne() { 40 | super.getAndSetDirectVolatileTestOne(); 41 | } 42 | 43 | 44 | @Override 45 | @Test 46 | public void getDeclaredFieldsTestOne() { 47 | super.getDeclaredFieldsTestOne(); 48 | } 49 | 50 | 51 | @Override 52 | @Test 53 | public void getDeclaredMethodsTestOne() { 54 | super.getDeclaredMethodsTestOne(); 55 | } 56 | 57 | 58 | @Override 59 | @Test 60 | public void getDeclaredConstructorsTestOne() { 61 | super.getDeclaredConstructorsTestOne(); 62 | } 63 | 64 | 65 | @Override 66 | @Test 67 | public void allocateInstanceTestOne() { 68 | super.allocateInstanceTestOne(); 69 | } 70 | 71 | 72 | @Override 73 | @Test 74 | public void setAccessibleTestOne() { 75 | super.setAccessibleTestOne(); 76 | } 77 | 78 | 79 | @Override 80 | @Test 81 | public void invokeTestOne() { 82 | super.invokeTestOne(); 83 | } 84 | 85 | 86 | @Override 87 | @Test 88 | public void newInstanceTestOne() { 89 | super.newInstanceTestOne(); 90 | } 91 | 92 | 93 | @Override 94 | @Test 95 | public void retrieveLoadedClassesTestOne() { 96 | super.retrieveLoadedClassesTestOne(); 97 | } 98 | 99 | 100 | @Override 101 | @Test 102 | public void retrieveLoadedPackagesTestOne() { 103 | super.retrieveLoadedPackagesTestOne(); 104 | } 105 | 106 | 107 | @Override 108 | @Test 109 | public void retrieveResourcesAsStreamsTestOne() { 110 | super.retrieveResourcesAsStreamsTestOne(); 111 | } 112 | 113 | @Override 114 | @Test 115 | public void getClassByNameTestOne() { 116 | super.getClassByNameTestOne(); 117 | } 118 | 119 | @Override 120 | @Test 121 | public void convertToBuiltinClassLoader() { 122 | super.convertToBuiltinClassLoader(); 123 | } 124 | 125 | @Override 126 | @Test 127 | public void stopThread() { 128 | super.stopThread(); 129 | } 130 | 131 | } -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/jvm/test/DynamicDriverTest.java: -------------------------------------------------------------------------------- 1 | package io.github.toolfactory.jvm.test; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import io.github.toolfactory.util.Reflection; 7 | 8 | 9 | public class DynamicDriverTest extends BaseTest { 10 | 11 | //For JDK 7 testing 12 | public static void main(String[] args) { 13 | new DynamicDriverTest().executeTests(); 14 | } 15 | 16 | @Override 17 | Reflection getReflection() { 18 | if (reflection == null) { 19 | try { 20 | reflection = Reflection.Factory.getNewWithDynamicDriver(); 21 | } catch (Throwable exc) { 22 | exc.printStackTrace(); 23 | throw new RuntimeException(exc); 24 | } 25 | } 26 | return reflection; 27 | } 28 | 29 | 30 | @Override 31 | @Test 32 | public void getConsulterTestOne() { 33 | super.getConsulterTestOne(); 34 | } 35 | 36 | 37 | @Override 38 | @Test 39 | public void getAndSetDirectVolatileTestOne() { 40 | super.getAndSetDirectVolatileTestOne(); 41 | } 42 | 43 | 44 | @Override 45 | @Test 46 | public void getDeclaredFieldsTestOne() { 47 | super.getDeclaredFieldsTestOne(); 48 | } 49 | 50 | 51 | @Override 52 | @Test 53 | public void getDeclaredMethodsTestOne() { 54 | super.getDeclaredMethodsTestOne(); 55 | } 56 | 57 | 58 | @Override 59 | @Test 60 | public void getDeclaredConstructorsTestOne() { 61 | super.getDeclaredConstructorsTestOne(); 62 | } 63 | 64 | 65 | @Override 66 | @Test 67 | public void allocateInstanceTestOne() { 68 | super.allocateInstanceTestOne(); 69 | } 70 | 71 | 72 | @Override 73 | @Test 74 | public void setAccessibleTestOne() { 75 | super.setAccessibleTestOne(); 76 | } 77 | 78 | 79 | @Override 80 | @Test 81 | public void invokeTestOne() { 82 | super.invokeTestOne(); 83 | } 84 | 85 | 86 | @Override 87 | @Test 88 | public void newInstanceTestOne() { 89 | super.newInstanceTestOne(); 90 | } 91 | 92 | 93 | @Override 94 | @Test 95 | public void retrieveLoadedClassesTestOne() { 96 | super.retrieveLoadedClassesTestOne(); 97 | } 98 | 99 | 100 | @Override 101 | @Test 102 | public void retrieveLoadedPackagesTestOne() { 103 | super.retrieveLoadedPackagesTestOne(); 104 | } 105 | 106 | 107 | @Override 108 | @Test 109 | public void retrieveResourcesAsStreamsTestOne() { 110 | super.retrieveResourcesAsStreamsTestOne(); 111 | } 112 | 113 | @Override 114 | @Test 115 | public void getClassByNameTestOne() { 116 | super.getClassByNameTestOne(); 117 | } 118 | 119 | @Override 120 | @Test 121 | public void convertToBuiltinClassLoader() { 122 | super.convertToBuiltinClassLoader(); 123 | } 124 | 125 | @Override 126 | @Test 127 | public void stopThread() { 128 | super.stopThread(); 129 | } 130 | } -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/jvm/test/HybridDriverTest.java: -------------------------------------------------------------------------------- 1 | package io.github.toolfactory.jvm.test; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import io.github.toolfactory.util.Reflection; 7 | 8 | 9 | public class HybridDriverTest extends BaseTest { 10 | 11 | //For JDK 7 testing 12 | public static void main(String[] args) { 13 | new HybridDriverTest().executeTests(); 14 | } 15 | 16 | @Override 17 | Reflection getReflection() { 18 | if (reflection == null) { 19 | try { 20 | reflection = Reflection.Factory.getNewWithHybridDriver(); 21 | } catch (Throwable exc) { 22 | exc.printStackTrace(); 23 | throw new RuntimeException(exc); 24 | } 25 | } 26 | return reflection; 27 | } 28 | 29 | 30 | @Override 31 | @Test 32 | public void getConsulterTestOne() { 33 | super.getConsulterTestOne(); 34 | } 35 | 36 | 37 | @Override 38 | @Test 39 | public void getAndSetDirectVolatileTestOne() { 40 | super.getAndSetDirectVolatileTestOne(); 41 | } 42 | 43 | 44 | @Override 45 | @Test 46 | public void getDeclaredFieldsTestOne() { 47 | super.getDeclaredFieldsTestOne(); 48 | } 49 | 50 | 51 | @Override 52 | @Test 53 | public void getDeclaredMethodsTestOne() { 54 | super.getDeclaredMethodsTestOne(); 55 | } 56 | 57 | 58 | @Override 59 | @Test 60 | public void getDeclaredConstructorsTestOne() { 61 | super.getDeclaredConstructorsTestOne(); 62 | } 63 | 64 | 65 | @Override 66 | @Test 67 | public void allocateInstanceTestOne() { 68 | super.allocateInstanceTestOne(); 69 | } 70 | 71 | 72 | @Override 73 | @Test 74 | public void setAccessibleTestOne() { 75 | super.setAccessibleTestOne(); 76 | } 77 | 78 | 79 | @Override 80 | @Test 81 | public void invokeTestOne() { 82 | super.invokeTestOne(); 83 | } 84 | 85 | 86 | @Override 87 | @Test 88 | public void newInstanceTestOne() { 89 | super.newInstanceTestOne(); 90 | } 91 | 92 | 93 | @Override 94 | @Test 95 | public void retrieveLoadedClassesTestOne() { 96 | super.retrieveLoadedClassesTestOne(); 97 | } 98 | 99 | 100 | @Override 101 | @Test 102 | public void retrieveLoadedPackagesTestOne() { 103 | super.retrieveLoadedPackagesTestOne(); 104 | } 105 | 106 | @Override 107 | @Test 108 | public void retrieveResourcesAsStreamsTestOne() { 109 | super.retrieveResourcesAsStreamsTestOne(); 110 | } 111 | 112 | @Override 113 | @Test 114 | public void getClassByNameTestOne() { 115 | super.getClassByNameTestOne(); 116 | } 117 | 118 | @Override 119 | @Test 120 | public void convertToBuiltinClassLoader() { 121 | super.convertToBuiltinClassLoader(); 122 | } 123 | 124 | @Override 125 | @Test 126 | public void stopThread() { 127 | super.stopThread(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/jvm/test/NativeDriverTest.java: -------------------------------------------------------------------------------- 1 | package io.github.toolfactory.jvm.test; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import io.github.toolfactory.util.Reflection; 7 | 8 | 9 | public class NativeDriverTest extends BaseTest { 10 | 11 | //For JDK 7 testing 12 | public static void main(String[] args) { 13 | new NativeDriverTest().executeTests(); 14 | } 15 | 16 | @Override 17 | Reflection getReflection() { 18 | if (reflection == null) { 19 | try { 20 | reflection = Reflection.Factory.getNewWithNativeDriver(); 21 | } catch (Throwable exc) { 22 | exc.printStackTrace(); 23 | throw new RuntimeException(exc); 24 | } 25 | } 26 | return reflection; 27 | } 28 | 29 | 30 | @Override 31 | @Test 32 | public void getConsulterTestOne() { 33 | super.getConsulterTestOne(); 34 | } 35 | 36 | 37 | @Override 38 | @Test 39 | public void getAndSetDirectVolatileTestOne() { 40 | super.getAndSetDirectVolatileTestOne(); 41 | } 42 | 43 | 44 | @Override 45 | @Test 46 | public void getDeclaredFieldsTestOne() { 47 | super.getDeclaredFieldsTestOne(); 48 | } 49 | 50 | 51 | @Override 52 | @Test 53 | public void getDeclaredMethodsTestOne() { 54 | super.getDeclaredMethodsTestOne(); 55 | } 56 | 57 | 58 | @Override 59 | @Test 60 | public void getDeclaredConstructorsTestOne() { 61 | super.getDeclaredConstructorsTestOne(); 62 | } 63 | 64 | 65 | @Override 66 | @Test 67 | public void allocateInstanceTestOne() { 68 | super.allocateInstanceTestOne(); 69 | } 70 | 71 | 72 | @Override 73 | @Test 74 | public void setAccessibleTestOne() { 75 | super.setAccessibleTestOne(); 76 | } 77 | 78 | 79 | @Override 80 | @Test 81 | public void invokeTestOne() { 82 | super.invokeTestOne(); 83 | } 84 | 85 | 86 | @Override 87 | @Test 88 | public void newInstanceTestOne() { 89 | super.newInstanceTestOne(); 90 | } 91 | 92 | 93 | @Override 94 | @Test 95 | public void retrieveLoadedClassesTestOne() { 96 | super.retrieveLoadedClassesTestOne(); 97 | } 98 | 99 | 100 | @Override 101 | @Test 102 | public void retrieveLoadedPackagesTestOne() { 103 | super.retrieveLoadedPackagesTestOne(); 104 | } 105 | 106 | @Override 107 | @Test 108 | public void retrieveResourcesAsStreamsTestOne() { 109 | super.retrieveResourcesAsStreamsTestOne(); 110 | } 111 | 112 | @Override 113 | @Test 114 | public void getClassByNameTestOne() { 115 | super.getClassByNameTestOne(); 116 | } 117 | 118 | @Override 119 | @Test 120 | public void convertToBuiltinClassLoader() { 121 | super.convertToBuiltinClassLoader(); 122 | } 123 | 124 | @Override 125 | @Test 126 | public void stopThread() { 127 | super.stopThread(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /java/src/test/java/io/github/toolfactory/util/Reflection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of ToolFactory JVM driver. 3 | * 4 | * Hosted at: https://github.com/toolfactory/jvm-driver 5 | * 6 | * -- 7 | * 8 | * The MIT License (MIT) 9 | * 10 | * Copyright (c) 2021 Luke Hutchison, Roberto Gentili 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 13 | * documentation files (the "Software"), to deal in the Software without restriction, including without 14 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 | * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in all copies or substantial 19 | * portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 22 | * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 23 | * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 24 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 25 | * OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package io.github.toolfactory.util; 28 | 29 | import java.lang.reflect.Constructor; 30 | import java.lang.reflect.Field; 31 | import java.lang.reflect.Member; 32 | import java.lang.reflect.Method; 33 | import java.util.Collection; 34 | import java.util.HashSet; 35 | import java.util.LinkedHashSet; 36 | import java.util.Set; 37 | 38 | import io.github.toolfactory.jvm.Driver; 39 | import io.github.toolfactory.jvm.function.template.Function; 40 | 41 | public class Reflection { 42 | private Driver driver; 43 | 44 | 45 | private Reflection(Driver driver) { 46 | this.driver = driver; 47 | } 48 | 49 | public Driver getDriver() { 50 | return this.driver; 51 | } 52 | 53 | public Collection getDeclaredMethods(Class cls) { 54 | Set declaredMembers = new LinkedHashSet<>(); 55 | for (Method method : driver.getDeclaredMethods(cls)) { 56 | declaredMembers.add(method); 57 | } 58 | return declaredMembers; 59 | } 60 | 61 | 62 | public Collection getAllMethods(Class cls) { 63 | return getAll( 64 | cls, 65 | new Function, Method[]>() { 66 | @Override 67 | public Method[] apply(Class input) { 68 | return driver.getDeclaredMethods(input); 69 | } 70 | }, 71 | new HashSet>(), 72 | new LinkedHashSet() 73 | ); 74 | } 75 | 76 | public T getFieldValue(Object target, Field field) { 77 | return driver.getFieldValue(target, field); 78 | } 79 | 80 | public void setFieldValue(Object target, Field field, Object value) { 81 | driver.setFieldValue(target, field, value); 82 | } 83 | 84 | 85 | public Field getDeclaredField(Class cls, String name) { 86 | for (Field member : driver.getDeclaredFields(cls)) { 87 | if (member.getName().equals(name)) { 88 | return member; 89 | } 90 | } 91 | return null; 92 | } 93 | 94 | public Collection getDeclaredFields(Class cls) { 95 | Set declaredMembers = new LinkedHashSet<>(); 96 | for (Field member : driver.getDeclaredFields(cls)) { 97 | declaredMembers.add(member); 98 | } 99 | return declaredMembers; 100 | } 101 | 102 | 103 | public Collection getAllFields(Class cls) { 104 | return getAll( 105 | cls, 106 | new Function, Field[]>() { 107 | @Override 108 | public Field[] apply(Class input) { 109 | return driver.getDeclaredFields(input); 110 | } 111 | }, 112 | new HashSet>(), 113 | new LinkedHashSet() 114 | ); 115 | } 116 | 117 | 118 | public Collection> getDeclaredConstructors(Class cls) { 119 | Set> declaredMembers = new LinkedHashSet<>(); 120 | for (Constructor member : driver.getDeclaredConstructors(cls)) { 121 | declaredMembers.add(member); 122 | } 123 | return declaredMembers; 124 | } 125 | 126 | 127 | public Collection> getAllConstructors(Class cls) { 128 | return getAll( 129 | cls, 130 | new Function, Constructor[]>() { 131 | @Override 132 | public Constructor[] apply(Class input) { 133 | return driver.getDeclaredConstructors(input); 134 | } 135 | }, 136 | new HashSet>(), 137 | new LinkedHashSet>() 138 | ); 139 | } 140 | 141 | private Collection getAll( 142 | Class cls, 143 | Function, M[]> memberSupplier, 144 | Collection> visitedInterfaces, 145 | Collection collection 146 | ) { 147 | for (M member : memberSupplier.apply(cls)) { 148 | collection.add(member); 149 | } 150 | for (Class interf : cls.getInterfaces()) { 151 | if (visitedInterfaces.add(interf)) { 152 | getAll(interf, memberSupplier, visitedInterfaces, collection); 153 | } 154 | } 155 | Class superClass = cls.getSuperclass(); 156 | return superClass != null ? 157 | getAll( 158 | superClass, 159 | memberSupplier, 160 | visitedInterfaces, 161 | collection 162 | ) : 163 | collection; 164 | } 165 | 166 | public static class Factory { 167 | 168 | public static Reflection getNew() { 169 | return getNewWith(Driver.Factory.getNew()); 170 | } 171 | 172 | public static Reflection getNewWith(Driver driver) { 173 | return new Reflection(driver); 174 | } 175 | 176 | public static Reflection getNewWithDynamicDriver() { 177 | return getNewWith(Driver.Factory.getNewDynamic()); 178 | } 179 | 180 | public static Reflection getNewWithDefaultDriver() { 181 | return getNewWith(Driver.Factory.getNewDefault()); 182 | } 183 | 184 | public static Reflection getNewWithHybridDriver() { 185 | return getNewWith(Driver.Factory.getNewHybrid()); 186 | } 187 | 188 | public static Reflection getNewWithNativeDriver() { 189 | return getNewWith(Driver.Factory.getNewNative()); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /java/src/test/resources/jvm-driver.properties: -------------------------------------------------------------------------------- 1 | priority-of-this-configuration=1 2 | -------------------------------------------------------------------------------- /launcher/eclipse/ToolFactory JVM Driver - Debug attacher.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /launcher/eclipse/ToolFactory JVM Driver - build and test with REMOTE DEBUG.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /launcher/eclipse/ToolFactory JVM Driver - clean package install.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /launcher/eclipse/ToolFactory JVM Driver - parent - clean package install.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /launcher/eclipse/ToolFactory JVM Driver - test.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 27 | 30 | 4.0.0 31 | 32 | io.github.toolfactory 33 | jvm-driver-parent 34 | ${revision} 35 | 36 | ToolFactory JVM Driver 37 | 38 | A driver to allow deep interaction with the JVM without any restrictions 39 | 40 | https://toolfactory.github.io/jvm-driver/ 41 | 42 | pom 43 | 44 | 45 | java-hook 46 | java 47 | 48 | 49 | 50 | 51 | MIT License 52 | https://github.com/toolfactory/jvm-driver/blob/master/LICENSE 53 | repo 54 | 55 | 56 | 57 | 58 | ToolFactory 59 | https://github.com/toolfactory 60 | 61 | 62 | 63 | 64 | Luke Hutchison 65 | tool.factory.heads@gmail.com 66 | ToolFactory 67 | https://github.com/toolfactory 68 | 69 | Administrator 70 | Developer 71 | 72 | 73 | 74 | Roberto Gentili 75 | roberto.gentili 76 | tool.factory.heads@gmail.com 77 | ToolFactory 78 | https://github.com/toolfactory 79 | 80 | Administrator 81 | Developer 82 | 83 | 84 | 85 | 86 | 87 | Luke Hutchison, Roberto Gentili 88 | 1.0.0 89 | 9 90 | https://toolfactory@github.com/toolfactory/jvm-driver.git 91 | 92 | 93 | 94 | github.com 95 | https://github.com/toolfactory/jvm-driver/issues 96 | 97 | 98 | 99 | 100 | ossrh 101 | https://oss.sonatype.org/content/repositories/snapshots 102 | 103 | 104 | ossrh 105 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 106 | 107 | 108 | 109 | 110 | scm:git:${repository.url} 111 | scm:git:${repository.url} 112 | https://github.com/toolfactory/jvm-driver 113 | HEAD 114 | 115 | 116 | --------------------------------------------------------------------------------