-1

My idea was to use casting to get a StringBuilder object to pass a valid argument to a method. Like this:

public TTTButton(String color) {
    StringBuilder sb = new StringBuilder();
    sb.append("Color."+ color.toString().toUpperCase());
    this.setBackground((Color)(Object)sb);// Runtime Exception: java.lang.StringBuilder cannot be cast to java.awt.Color 

Please share your thoughts on why such things are impossible (or maybe they are possible)?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Mindaugas Bernatavičius
  • 3,757
  • 4
  • 31
  • 58
  • 5
    Because a cast is not the same thing as an [`Eval` function](http://stackoverflow.com/questions/7143343/is-there-a-java-equivalent-of-the-python-eval-function). – Robert Harvey Jan 14 '14 at 00:17
  • You can't cast it, because a `String` is not a `Color`. You can perform a lookup, [as in my answer to your previous question](http://stackoverflow.com/questions/21103630/java-how-to-get-a-color-value-from-the-user-as-a-string-and-use-it-in-a-method/21103675#21103675). – rgettman Jan 14 '14 at 00:19
  • A StringBuilder is not a Color. Casting only works if they actually are the right type. – Krease Jan 14 '14 at 00:19
  • Fyi, `sb.append("a" + b)` does `sb.append(new StringBuilder("a").append(b).toString())` under the hood so your explicit `StringBuilder` is redundant. – Mike Samuel Jan 14 '14 at 00:19
  • @RobertHarvey, and if string arguments were automatically evaled as in the example, then `System.out.println("new java.lang.String(\"foo\")");` would be ambiguous. – Mike Samuel Jan 14 '14 at 00:22
  • Is this question related to this other one? [JAVA: How to get a color value from the user as a String and use it in a method that accepts java.awt.Color enum values](http://stackoverflow.com/questions/21103630/java-how-to-get-a-color-value-from-the-user-as-a-string-and-use-it-in-a-method/21103930#21103930) – dic19 Jan 14 '14 at 00:22
  • Yes, I know that "A StringBuilder is not a Color". The form of an answer I was hoping for was "because in situation X it would be ambiguous" or "it would be unsafe in situation X" @Mike Samuel: thaks for `System.out.println("new java.lang.String(\"foo\")");` @dix19: no, its a general question spawned by the situation that also spawned the previous question. It could have been illustrated by another example. – Mindaugas Bernatavičius Jan 14 '14 at 00:35

1 Answers1

1

String or StringBuilder is not Color type so you can't use casting here. Maybe try to use Color.getColor(colorName) where colorName is String.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Note - where it is a `String`, not a `StringBuilder`... – Krease Jan 14 '14 at 00:22
  • 1
    As people explained before, you just cannot cast from String to Color because the interpreter won't know how to do such a conversion. For the sake of curiosity, a way to do what you want is like this import java.awt.Color; import bsh.EvalError; import bsh.Interpreter; public class ColorEval { public static void main(String[] args) throws EvalError { Interpreter bsh = new Interpreter(); bsh.eval("java.awt.Color c = java.awt.Color.BLACK"); Color black = (Color)bsh.eval("c"); System.out.println(black.equals(Color.BLACK)); } } –  Jan 14 '14 at 00:34
  • `public TTTButton(String color) throws EvalError {Color passedColor = (Color)bsh.eval("Color."+ color.toString().toUpperCase()); this.setBackground(passedColor);` Works fantastically! I think it allows to construct any method, argument or anything from string. Thanks! – Mindaugas Bernatavičius Jan 14 '14 at 01:14