Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mehdiyari
Created March 1, 2022 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdiyari/c1cfa4a6e6285c512a258e39ac9e0fbf to your computer and use it in GitHub Desktop.
Save mehdiyari/c1cfa4a6e6285c512a258e39ac9e0fbf to your computer and use it in GitHub Desktop.
This gist is part of meta-programming with kotlin articles
/**
* 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