0

Given Java source code and a preprocessor (like C++), I would like to replace all mentions of null with a function that returns null. It finds a call to null and replaces it with the following function.

public static Object returnNull(){
    return null;
}

This fails because there are varied classes and:

functionThatWantsCustomClass( returnNull() ); //Object cannot be converted to CustomClass

or

if( cc == returnNull() ) //Object cannot be converted to CustomClass

etc.

Easiest solution I can imagine is having to parametrize the preprocessor, although that would require going through every single null to add the parameter maually, eg: null/*CustomClass*/.

Another method is spending a lot of time writing a much better parser so it always knows the required class for a returnTypedNull() function.

Is there a way to get through this error with minimal modification/parsing?

2
  • 3
    Have you thought about using java.util.Optional instead of nulls? Commented Sep 1, 2018 at 5:11
  • 1
    This seems ill-advised. The expression null is not the same as a method invocation returning null, since the former is of the special null type (and thus can be added to a List<?>, for example), whereas the latter is of a reference type. Why would you want to do this? Commented Sep 1, 2018 at 6:36

2 Answers 2

4

Use generics:

public static <T> T returnNull() {
    return (T) null;
}

Follow-up from comment

The following code is as close to comment as I can decipher, and it compiles fine:

public class Test {
    public static void main(String[] args) {
        CustomClass cc = new CustomClass();
        if (cc != returnNull())
            cc.errlog( returnNull() );
    }
    public static <T> T returnNull() {
        return (T) null;
    }
}
class CustomClass {
    void errlog(Exception e) {
    }
}

Now, if there are 2 errlog methods with only one non-primitive parameter:

class CustomClass {
    void errlog(Exception e) {
    }
    void errlog(String s) {
    }
}

Then it will fail with error The method errlog(Exception) is ambiguous for the type CustomClass, because the compiler doesn't know whether T should be Exception or String, i.e. which of the two to call.

You have to explicitly tell the compiler:

cc.errlog( Test.<Exception>returnNull() );
Sign up to request clarification or add additional context in comments.

3 Comments

This fixed a lot of errors but I am still getting errors when the null is being sent as parameter to a function. error: method errlog in class CustomClass cannot be applied to given types; if( cc != returnOtherNull() ) cc.errlog( returnNull() ); required: Exception. found: Object. reason: actual argument Object cannot be converted to Exception by method invocation conversion. (both return nulls are defined separately using generics, if one passes, method one fails). Im getting new unrelated errors so Ill fix those first, maybe they are messing with the compilation.
cc is an instance of CustomClass, errlog is public static synchronized void errlog( Exception e ), it prints the exception if not null, prints "error" if null. Like I said I am getting unrelated errors, so I need to get rid of them before I can be sure it is working or not, but all the errors related to returnNull I can see are in the form of that one error. Only one errlog definition in entire project.
Alright, after fixing all the errors I still had this one. It was solved by upgrading from JDK6 to JDK7 to finally JDK8 (well I dont know if this RUNS, but it compiles), so theres some generics change from 7 to 8 that makes this possible when doing method invocation conversion. Please add this to your answer for completeness sake, or the official change if you know/find it.
1

Use generics ant it will work. Example:

public class ReturnNullExample {

    public static void main(String[] args) {

        ReturnNullExample example = new ReturnNullExample();
        example.someMethod(ReturnNullClass.returnNull());

        CustomClass cc = null;
        if(cc == ReturnNullClass.returnNull()) {
            System.out.println("cc is null");
        }

        cc = new CustomClass();
        if(cc != ReturnNullClass.returnNull()) {
            System.out.println("cc is not null");
        }
    }

    public void someMethod(CustomClass customClass) {
        System.out.println("This method does nothing");
    }
}

class CustomClass {

    private int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

class ReturnNullClass {

    public static <T> T returnNull() {
        return null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.