Eclipse gives me a weird error when compiling
I have this function which returns a datatype InetAddress[]
public InetAddress []
lookupAllHostAddr(String host) throws UnknownHostException {
Name name = null;
try {
name = new Name(host);
}
catch (TextParseException e) {
throw new UnknownHostException(开发者_开发知识库host);
}
Record [] records = null;
if (preferV6)
records = new Lookup(name, Type.AAAA).run();
if (records == null)
records = new Lookup(name, Type.A).run();
if (records == null && !preferV6)
records = new Lookup(name, Type.AAAA).run();
if (records == null)
throw new UnknownHostException(host);
InetAddress[] array = new InetAddress[records.length];
for (int i = 0; i < records.length; i++) {
Record record = records[i];
if (records[i] instanceof ARecord) {
ARecord a = (ARecord) records[i];
array[i] = a.getAddress();
} else {
AAAARecord aaaa = (AAAARecord) records[i];
array[i] = aaaa.getAddress();
}
}
return array;
}
Eclipse complains that the return type should be byte[][] but when I change the return type to byte[][], it complains that the function is returning the wrong data type. I'm stuck in a loop. Does anyone know what is happening here?
A little research based on lookupAllHostAddr
reveals the following:
The sun.net.spi.nameservice.NameService interface was changed
for JDK 6 in a way that makes it impossible for Java source code to
declare a class that can implement either the old or the new version
(the return type of the lookupAllHostAddr
method was changed from
byte[][]
to InetAddress[]
).
Using JDK 6 must fix this!
This code is pretty confusing. It's impossible to know what's happening without knowing about the other classes that are yours.
There's a lot not to like about this code. If I see a loop over a set of objects that depends on "instanceof" for proper operation, I can't help but think that polymorphism would be a better solution.
Your call to new Lookup(name, type).run()
makes me think of Runnable and Threads, but that's not what you're doing.
Looks like you're trying to do DNS server lookups. I'd wonder if there wasn't something available already to help (e.g., Apache James).
UPDATE:
I tried to take your method and make it work. I stripped out the things that I didn't have and reduced the problem to the simplest thing that I could. It has your method signature and the same return type. This simpler example works. If there's a problem, it's got to be in the stuff I stripped out. Sorry I can't be more helpful.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostLookup
{
public static void main(String[] args)
{
try
{
HostLookup lookup = new HostLookup();
String host = ((args.length > 0) ? args[0] : "localhost");
InetAddress [] allHostAddresses = lookup.lookupAllHostAddr(host);
for (InetAddress address : allHostAddresses)
{
System.out.println("address: " + address);
}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
}
public InetAddress [] lookupAllHostAddr(String host) throws UnknownHostException
{
InetAddress[] array = InetAddress.getAllByName(host);
return array;
}
}
精彩评论