简体   繁体   English

如何在Java中运行时生成setter和getter?

[英]How to generate setters and getters at runtime in java..?

I am having an functionality through which I am displaying the properties of a document on JSF. 我有一个功能,可以通过它在JSF上显示文档的属性。 The properties are coming through a map and I am iterating it over the JSF as follows: 这些属性通过地图传递,我在JSF上对其进行了如下迭代:

<af:iterator var="list" value="#{MainContentBean.documentProperties}" id="i1">
    <af:inputText label="#{list.key}" id="it1"editable="always" value="list.value">                
</af:iterator>

This is working fine till I want to use it only for the display purpose, but if I want to make it up datatable, ie the user can change the values of the input text box it's not working. 直到我只想将其用于显示目的时,此方法才能正常工作,但如果要使其成为数据表,即用户可以更改输入文本框的值,则此方法将不起作用。 The input text box is always displayed in read only mode if the setters and getters are not present. 如果不存在setter和getter,则输入文本框始终以只读模式显示。 The values I am getting in map is uncertain, so in this situation, how can I generate setters and getters for each property value at runtime? 我在映射中获取的值是不确定的,因此在这种情况下,如何在运行时为每个属性值生成设置器和获取器? Is this possible to do that? 这可以做到吗?

Yes, it is possible using cglib. 是的,可以使用cglib。 Take a look at "Adding setter/getters with cglib to compete with RoR... Dumb idea? Input from Crazy Bob" 看看“使用cglib添加setter / getter与RoR竞争...愚蠢的想法?来自Crazy Bob的输入”

Also, take a look at this answer which suggest using ASM 另外,看看这个建议使用ASM的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM