Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:29:14+00:00 2026-06-18T04:29:14+00:00

How to enforce reflection security by not allow the Method , Field , Constructor

  • 0

How to enforce reflection security by not allow the Method, Field, Constructor object to call setAccessible(true) ? SecurityPolicy File or something else?

Normally for stand-alone Java applications there is no SecurityManager registered.

I using this System.setSecurityManager(new SecurityManager());

This approach will work for calling methods.

I would like to enforce the whole jar or client code that uses the jar is not allow to call setAccessible(true);

Any better approach ?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:29:16+00:00Added an answer on June 18, 2026 at 4:29 am

    Um, it does work for setAccessible. See:

    class A {
      private String method1() {
        return "Hello World!";
      }
    }
    

    and

    import java.lang.reflect.Method;
    
    class B {
      public static void main(String[] args) throws Exception {
        System.setSecurityManager(new SecurityManager());
        Class clazz = A.class;
        Method m = clazz.getDeclaredMethod("method1");
        m.setAccessible(true);
      }
    }
    

    Results in

    Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
            at java.security.AccessControlContext.checkPermission(Unknown Source)
            at java.security.AccessController.checkPermission(Unknown Source)
            at java.lang.SecurityManager.checkPermission(Unknown Source)
            at java.lang.reflect.AccessibleObject.setAccessible(Unknown Source)
            at B.main(B.java:8)
    

    One reason it might’ve not worked for you is that according to comments in this post it didn’t use to work in Java 1.5, but works in 6 and thereafter.


    Edit: to deny it for specific jars, you need to either use a policy file, example:

    // specific file
    grant codeBase "file:/test/path/tools.jar" {
      // no permissions for this one
    };
    
    // default to giving all
    grant {
      permission java.security.AllPermission;
    };
    

    There’s two ways of specifying the policy file, either give it as additions to default, or give only those that are specified (source):

    If you use

    java -Djava.security.manager -Djava.security.policy==someURL SomeApp
    

    (note the double equals) then just the specified policy file will be
    used; all the ones indicated in the security properties file will be
    ignored.

    …or implement a custom security manager, which doesn’t look that hard. Haven’t done that myself though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to enforce that a method call in soap based wcf
How can I enforce the JFileChooser filetype (when saving). I have implemented a file
Is it possible to enforce rules or throw an error when using object initializers
I have to enforce a policy issuing a warning if items not belonging to
How do I enforce that the method getFoo() in the implementing class, returns a
How would I enforce a model constraint for a table users, where not only
How can I enforce a stub object in RhinoMocks to return void for a
Is there a way for SQL to enforce unique column values, that are not
I want to enforce on my code base immutable rule with following test [TestFixture]
I am trying to enforce a CHECK Constraint in a ORACLE Database on multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.