0

I'm new to Scala and have imported an external Java (nmap4J) library that returns an object called:

class org.nmap4j.data.nmaprun.Verbose
  • How do I determine what the object type is in scala?
  • How would I then convert it to something thats usable in Scala?

I'm assuming I would ned to use the Scala Java Conversions, but I'm a little confused as where to start.

http://www.scala-lang.org/api/2.10.2/index.html#scala.collection.JavaConversions

2 Answers 2

3

You can use it just like you'd use it in Java, and it's type doesn't change: it's org.nmap4j.data.nmaprun.Verbose.

Conversions and Converters are intended to transform a Java standard library collection into a Scala standard library collection and vice versa. Generally speaking, if you have a Java collection that is not from the standard library, there's likely some reason for it to be different, and converting it to Scala would defy its purpose.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - I'll try again. When I tried to iterate over the object I was getting type mismatch errors which seemed to imply there there was a mismatch. Is there any easy way to dump the object to string?
Hmm. Maybe I'm missing something here. I still can't determine how I can iterate over this object and get/use its contents.
@user1513388 from what I can see Verbose is not intended to be iterated in neither java or scala. You only can call .getLevel/.setLevel methods and access to public constants, not much specific for scala.
Sorry - maybe I'm being stupid here. I just need to get the contents of Verbose! But seem to be struggling to get to it!
0

type conversion

Lets say you have an unknown Object (scala Any / AnyRef) and you think it is a class of Verbose:

import org.nmap4j.data.nmaprun.Verbose

def checkObj(in: Any) {
  in match {
    case v: Verbose =>
      // do something with v

    case _ =>
      // do error handling
}

This is the scala way of doing isInstanceOf / asInstanceOf. Almost always you can treat a java class like a scala class.

list conversion

If your framework gives you java collections, you import, as you said correctly, java conversions.

import scala.collection.JavaConversions._

def doSomething(list: java.util.List[Any]) {
  // map is now possible due to the implicit conversion of list
  list.map(el => doStuff(el))

  // other methods
  list.toSeq
  list.foreach(println)
}

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.