Note that there are some explanatory texts on larger screens.

plurals
  1. POAllowing object construction only from some packages
    text
    copied!<p>I work on a game-like system. Users can submit .class and .java files for customized behaviour. Some objects are delivered to the user via callback, but if the user can construct these object himself (with custom parameters), it would mean an advantage to him. I will disallow reflection for the user and seal my packages. I can get this working if I abandon all package structure (and make the constructors package-private), but I would like not to do so. </p> <hr> <p>Here is an example:</p> <p><strong>sscce.mycode.a.SomeClass.java</strong>:</p> <pre><code>package sscce.mycode.a; import sscce.mycode.b.RestrictedObject; import sscce.usercode.SomeUserClass; public class SomeClass { public static void main(String[] args) { SomeUserClass userClass=new SomeUserClass(); // If I can create it from here, anyone can... RestrictedObject object=new RestrictedObject(); userClass.someMethod(object); } } </code></pre> <p><strong>sscce.mycode.b.Interface.java</strong>:</p> <pre><code>package sscce.mycode.b; public interface Interface { public void someMethod(RestrictedObject restrictedObject); } </code></pre> <p><strong>sscce.mycode.b.RestrictedObject.java</strong>:</p> <pre><code>package sscce.mycode.b; public class RestrictedObject { public RestrictedObject() {} } </code></pre> <p><strong>sscce.usercode.SomeUserClass.java</strong>:</p> <pre><code>package sscce.usercode; import sscce.mycode.b.Interface; import sscce.mycode.b.RestrictedObject; public class SomeUserClass implements Interface { @Override public void someMethod(RestrictedObject restrictedObject) { // It receives an instance, but cannot create it. System.out.println("Got "+restrictedObject); } } </code></pre> <hr> <p><strong>Motivation</strong>: Having everything in one package sounds messy...</p> <p>Does anyone have ideas on how to accomplish this without flattening the packages?<br> Thanks in advance for any solutions, ideas or comments, Till</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload