22cos February 2016

How do I restrict java function usage in intellij?

I have a project with more than 100 java classes. My case is: - i have Button.java, DropDown.java, Checkbox.Java, ListBox.java.... - also, some business class that represent a page for my web app with custom functions and mapping for Button.java and DropDown.java - I want now to be able to use only functions from button and dropdown

An example:

public class FieldsProperties extends RHRegion{
    public static void setProperties(String strMap){
        HashMap<String, String> mapProperties = getMapProperties();
        if(strMap.equals("Properties")){
        mapProperties = UISummary.getProperties("UISummary");
        }
        setMapProperties(mapProperties);
        RHRegion.setProperties(strMap);
    }
}

so in my scenario i want to be allowed only functions from UISummary.java or just to be notified if it is used another class

Answers


Andreas Fester February 2016

You can use the IllegalType rule from checkStyle:

Checks that particular classes are never used as types in variable declarations, return values or parameters.

Rationale: Helps reduce coupling on concrete classes.

Simply add the classes to the list of illegalClassNames. You can configure checkStyle to treat the rule violation either as warning or as error.

Note that this does not prevent from instantiation - for example, assigning to a parent type is still possible. A better (or additional) rule might be IllegalInstantiation:

Checks for illegal instantiations where a factory method is preferred.

Assumed that you do not have a factory method for your restricted classes, this would violate all instantiations of classes defined in the classes property. of that rule There is also an IntelliJ plugin for checkstyle available.


Thorsten Schiffer February 2016

Without having deeper knowledge of your application, but wouldn't it be much better to rethink your packge / application / class structure to prevent the usage of unwanted functionality?

Post Status

Asked in February 2016
Viewed 3,349 times
Voted 7
Answered 2 times

Search




Leave an answer