0

I am still trying to switch efficiently on classes, and I am wondering if this could work : generate the same code as a compiler-generated "switch on strings" code but using intern instead of equals.

Related to : Why can't we switch on classes in Java 7+?

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • In order to use a class name in a switch, you'd have to use a string literal, so these would be in the string constant pool. So, even if class names aren't automatically interned, they would be as a result of your switch. – Andy Turner May 01 '16 at 17:59
  • Sounds like an XY problem. Why? – chrylis -cautiouslyoptimistic- May 01 '16 at 18:28
  • @AndyTurner not necessarily, look at http://stackoverflow.com/a/31671438/693752 – Snicolas May 01 '16 at 18:39
  • @chrylis sorry I am not a native english speaker, I didn't get it. – Snicolas May 01 '16 at 18:39
  • Explain the task you are trying to accomplish. "Switching on classes" sounds like a hack to do something that there's a better way to do. – chrylis -cautiouslyoptimistic- May 01 '16 at 18:50
  • @chrylis I am an advanced developer and this is an advanced question. I am writing an annotation processor that generates classes, and a registry for all those classes that basically maps them to some code that instantiates the generated classes. The whole goal is to bypass reflection as it is slow on some VMs, namely Android. – Snicolas May 01 '16 at 19:03
  • Note that the code of [my answer](http://stackoverflow.com/a/31671438/2711488) **is** “the same code as a compiler-generated ‘switch on strings’” without calling `equals` on the string. It does a reference comparison for the *classes*, so there is no need to compare the strings at all. – Holger May 02 '16 at 09:00
  • @Holger, yes, thx, that is what I am going to implement. I had missed that part in your answer. But yes, it solves well my problem. Though, I leave the question open as it has a broader scope. – Snicolas May 02 '16 at 15:27

1 Answers1

4

I suspect you could get into trouble with this kind of approach. The simple answer is that the JVM interns all Strings by default. However, your problem is much deeper than that - the name of a class is not unique within a single JVM. If the same class is loaded by two different classloaders then there will be two instances of that class object within the JVM which have the same name but which are not equal to each other. If you want to model the semantics of switching on a class then you need a way of capturing that behaviour.

sisyphus
  • 6,174
  • 1
  • 25
  • 35
  • 3
    Ah, yes, the old `cannot cast javax.servlet.http.HttpServletRequest to javax.servlet.http.HttpServletRequest`... – chrylis -cautiouslyoptimistic- May 01 '16 at 18:29
  • Good point. I guess it implies that : http://stackoverflow.com/a/31671438/693752 is the best approach then. The final test can't rely on strings only. – Snicolas May 01 '16 at 18:41
  • I leave the question open though as I got an answer for my current reseach work but the question has a broader scope. – Snicolas May 01 '16 at 18:42
  • Though I disagree with the sentence : "The simple answer is that the JVM interns all Strings by default.". It's not true. – Snicolas May 01 '16 at 18:44