简体   繁体   中英

No Constructor Found Java + Lombok

Main

public static List<Viking> uploadVikings(){
        List<Viking> vikings = new ArrayList<Viking>();

        vikings.add(new Viking("Lean",23,100, new DrinkVikingImp(), new PeeVikingImp(),10));
        vikings.add(new Viking("Thor",24,99, new DrinkVikingImp(), new PeeVikingImp(),9));
        vikings.add(new Viking("Thanos",25,98, new DrinkVikingImp(), new PeeVikingImp(),9));
        vikings.add(new Viking("Hulk",26,97, new DrinkVikingImp(), new PeeVikingImp(),5));
        vikings.add(new Viking("Thrall",27,96, new DrinkVikingImp(), new PeeVikingImp(),3));

        return vikings;
    }

Viking Class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Viking extends Human{

    public Integer proDrinker;
    public Pee pee;
    public Drink drink;

    public void pee() {pee.pee();}
    public void drink() {drink.drink();}

}

Human Class

@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class Human {

    public String Name;
    public Integer Age;
    public Integer Weight;

}

Drink and Pee interface are the same

public class DrinkVikingImp implements Drink {

    @Override
    public void drink() {
        System.out.println("Viking is Drinking");
    }
}
Error: Error:(21, 21) java: no suitable constructor found for Viking(java.lang.String,int,int,com.company.models.DrinkVikingImp,com.company.models.PeeVikingImp,int)
    constructor com.company.models.Viking.Viking() is not applicable
      (actual and formal argument lists differ in length)
    constructor com.company.models.Viking.Viking(java.lang.Integer,com.company.interfaces.Pee,com.company.interfaces.Drink) is not applicable
      (actual and formal argument lists differ in length)

You can solve this problem using Lombok`s builder generator (@SuperBuilder annotation), which was introduced in version 1.18

@Data
@SuperBuilder
public abstract class Human {
...
}

@Data
@SuperBuilder
public class Viking extends Human {
...
}

Then you`ll be able to construct your Viking objects as below:

    vikings.add(Viking.builder()
            .name("Lean")
            .age(23)
            .weight(100)
            .drink(new DrinkVikingImp())
            .pee(new PeeVikingImp())
            .proDrinker(10)
            .build());

Also check your Human class field names, they should not start with a capital letter.

From lombok :

@AllArgsConstructor generates a constructor with 1 parameter for each field in your class.

Based on the error message

com.company.models.Viking.Viking(java.lang.Integer,com.company.interfaces.Pee,com.company.interfaces.Drink) is not applicable

You probably need this instead

vikings.add(10, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(9, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(9, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(5, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(3, new Viking(new DrinkVikingImp(), new PeeVikingImp()));

And then you'll have to set human attributes by yourself

This is not possible in Lombok. Although it would be a really nice feature, it requires resolution to find the constructors of the super class. The super class is only known by name the moment Lombok gets invoked. Using the import statements and the classpath to find the actual class is not trivial. And during compilation you cannot just use reflection to get a list of constructors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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