Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - Java - Lombok - The method is undefined for the type

Tags:

I downloaded the following project and imported it into Visual Studio Code:

https://github.com/oktadeveloper/okta-spring-boot-2-angular-5-example

I have a problem on the following class, when invoking: car.getName().

https://github.com/oktadeveloper/okta-spring-boot-2-angular-5-example/blob/d5c959162ed0f862a5dceb93f5957f92e052e062/server/src/main/java/com/okta/developer/demo/CoolCarController.java

which content is:

CoolCarController.java

package com.okta.developer.demo;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.stream.Collectors;

@RestController
class CoolCarController {
    private CarRepository repository;

    public CoolCarController(CarRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/cool-cars")
    @CrossOrigin(origins = "http://localhost:4200")
    public Collection<Car> coolCars() {
        return repository.findAll().stream()
                .filter(this::isCool)
                .collect(Collectors.toList());
    }

    private boolean isCool(Car car) {
        return !car.getName().equals("AMC Gremlin") &&
                !car.getName().equals("Triumph Stag") &&
                !car.getName().equals("Ford Pinto") &&
                !car.getName().equals("Yugo GV");
    }
}

here is also the content of:

Car.java

package com.okta.developer.demo;

import lombok.*;

import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;

@Entity
@Getter @Setter
@NoArgsConstructor
@ToString @EqualsAndHashCode
public class Car {
    @Id @GeneratedValue
    private Long id;
    private @NonNull String name;
}

As you can see on the image below, I'm getting the error:

[Java] The method getName() is undefined for the type Car

enter image description here

I think Visual Studio Code doesn't understand the package: lombok.

Any idea on how can I make Visual Studio Code understand that package?

Thanks!

like image 355
davidesp Avatar asked Jul 13 '18 23:07

davidesp


People also ask

How do I add Lombok to VS code?

Open VS Code and press Ctrl + Shift + X to open extension manager. Type lombok and click install. Reload VS Code when asked.

How do I enable Java code in Visual Studio?

Once you've installed the Extension Pack for Java, you can see the tips using the Java: Tips for Beginners command from the Command Palette in VS Code. Open the Command Palette (Ctrl+Shift+P) and type "java tips" to select the command.

Does Lombok work with Java 11?

Latest version of Lombok and/or IntelliJ plugin perfectly supports Java 11.

Is Lombok included in spring boot?

Lombok Dependency Note: If you're using a Spring Boot POM, Project Lombok is a curated dependency. Thus, you can omit the version (which will then be inherited from the Spring Boot parent POM).


2 Answers

Ok, Installing extension: Lombok Annotations Support for VS Code (gabrielbb.vscode-lombok) did the trick.

like image 86
davidesp Avatar answered Oct 26 '22 23:10

davidesp


If your project loads before installing this plugin Lombok Annotations Support for VS Code, you can run this command in vscode to reload the project.

Press Command + shift + P and execute:

Java: Clean Java language server workspace
like image 31
Ringtail Avatar answered Oct 27 '22 01:10

Ringtail