How to loop over a Class attributes in Java?

How can I loop over a class attributes in java dynamically. For eg : public class MyClass { private type1 att1; private type2 att2; ... public void function() { for(var in MyClass.Attributes) { System.out.println(var.class); } } } is this possible in Java? There is no linguistic support to do what you're asking for. You can reflectively access the members of a type

如何遍历Java中的Class属性?

我怎样才能动态地在java中循环一个类的​​属性。 例如: public class MyClass { private type1 att1; private type2 att2; ... public void function() { for(var in MyClass.Attributes) { System.out.println(var.class); } } } 这在Java中可能吗? 没有语言支持去做你想要的东西。 你可以在运行时反射地访问类型的成员(例如用Class.getDeclaredFields()来获得一个Field数组),但根据你想要做什

Dumping a java object's properties

Is there a library that will recursively dump/print an objects properties? I'm looking for something similar to the console.dir() function in Firebug. I'm aware of the commons-lang ReflectionToStringBuilder but it does not recurse into an object. Ie, if I run the following: public class ToString { public static void main(String [] args) { System.out.println(ReflectionToS

转储Java对象的属性

是否有递归地转储/打印对象属性的库? 我正在寻找类似于Firebug中的console.dir()函数的东西。 我意识到commons-lang ReflectionToStringBuilder,但它不会递归到一个对象中。 即,如果我运行以下操作: public class ToString { public static void main(String [] args) { System.out.println(ReflectionToStringBuilder.toString(new Outer(), ToStringStyle.MULTI_LINE_STYLE)); } private stat

Execute method from string

This question already has an answer here: What is reflection and why is it useful? 20 answers How do you dynamically compile and load external java classes? [duplicate] 2 answers

从字符串执行方法

这个问题在这里已经有了答案: 什么是反思,为什么它有用? 20个答案 你如何动态编译和加载外部java类? [重复] 2个答案

Create an object dynamically in java

This question already has an answer here: Create new object using reflection? 2 answers What is reflection and why is it useful? 20 answers Are you familiar with hashes? I think you could use a HashMap, which is a common Hash implementation built into the Java library: HashMap<String,Object> person1 = new HashMap<String,Object>(); person1.put("className", "Person"); person1.

在java中动态创建一个对象

这个问题在这里已经有了答案: 使用反射创建新对象? 2个答案 什么是反思,为什么它有用? 20个答案 你是否熟悉哈希? 我想你可以使用一个HashMap,它是Java库中内置的常见哈希实现: HashMap<String,Object> person1 = new HashMap<String,Object>(); person1.put("className", "Person"); person1.put("name", "Jack"); person1.put("age", 21); 每次你想改变这些值时,请执行: person1.put("nam

Reflection: Effective, Awesome, Necessary uses

Possible Duplicates: What is reflection, and why is it useful? So I've read the Reflection tutorial on Java's website, and I think I generally understand that it allows a class to inspect itself, having access to properties, methods, etc. However, how, if at all, does this relate to mutable or immutable code? Can classes change their own code using something like reflection? If not,

反思:有效,真棒,必要的用途

可能重复: 什么是反思,为什么它有用? 所以我已经阅读了Java网站上的Reflection教程,我认为我通常理解它允许一个类可以检查自己,访问属性,方法等等。但是,如果可以的话,它是如何与可变或不可变的代码? 类可以使用反射来改变自己的代码吗? 如果不是,你遇到/创建的反射最令人敬畏的用法是什么? 谢谢! 不,反射并不能直接让类改变它的代码。 然而,你可以用java.lang.reflect.Proxy做一些很棒的事情 - 例如

Reflection api in Java

This question already has an answer here: What is reflection and why is it useful? 20 answers what is reflection in java? Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

Java中的反射api

这个问题在这里已经有了答案: 什么是反思,为什么它有用? 20个答案 what is reflection in java? 反射使得可以在运行时检查类,接口,字段和方法,而无需在编译时知道类名,方法等。 也可以实例化新对象,调用方法并使用反射来获取/设置字段值。 In which situation i need to use reflection? 在运行时将对象映射到数据库中的表。 Real time scenarios and examples for the need of using reflection? 好的实时

What are uses for reflection? (e.g the Java Reflection API)

Possible Duplicate: What is reflection, and why is it useful? So I've been messing with the Reflection API lately, and it seems cool to screw around with here and there but what exactly is the use of it? You see, it's all fun to get field/method names and return types and set accessibility and whatnot, but how could this [reflection] possibly be of any use if you can read the java f

什么用于反思? (例如Java Reflection API)

可能重复: 什么是反思,为什么它有用? 所以最近我一直在使用Reflection API,而且在这里和那里搞得一团糟似乎很酷,但究竟是什么使用它呢? 你会发现,获取字段/方法名称和返回类型以及设置可访问性和什么是非常有趣的,但是如果你可以用自己的眼睛阅读java文件/类,那么这个[反射]怎么可能有任何用处呢? 也许我真的很无知,或者我不明白这个Reflection的完整概念,但是你能向我解释这可能会有帮助吗? 有时你想拥

Programming with Core JAVA

Possible Duplicate: What is reflection, and why is it useful? What is the exact use of reflection in Java? Can anyone demonstrate with an example? If there are any related concepts, please share. Reflection is a powerful construct which is often used by underlying libraries such as Guice and Hibernate to make life easier. It is often used where a class needs to be configured and then ins

用Core JAVA编程

可能重复: 什么是反思,为什么它有用? Java中反射的确切用法是什么? 任何人都可以用一个例子来证明? 如果有任何相关的概念,请分享。 反射是一个强大的构造,它经常被底层库(如Guice和Hibernate)用来使生活更轻松。 它经常用于需要配置类然后实时实例化的地方。 例如: public Strategy prepare(String clazz, Properties config) throws ClassNotFoundException, InstantiationException, IllegalAccessExcepti

How does @autowired annotation works for a private field?

This question already has an answer here: What is reflection and why is it useful? 20 answers How does Spring @Autowired work 4 answers It works with reflection. Here you can find an example of how to set public fields. But setting private fields does not make much of a difference @Component public class A(){} @Component public class B(){ @Autowired private A a; } Spring create beans

@autowired注释如何用于私人领域?

这个问题在这里已经有了答案: 什么是反思,为什么它有用? 20个答案 Spring @Autowired如何工作4个答案 它适用于反思。 在这里您可以找到如何设置公共字段的示例。 但设置私人领域并没有太大区别 @Component public class A(){} @Component public class B(){ @Autowired private A a; } Spring创建bean作为@Component提到。 这里首先创建bean A,并且由于B依赖于A,那么完成A到B的注入。 不需要任何setter。 只

What happens if you don't roll back a transaction in Hibernate?

Everything I read about Hibernate states that you must roll back a transaction and close the session when an error occurs, and there's usually some variation of the following code (taken from Hibernate's docs) given as an example: Session sess = factory.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); // do some work ... tx.commit(); } catch (Runt

如果不在Hibernate中回滚事务,会发生什么?

我所读到的关于Hibernate的一切都表明,当发生错误时,您必须回滚事务并关闭会话,并且通常会给出以下代码(取自Hibernate的文档)的一些变体,例如: Session sess = factory.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); // do some work ... tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; // or display error message } fi