如何将(java.lang.Class)类转换为使用反射获得的类

时间:2017-07-28 18:27:10

标签: jsf reflection primefaces java.lang.class

我正在尝试构建一个模块来管理我的数据库目录,例如:国家,企业,用户等。用户应该从组合框中选择目录,系统应该显示一个具有主要列的表(数据库中不为null,而我预定义了一些)。从3个目标我只实现了2个: 1.-使用反射选择目录后,从实体类中获取@NotNull个字段 2.-显示具有动态列的表,也从上面检索它们。  但是3号给我带来了麻烦。问题是,我在视图中使用此folling代码动态显示列(基于我存储在对象中的@NotNull字段),(https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml):

<p:dataTable  id="conceptos" var="pojo" value="#{catalogoMB.comocombo}>
<p:columns value="#{catalogoMB.columns}" var="column" 
columnIndexVar="colIndex" sortBy="#{pojo[column.property]}" filterBy="#
{pojo[column.property]}">
        <f:facet name="header">
        <h:outputText value="#{column.header}" />
        </f:facet>
        <h:outputText value="#{pojo[column.property]}" />
</p:columns>
</p:dataTable> 

例如,以正常方式,没有反射,上面的代码将像这样工作:   comocombo 将具有以下属性:name,value,id;我的列数将是相同的:名称,值,ID ... 问题是,comocombo是一个List<Object>对象,我存储了字段的反射类值,它返回java.lang.class而不是EntityClass的实例,尽管我设法调用了setter和getter来自该类的对象实例(combito) - 相反 - 所以当我尝试显示pojo[column.property] - &gt; comocombo [&#34; id&#34;],comocombo [&#34; name&#34;]或comocombo [&#34; value&#34;]它向我发送一个例外,说明java.lang.class没有&#39这有任何属性....我怎样才能达到它们?我已经读过关于Map<String, String>.cast()但我不确定这可能就是这样。

public void populateT(){ 
comocombo=new ArrayList<>();
Object tt ;
y = tabla.get(tabla.size()-1).getConcpetos(); //result of query type: 
FindAll from the entity Class 
try{
Class combito= Class.forName("sipe.services."+ catName); //the "path" of the 
Entity Classes
for (Integer j=0; j<y.size()-1; j++){
tt=y.get(j);
            for (Integer i=0; i< tabla.size()-1; i++){
            tucampo=minustomayus(y.get(j).getClass().getDeclaredField(tabla.get(i).getNombre_c()).getName()); //tabla.get(i).getNombre_c()-> here I've stored the @NotNull properties' names (countryid, countryname...) whic are the same in  columns = new ArrayList<ColumnModel>();  (catalogoMB.columns in the view) 
            Class cls= Class.forName("sipe.services."+ catName);
            Method method = cls.getDeclaredMethod("get"+tucampo); // for example "countryid" -> getCountryid              
            Class<?> type = null;
            for (Method methods : combito.getDeclaredMethods())
            { //Here I'm trying to invoke setter of the Entity Class in order to store its values..
              //equivalent to: if o is an instance of Entity Class Country: Country o = new Country(); o.setCountryid(2);
                if (methods.getName().contains("set"+tucampo)){
                type=method.getReturnType();
                methods.invoke(combito.newInstance(),method.invoke(tt));
             }
            }

1 个答案:

答案 0 :(得分:0)

我让我以这种方式工作,希望它有所帮助

 private void createDynamicColumns(){
            //split column templates
            String[] columnKeys = columnTemplate.split(" ");
            //set first list to null
            //this should be a List with no type specified
            //eg List objects = new ArrayList<>();
            //since you will dynamically populate it based on what the user selects from combobox
            objects = null;
            //clear columns
            columns.clear();
            //create a class from combobox selection
            Class<?> forName = Class.forName("pathToClass." + comboboxSelection);
            //get declared fields in that class
            List<Field> asList = Arrays.asList(forName.getDeclaredFields());
            //for each columkeys
            //if method.getName() can be found in columnKeys
            //add that columnKey to the new columnList;
            for(int i =0;i< asList.size();i++)
                for (String columnKey : columnKeys) {
                    String fieldName = asList.get(i).getName();
                    if (columnKey.equalsIgnoreCase(fieldName)) {
                        columns.add(fieldName);
                    }
                }

          //fetch from the database the objects list
           objects = DAO.findAll();
        }

和facelets页面

<p:dataTable var="object" value="#{backingBean.objects}">                    
        <p:columns value="#{BackingBean.columns}" var="column">
            <f:facet name="header">
                <h:outputText value="#{column}" />
            </f:facet>
            <h:outputText value="#{object[column]}" />
        </p:columns>
</p:dataTable>

请记住处理异常

相关问题