Bugs in IntelliJ IDEA.

£

£ukasz Krawczyk

Make a new Java project in IntelliJ Idea and add the following files. Try to
compile them.

1.
public class Test {

// Variable name is the same as Class name!!!
static String String;

public static void main(String[] args) {
System.out.println(String.valueOf('c'));
}

}

2.
import java.util.ArrayList;

public class Test2 {
ArrayList list = new <String>ArrayList<String>();
}

In the first example IntelliJ gives a warning: "Static member ... accessed
via instance reference", however Sun compiler doesn`t show anything.

In the second example Intellij doesn`t show anything, hovewer Sun`s compiler
points out a BUG.

I work on IntelliJ 5.1.2. Are these problems present in another IDE-s?
Are these true BUGS or is there something with my brain :D

Jujo
 
D

Daniel Dyer

Make a new Java project in IntelliJ Idea and add the following files.
Try to
compile them.

1.
public class Test {

// Variable name is the same as Class name!!!
static String String;

public static void main(String[] args) {
System.out.println(String.valueOf('c'));
}

}

2.
import java.util.ArrayList;

public class Test2 {
ArrayList list = new <String>ArrayList<String>();
}

In the first example IntelliJ gives a warning: "Static member ...
accessed
via instance reference", however Sun compiler doesn`t show anything.

This is exactly what I would expect to see. The IDE has a lot of warnings
that are not present in the compiler. You can disable the inspections if
you don't like them. The reason you get a warning is because the "String"
in the main method is the variable name, not the String class. Calling a
static method in this way is confusing and probably not what the
programmer intended, hence the warning. It's not wrong in the strictest
sense, so the compiler doesn't complain.
In the second example Intellij doesn`t show anything, hovewer Sun`s
compiler
points out a BUG.

This one does look to be a bug in the IDE. Have you tried one of the 6.0
builds to see if it has been fixed? Perhaps you should file a bug report.

Dan.
 
£

£ukasz Krawczyk

In the first example IntelliJ gives a warning: "Static member ...
This is exactly what I would expect to see. The IDE has a lot of warnings
that are not present in the compiler. You can disable the inspections if
you don't like them. The reason you get a warning is because the "String"
in the main method is the variable name, not the String class. Calling a
static method in this way is confusing and probably not what the
programmer intended, hence the warning. It's not wrong in the strictest
sense, so the compiler doesn't complain.
I agree, I didn`t realize that was the inspection, not compiler :)

But are You sure that this method is REALLY accessed, in this particular
case, via instance reference and NOT class name?
I can`t figure out the test to check this...
Even if String String = null, static methods of 'String' Class still can by
called, and not crash with an NullPointerException.

Jujo
 
D

Daniel Dyer

I agree, I didn`t realize that was the inspection, not compiler :)

But are You sure that this method is REALLY accessed, in this particular
case, via instance reference and NOT class name?
I can`t figure out the test to check this...

If you change the type of the variable called String to be Object:

Object String = null;

The compilation will fail because there is no valueOf method in the Object
class.

String.valeOf('c'); // Fails to compile, "cannot find symbol".
Even if String String = null, static methods of 'String' Class still can
by
called, and not crash with an NullPointerException.

Doesn't matter, the field name is just used (instead of the class name) to
qualify the static method name. So this still works without a
NullPointerException:

public class Test
{
private String myString = null;

public static void main(String[] args)
{
new Test().doStuff();
}

private void doStuff()
{
System.out.println(myString.valueOf(76));
}
}

Dan.
 
D

Daniel Dyer

....


This one does look to be a bug in the IDE. Have you tried one of the
6.0 builds to see if it has been fixed? Perhaps you should file a bug
report.

This is still an issue in IDEA 6.0.2.

Dan.
 
D

Daniel Pitts

Daniel said:
This is still an issue in IDEA 6.0.2.

Dan.
It may be a bug, but it doesn't come up that often, does it?
Usually the generic type can be infered by the parameters, and AFAIK,
Idea handles this well enough, and its not really invalid syntax, so I
can understand why they missed it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top