Passing a link line by line in Java

Is there a way to implement this kind of code in Java?

int foo = 3; String data = "foo"; System.out.println(StringToReference(data)); 

and type 3 ?

Edit: In particular, I would like to parse a String or char and return an int representing a KeyEvent . For example, I would like to be able to do this:

 for(char c : "hello") new Robot().keyPress(StringToReference("KeyEvent.VK_"+c)); 
+4
source share
6 answers

In general, in Java you cannot refer to variables by name (like strings, at runtime, which Perl calls "soft links"). But in some cases (fields, not local variables) you can get similar behavior with reflection.

For your intention, you can do this:

 public static int getKeyEventVK(char c) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { return KeyEvent.class.getField("VK_" + String.valueOf(c).toUpperCase()).getInt(null); } public static void main(String[] args) throws Exception{ System.out.println(getKeyEventVK('h')); System.out.println(KeyEvent.VK_H); } 

But remember that a typed string can trigger more than one VK_ * event (e.g. shift). And both H and H correspond to VK_H , so this will not accurately simulate the events triggered by the entry of the hello string (not to mention non-alphanumeric characters, dead keys, backspaces, etc.)

+2
source

Not knowing exactly what you are trying to achieve, this is the closest thing that I think you can get:

 Map<String, Integer> map = new HashMap<String, Integer>(); map.put("foo", 3); System.out.println(map.get("foo")); 
+6
source

Here's how to do it with reflection:

 char c = "C"; Robot rob = new Robot(); Field field = KeyEvent.class.getDeclaredField("VK_"+c); int i = field.getInt(null); rob.keyPress(i); 
+1
source

I see what you are trying to do. None of the other answers are correct.

You want to get the β€œcorrect” KeyEvent constant for the given character, and you want to do this without creating some kind of lookup table whose length should be two million long.

Indeed, thinking will help you here. It will be pretty slow. But he will do the job. Is the work really needed - another question. :-)

The function you want about is probably something like this:

 /** * If possible, returns an {@code int} equal to one of the {@code public * static final int} constants present in the {@link KeyEvent} class * whose names start with {@code VK_}. * * <p>This implementation does no error handling whatsoever and has not * been tested or compiled.</p> * * <p>This method is placed explicitly in the public domain.</p> * * @param c the character to use while searching for a field; no attempt * will be made by this implementation to validate it in any way * * @return a {@link KeyEvent} constant whose name starts with {@code VK_} * * @exception Exception for any of a number of possible reasons */ public int getKeyEventConstant(final char c) throws Exception { final Field field = KeyEvent.class.getField(String.format("VK_%S", Character.valueOf(c))); assert field != null; return field.getInt(null); } 

Then you can feed it as shown below, although you will have all kinds of problems if the supplied String contains characters, the function described above is not edited to handle exceptions correctly:

 public toKeyEventCodes(final String s) { int[] returnValue = null; if (s != null && !s.isEmpty()) { final Collection<Integer> codes = new ArrayList<Integer>(s.length()); final char[] chars = s.toCharArray(); assert chars != null; assert chars.length > 0; for (final char c : chars) { if (!Character.isWhitespace(c)) { // TODO: weed out other obvious dumb chars codes.add(Integer.valueOf(getKeyEventConstant(c))); } } returnValue = codes.toArray(new int[codes.size()]); } if (returnValue == null) { returnValue = new int[0]; } return returnValue; } 

All this code has not been verified. Good luck. I assume that you still have something too complicated, but hopefully this will force you to point in the right direction.

+1
source

You can use the following code:

 int foo = 3; String data = Integer.toString(foo); 

This will store the int in the String file. I suppose you could pass it into your method and return whatever you want from it.

Although the code above will work, I'm not sure when you use it.

0
source

You can use java reflection.

Reflection for the field

  Class<?> c = Class.forName("YOUR_CLASS_NAME"); Field f = c.getField("FOO"); System.out.println(f.getInt()); 
-1
source

Source: https://habr.com/ru/post/1482453/


All Articles