Is it possible to access a variable in Java by referencing its name?

1

I have the following concern for which I am looking for the best solution, if possible in Java. I clarify, that the routine of for includes an error that is just the origin of the concern.

I declare constants, for example:

Map<String, Map<Integer, String>> mAlgo = new HashMap();
Map<Integer, String> mPartes = new HashMap();

String[] aPartes = {"top", "left", ...};
String[] aEstados = {"Solido", "Flexible", ...};

String[][] topSolido = {
  {"parte11", "parte12", "parte13"},
  {"parte21", "parte22", "parte23"},
  ...
};
String[][] topFlexible = {
  {"parte11", "parte12", "parte13"},
  {"parte21", "parte22", "parte23"},
  ...
};

String[][] leftSolido = {
  {"parte11", "parte12", "parte13"},
  {"parte21", "parte22", "parte23"},
  ...
};
String[][] leftFlexible = {
  {"parte11", "parte12", "parte13"},
  {"parte21", "parte22", "parte23"},
  ...
};

for (String sParte : aPartes) {
  for (String sEstado : aEstados) {
    String sEnsamble = sParte + sEstado;
    for (int f=0; f<sEnsamble.length; f++) {
      for (int c=0; c<sEnsamble[f].length; c++) {
        mPartes.put(c, sEnsamble[f][c]);
      }
      mAlgo.put(sEnsamble, mPartes);
      mPartes.clear();
    }
  }
}

The crucial part of the exercise is that it throws an obvious error, it is when you try to traverse the variable, as if it were outside the matrix to which its value refers. After the next assignment:

String sEnsamble = sParte + sEstado;

That is ...

for (int f=0; f<sEnsamble.length; f++) {
    ...
}

This in practice causes sEnsamble to take the values "topSolido" , "topFlexible" , "leftSolido" , "leftFlexible" successively, which match the names of each one of the matrices previously declared.

Is there a mechanism in Java that allows you to do something similar to what is proposed? o Is it possible to load map information "mAlgo" with the proposed structure Map<String, Map<Integer, String>> as a constant eg: "M_ALGO" from the beginning without having to perform this conversion in any method?

    
asked by Charlie Brown 11.11.2016 в 15:00
source

1 answer

4

Yes, but only for members of a class. And the truth, unless you know very well what you are doing and WHY DO IT THEN , the chances that you are not interested in using this solution are considerable.

The java.lang.reflect package includes classes to analyze and treat the Java classes themselves. You can instantiate a class by passing the name of the class as a String, access a method or property of the class in the same way, etc.

For example, to create an instance of a BigDecimal class:

Class claseBD = Class.forName("java.math.BigDecimal");
Constructor constructor = claseBd.getConstructor(String.class); // Referencia al constructor que acepta un String.
BigDecimal bd = (BigDecimal) constructor.newInstance("342348948247897980");

Similarly, you can get the methods of an instance and execute them.

Method metodoSetScale = claseBd.getMethod("setScale", Integer.type);
metodoSetScale.invoke(bd, 4); // Igual que hacer bd.setScale(4);

and get the attributes with getField .

Why do I advise against using it? Well it is very powerful, and it is very tempting to start making inventions of this type, but unless they structure very well, you end up complicating the code a lot and having errors in runtime that you would have discovered with compilation.

If you have the experience to mount a CDI or JSF framework with closed and well-defined functionality, they are very useful, but do not treat them with respect. If not, I'm sure I'm not the only one who has inherited a project to find a lot of errors because someone devised an "easy" way to generate SQL from the reflection data and other geniuses.

For your case, if you want to follow that path of combining names, it is much easier to make a Map<String, String[][]> and use what in your example were "variable names" as keys.

    
answered by 11.11.2016 в 15:25