java - Reflection: How to compare two objects from unkown type -


i'm doing application custom decision making. let's have 4 tables: documment, file, task , process. i'm working jpa every table translated entity, java class variables. process have many task, task have file associated , file have many documments associated. want configure can compare attribute in task attribute in process. example: configure in table decision:

attribute 1: process limit date attribute 2: task actual date operator: >

what i'm trying on runtime know valor of variable take decisions.

and now. have these methods:

public object rungetter(field field, class o, object instance) {     (method method : o.getmethods()) {         if ((method.getname().startswith("get")) && (method.getname().length() == (field.getname().length() + 3))) {             if (method.getname().tolowercase().endswith(field.getname().tolowercase())) {                 try {                     system.out.println("method name: " + method.getname());                     return method.invoke(instance);                 } catch (illegalaccessexception | invocationtargetexception e) {                 }             }         }     }     return null; } 

after search field in class, run getter , catch object in return. that's object want compare. don't know object did find it's type (which save in table):

public <t> t convertirobjeto(object objetoaconvertir, class<t> clasedestino) { try {     return clasedestino.cast(objetoaconvertir); } catch(classcastexception e) {     return null; } 

that return me string.class, integer.class.

my idea first method, call second 1 sending first method's return object , in return object. let's said save integer. wish compare if integer 1 higher or lower integer 2. theory (wish possibly stupid) said can do:

 if (convertirobjeto(obtenervaloratributo(atributo1),class.forname(atributo1.gettipoatributo())) <convertirobjeto(obtenervaloratributo(atributo2),class.forname(atributo2.gettipoatributo()))) { 

instead of cast

(integer) obtenervaloratributo(atributo2) 

but when try compare compiler throws following error:

bad operand types binary operator '<' first type: cap#1 second type: cap#2 cap#1, cap#2 fresh type-variables: cap#1 extends object capture of ? cap#2 extends object capture of ? 

i think, compiler trying tell me: sir, don't know these objects are, stop trying burn me please. question are:

if(is there *easier* solution problem reflection i'm trying do?) else if(is there way fix problem compare 2 variables different classes searching in database name, table , maybe type?) 

edit: question edited asking for full problem , not solution though.

class.forname returns class<?>. need cast correct class type:

integer first = convertobject(new integer(5), (class<integer>) class.forname("java.lang.integer")); integer second = convertobject(new integer(10), (class<integer>) class.forname("java.lang.integer"));  system.out.println(first < second); 

this allow method infer generic type. since class.forname returns class<?> , not class, thet` type in method not infered class type unless cast first.

as better solution, i'm gonna honest, have no clue you're trying achieve this. check out the xy problem, seeking think solution actual problem @ hand, rather seeking ultimate goal


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -