Reputation: 2576
I undeerstand it's certainly not a good practice, but does Java provide any mechanism to prevent accessing package-private methods/attributes? If not, does that mean this:
package com.other_people_other_company;
public class MyClass {
// Let's hack! Access all package privates
}
is valid?
It's mentioned in the answer:
Name: myCompany/myPackage/
Sealed: true
This is new info to me. But what if I sabotage by distributing a jar of mine, and sealing other people's package there? E.g. Name: org.apache Sealed: true
Upvotes: 2
Views: 123
Reputation: 29375
Assuming .JAR distribution, you can seal the packages by adding the following lines to META-INF/MANIFEST.MF
:
Name: myCompany/myPackage/
Sealed: true
A few notes:
Upvotes: 2
Reputation: 34920
Yes, that's true. On this idea some kind of "patch" method is based: when you have to fix some bug in 3d-party library you can create same package with same class in your classpath and create fixed class.
The only one restriction is special package names like java.util
, e.g. this example will be successfully compiled, though java.util.Tripwire
class has package local access modifier:
package java.util;
public class TestTripwire {
public static void main(String[] args) {
Tripwire.trip(Object.class, "sss");
}
}
But if you run it you will get exception Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util
Upvotes: 0