1

I have a problem with the object creation in java, I have 3 jar's and every one have a class called "Person", I included those jars files into my project, and I need to define 3 objects Person, the problem is the following:

public class UtilClass { 
    public static com.jar1.Person definePerson1() {
       com.jar1.Person person = new com.jar1.Person();
       person.setName(Constant.NAME);
       person.setLastName(Constant.LASTNAME);
       return person;
    }

    public static com.jar2.Person definePerson2() {
       com.jar2.Person person = new com.jar2.Person();
       person.setName(Constant.NAME);
       person.setLastName(Constant.LASTNAME);
       return person;
    }

    public static com.jar3.Person definePerson3() {
       com.jar3.Person person = new com.jar3.Person();
       person.setName(Constant.NAME);
       person.setLastName(Constant.LASTNAME);
       return person;
    }
}

As you can see, the classes are "the same" but the package is different, I have this UtilClass because I defined a method in another class:

public void create() {
   com.jar1.Group = new Group(UtilClass.definePerson1()); //Only accept com.jar1.Person
   com.jar2.Group = new Group(UtilClass.definePerson2()); //Only accept com.jar2.Person
   com.jar3.Group = new Group(UtilClass.definePerson3()); //Only accept com.jar3.Person
}

How I can simplify the class UtilClass and avoid duplicated code? I can't change my jar files.

Candres
  • 385
  • 2
  • 5
  • 18
  • Does your person class share common interface? – SMA Oct 16 '14 at 13:26
  • Ugly, but you could use reflection – Predrag Maric Oct 16 '14 at 13:27
  • Thanks Holger. This is only an example. Really, the 3 jar files correspond to 3 WS client, and every RequestType have a property called Person(and every Person class is the same for every jar file) and I need to set it with same values. – Candres Oct 16 '14 at 13:39

3 Answers3

1

You could use reflection to set your values.

For example, using BeanUtils and ConstructorUtils, that are easier to use than java bean api (see answer)

public static class UtilClass { 
    public static com.jar1.Person definePerson1() {
       return newPerson(com.jar1.Person.class);
    }

    public static com.jar2.Person definePerson2() {
         return newPerson(com.jar2.Person.class);
    }

    public static com.jar3.Person definePerson3() {
         return newPerson(com.jar3.Person.class);
    }

    public static <T> T newPerson(Class<T> clazz) {
        try {
            T person = ConstructorUtils.invokeConstructor(clazz, null);
            BeanUtils.setProperty(person, "name", Constant.NAME);
            BeanUtils.setProperty(person, "lastName", Constant.LASTNAME);
            return person;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Community
  • 1
  • 1
Xavier Delamotte
  • 3,519
  • 19
  • 30
1

If these classes have nothing in common, i.e. do not implement a common interface you could use, you can solve the task without any third party library using the standard java.beans package:

import java.beans.Expression;
import java.beans.Statement;

public class UtilClass {
  public static <T> T definePerson(Class<T> type) {
    try {
      Object o=new Expression(type, "new", null).getValue();
      new Statement(o, "setName", new Object[]{Constant.NAME}).execute();
      new Statement(o, "setLastName", new Object[]{Constant.LASTNAME}).execute();
      return type.cast(o);
    } catch(Exception ex) { throw new IllegalStateException(ex); }
  }
}

Thanks to Generics, the method declares to return the type of the Class instance you pass in. Then your use case would look like:

com.jar1.Group = new Group(UtilClass.definePerson(com.jar1.Person.class));
com.jar2.Group = new Group(UtilClass.definePerson(com.jar2.Person.class));
com.jar3.Group = new Group(UtilClass.definePerson(com.jar3.Person.class));
Holger
  • 285,553
  • 42
  • 434
  • 765
0

If the three Persos classes don't have a common superclass with name and lastName fields or inteface with setName and setLastName methods, the most simple solution is to use reflection.

Using reflection for this is bad practice, however. If you rename Person's name to firstName later, your code will compile, and nothing will warn you, that your UtilClass is broken.

traianus
  • 111
  • 1
  • 7