Created
March 1, 2022 08:55
-
-
Save mehdiyari/c1cfa4a6e6285c512a258e39ac9e0fbf to your computer and use it in GitHub Desktop.
This gist is part of meta-programming with kotlin articles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simply security manager that check the packages and if equal | |
* to "java.lang.reflect" or "kotlin.reflect" throws [SecurityException] | |
*/ | |
class AppSecurityManager : SecurityManager() { | |
override fun checkPackageAccess(pkg: String?) { | |
if (pkg == "java.lang.reflect" || pkg == "kotlin.reflect") { | |
throw SecurityException("Reflection is not allowed in this program") | |
} | |
super.checkPackageAccess(pkg) | |
} | |
} | |
fun main() { | |
System.setSecurityManager(AppSecurityManager()) | |
// Using reflection cause [SecurityException] | |
Person::class.createInstance().name.also(::println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment