简体   繁体   English

从不同的jar中删除重复的代码来调用同一对象

[英]Remove duplicated code calling same object from different jars

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: 我在Java中创建对象时遇到问题,我有3个jar,每个人都有一个名为“ Person”的类,我将这些jar文件包含到我的项目中,并且我需要定义3个对象Person,问题如下:

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: 如您所见,这些类是“相同的”,但包是不同的,我有这个UtilClass,因为我在另一个类中定义了一个方法:

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? 如何简化类UtilClass并避免重复代码? I can't change my jar files. 我无法更改我的jar文件。

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 ) 例如,使用BeanUtilsConstructorUtils ,它们比java Bean api更易于使用(请参阅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);
        }
    }
}

If these classes have nothing in common, ie 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: 如果这些类没有共同点,即未实现可使用的公共接口,则可以使用标准java.beans包在没有任何第三方库的情况下解决任务:

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: 感谢Generics,该方法声明返回您传入的Class实例的类型。然后,您的用例将如下所示:

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));

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. 如果这三个Persos类没有通用的超类,它们具有name和lastName字段,或者没有具有setName和setLastName方法的接口,则最简单的解决方案是使用反射。

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. 如果稍后将Person的名称重命名为firstName,则您的代码将编译,并且没有任何警告您UtilClass已损坏。

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

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