简体   繁体   中英

How can I define an interface that forces the IDE to show “not declared” warning?

When you implement Serializable for your class, IDE shows a warning that The serializable class YOURCLASS does not declare a static final serialVersionUID field of type long.

How can I define an interface that forces the IDE to show this warning for my field?

public class MyClass implements Serializable 
{
    // this class has warning
}

public class MyClass implements MyInterface
{
    // I want to have warning for this class
}

public interface MyInterface{
    public static final long myField;
}

It is special case applicable only in case of classes implementing Serializable interface and configurable in eclipse. Windows--->Preference--->Compile--Error/warnings-->Potential programming problems---> This where eclipse has configuration for this. Even though you force IDE to display warning by some other means for your class, when you compile with javac, it won't force.

You can use aspectj to generate compiler warnings or errors. It is sometimes used to enforce architectural aspects, but might be applicable for you, too.

The Pointcut definitions itself are left as an excercise for the reader :-)

@Aspect
public class ArchitecturalEnforcement {
    @Pointcut("...")
    public void implementsInterface() {}

    @Pointcut("...")
    public void definesConstant(){}

    /** Advice that defines an error when a GUI method calls a method in the DAO layer */
    @DeclareWarning("implementsInterface() && !definesConstant()")
    private static final String WARNING= "...";
}

Read more on AOP (Aspect Oriented Programming)

This doesn't have anything to with what IDE you're using, it has to do with that you declared your interface wrong. Your interface must contain method signatures and right now in your example it only contains a declaration for a constant. For your interface to be valid it would need to look something more like this:

public interface MyInterface{
   public long doSomethingAndReturnALong();
   public void doSomethingElseButRequireAnInt(int var);
}

If you declare any kind of variable in an interface, such as you did above, it's assumed to be static and final, also known as a constant that can't be changed by classes that implement the interface.

Hope that helps! David

In general, this is not possible with interfaces in Java.

If you want the field to be publicly accessible, then you should provide getter and setter methods for it anyway. You can declare these methods in the interface, which will of course result in a compiler error if an implementing class does not declare them.

If you only want to force the classes to have a private field which you would at some point access by reflection, you should think if there is another solution that doesn't need the field. Perhaps its also possible to use methods?

The last option would be to use a static analysis tool like FindBugs , which allows you to define custom bug patterns . So you would write a rule which finds implementors of your interface without this field and enable it in your FindBugs configuration. There exists a good Eclipse plugin for FindBugs which displays the FindBugs warnings in your code (just like the regular Eclipse warnings).

The IDE is not the one raising the warning. It is the compiler. If you need a custom warning, you need a custom compiler. But then you should distribute the compiler with your interface.

In order to create a custom compiler you can use:
http://today.java.net/article/2008/04/09/source-code-analysis-using-java-6-apis

Basically you need to define your own annotation and annotation processor. Probably you will create an annotation for your interface that when a class implements it and it does not define a variable you will raise a warning.
I haven't done this yet, so I cannot give you more info about it.

Another possibility is to use an existing compiler plugin that defines custom warnings and annotations: https://checkerframework.org/
I do not know if it will help you in your quest, or you should eventually write your own compiler plugin.

PS: checkout this post also:
How to intentionally cause a custom java compiler warning message?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM