Java Reflection Utility

Is there a utility to get a property that does not have a prefix received from an object using reflection similar to BeanUtils? for example, if I specify "hashCode" and I want to get the value of object.hashCode ().

Thank.

+3
source share
5 answers

You can call hashCode()on everyone Object. You do not need to think about it.

Otherwise, you can use the standard reflection classes - java.lang.Classand its method getMethod(..), which returns java.lang.reflect.Method.

+3
source

The java mapping API allows you to access any property in a given instance of the class, including private variables.

Reflection - , , .

, Google.

http://tutorials.jenkov.com/java-reflection/index.html

+1

org.apache.commons.beanutils.MethodUtils - , .

+1

. , . :

   import java.lang.reflect.*;

   public class field1 {
      private double d;
      public static final int i = 37;
      String s = "testing";

      public static void main(String args[])
      {
         try {
            Class cls = Class.forName("field1");

            Field fieldlist[] 
              = cls.getDeclaredFields();
            for (int i 
              = 0; i < fieldlist.length; i++) {
               Field fld = fieldlist[i];
               System.out.println("name
                  = " + fld.getName());
               System.out.println("decl class = " +
                           fld.getDeclaringClass());
               System.out.println("type
                  = " + fld.getType());
               int mod = fld.getModifiers();
               System.out.println("modifiers = " +
                          Modifier.toString(mod));
               System.out.println("-----");
            }
          }
          catch (Throwable e) {
             System.err.println(e);
          }
       }
   }

: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

0

, , , , BeanUtils?

There are standard reflection APIs, but what you ask is problematic. The Bean convention is that a method starting with getor is(and some other characteristics) is a property. Without this convention, it is impossible to know which of the class methods are property attributes (or setters) and which are methods that have a completely different purpose. For example, you would not want to call it an File.delete()erroneous belief that it was a getter for some property boolean!

0
source

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


All Articles