Showing posts with label access level modifiers. Show all posts
Showing posts with label access level modifiers. Show all posts

Thursday, 28 March 2019

Did you ever do this?

So I was working on a class which was too big. Time to pull it apart into helper classes (in the same package). Then I noticed a number of methods were protected.

Now protected means it can be both accessible from subclasses as well as classes in the same package. (That last part is often forgotten by me.)

Which means when pulling things apart into classes, I can simply call these protected methods. Yay!

Unfortunately, these protected methods were defined in a superclass and the superclass was declared in... you guessed it... a different package.

The ugly hack that solved this is the following:

Of course, I deleted this, because to my mind overriding a method, just to increase the visibility is a huge code-smell. (Or isn't it?)

But I was wondering if anyone else ever did this? (due to time pressures and not being able to change the super class, etc.)

And what would be a more pretty work around, if you have one?

Friday, 18 May 2018

Access control modifiers in Java

Just a small blog post.

Some time ago I came across a basic fact in the core Java programming language, that I have interpreted too narrowly for a very long time.

I always have interpreted the "protected" keyword as making the properties or methods of a class available in its subclasses.

Turns out, according to [1] this is not entirely accurate. That's not all it does.

The following table is lifted from the docs in [1]:

ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifier Y Y N N
private Y N N N

If you look at the table above, you'll notice that protected is really only one level more secure than public, which was an eye opener for me.

It turns out "protected", isn't really that protected really.

No modifier

If you omit any kind of modifier, you will automatically get the behaviour that the method/property can be accessed only by other classes in the same package.

There are several sources declaring it "no modifier" or "default modifier" or "default protected modifier", but I prefer the "package private modifier" (or just "package-private" for short). The latter is the official Java convention as described in [1].

To my mind it is the best clear short precise definition of what you can do with it.

The JLS2 strictly only mentions "package access".

Conclusion

Apparently there is no Access Modifier in Java that makes methods or fields only be inherited from subclasses3.

However we can achieve the same goal, by putting the subclasses of a class into the same package, and keep this package devoid of all others.

Sealing JARs

Of course, it means that if I create a package with the same name as a package in a JAR file included in my project, I will gain access to all the package private classes in the same package in the JAR.

This problem can apparently be solved by sealing4 the JAR file.

References

[1] Oracle - The Javatm Tutorials - Controlling Access to Members of a Class
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
[2] Oracle - Java Language Spec - JDK 8
https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf
[3] StackOverflow - Why is there no subclasses only access modifier in Java?
https://softwareengineering.stackexchange.com/questions/238581/why-is-there-no-subclasses-only-access-modifier-in-java
[4] Oracle - The Javatm Tutorials - Sealing Packages within a JAR File
https://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html

Thursday, 13 August 2015

@VisibleForTesting

I recently encountered the following annotation in the source code at work:
@VisibleForTesting
It is one I had not seen before. Below is an example of what it looked like.
@VisibleForTesting
void updateTaxation(TaxationData data) 
{
    ...
}
The annotation is part of the com.google.common.annotations package.

It turns out that it is a Marker annotation, simply to communicate to (other) software designers that the access level modifier has been relaxed, to make the code more easy to test.

In the example above, the access level modifier is "default", while it should/could have been "private".

I am still not convinced this is a good thing, because you changed production code to fix a testing problem.

We could work around this issue by using Reflection to access private methods/members, but Reflection is very brittle, especially when dealing with living code. So, that doesn't appeal to me either.

The best way, is to either:
  • test the private method, by using the public api that uses it
  • if this not enough, you can move the private method into a Strategy object1, and test the Strategy object in isolation

References

[1] Wikipedia - Strategy Pattern
https://en.wikipedia.org/wiki/Strategy_pattern
[2] StackOverflow -annotation to make a private method public only for test classes
http://stackoverflow.com/questions/6913325/annotation-to-make-a-private-method-public-only-for-test-classes