views:

44

answers:

2

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?

+1  A: 

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;
    }
}
duffymo
I wish I wrote this so that I could have corrected it based on your suggestions. I'm playing around with a large open source project that uses multiple open source projects and am trying to make it compile properly but it doesn't... I was thinking maybe it has something to do with the jdk version (I am using 1.5.0.11 currently), but I do not have sudo on this machine to upgrade it...
Legend
Thanks for your time. Changing my JDK version to 1.6 fixed the problem.
Legend
+1  A: 

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!

ring bearer
Great... thanks for this...!
Legend