Lombok + javac: cannot find symbol - java-8

I'm trying to get started with Lombok. As the official demo and the documentation state one should be able to compile a class with lombok annotation with following simple steps:
Create a class and include #Data annotation in the code. Should be something like this
public #Data class Test {
private final String name;
public static void main(String[] args) {
System.out.println(new Test("name"));
}
}
Compile with javac and lombok.jar put into classpath:
javac -cp lombok.jar Test.java -verbose
Doing so unfortunately I'm running into an error:
Round 1:
input files: {test.Test}
annotations: [Data]
last round: false
Processor lombok.launch.AnnotationProcessorHider$AnnotationProcessor matches [Data] and returns false.
[parsing started lombok.javac.apt.EmptyLombokFileObject#54f47846]
[parsing completed 1ms]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_144\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_144\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
Round 2:
input files: {}
annotations: []
last round: false
Processor lombok.launch.AnnotationProcessorHider$AnnotationProcessor matches [] and returns false.
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_144\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_144\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
Round 3:
input files: {}
annotations: []
last round: true
Test.java:3: error: cannot find symbol
public #Data class Test {
^
symbol: class Data
[total 506ms]
1 error
I have javac 1.8.0_144, lombok 1.18.0 and running it on Windows 8.1 Pro.
Any ideas would be very helpful!

The cause of compilation error was missed import:
import lombok.Data
Thanks to #SzymonStepniak who caught the problem.

Related

How to provide the value of a #Nested property of a gradle task?

The gradle doc describes the #Nested annotation for custom gradle tasks:
https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_annotations
Unfortunately, there is no complete example of this mechanism in terms of how it is used in a build.gradle file. I created a project to demonstrate a strange exception that happens whenever gradle configures the project:
https://github.com/NicolasRouquette/gradle-nested-property-test
The build.gradle has the following:
task T(type: NestedTest) {
tool = file('x')
metadata = {
a = "1"
}
}
The NestedTest custom task is in the buildSrc folder:
class NestedTest extends DefaultTask {
#InputFile
public File tool
#Nested
#Input
public Metadata metadata
#TaskAction
def run() throws IOException {
// do something...
}
}
The important bit is the #Nested property whose type is really basic:
class Mlang-groovyetadata {
String a
}
When I execute the following: ./gradlew tasks, I get this:
Build file '/opt/local/github.me/gradle-nested-property-test/build.gradle' line: 26
* What went wrong:
A problem occurred evaluating root project 'gradle-nested-property-test'.
> Cannot cast object 'build_6wy0cf8fn1e9nrlxf3vmxnl5z$_run_closure4$_closure5#2bde737' with class 'build_6wy0cf8fn1e9nrlxf3vmxnl5z$_run_closure4$_closure5' to class 'Metadata'
Can anyone explain what is happening and how to make this work?
Nicolas
Looking at the unit tests in Gradle's source code, I found that the syntax for #Nested properties requires invoking the type constructor in the build.gradle file.
That is, the following works:
task T(type: NestedTest) {
tool = file('x')
metadata = new Metadata(
a: "1"
)
}

Spring-Boot and Spock fail when using Unroll

I'm setting up a demo project with Spring-Boot. For entity-persistence, I'm using Spring generated Repository implementations based on interfaces:
#Repository
public interface MovieRepository extends JpaRepository<Movie, Long> {
List<Movie> findByNameContaining(String name);
List<Movie> findByRelease(LocalDate release);
List<Movie> findByReleaseBetween(LocalDate start, LocalDate end);
List<Movie> findByNameContainingAndRelease(String name, LocalDate release);
}
To test this, I'm using Spock with Groovy, which works wonders:
#RunWith(SpringRunner.class)
#ContextConfiguration
#SpringBootTest
class MovieRepositoryTest extends Specification {
#Autowired
MovieRepository movieRepository
#Test
def findByNameContaining_shouldFindCorrectMovies() {
given:
movieRepository = this.movieRepository
when:
def result = movieRepository.findByNameContaining("Iron Man")
then:
result.size() == 3
}
}
But as soon as I try to mix in Spock's #Unroll, everything falls apart:
#Test
#Unroll
def findByNameContaining_shouldFindCorrectMovies() {
given:
movieRepository = this.movieRepository
when:
def result = movieRepository.findByNameContaining(query)
then:
result.size() == expected
where:
query || expected
"Iron Man" || 3
"Hulk" || 1
"Thor" || 3
"Avengers" || 3
"Thanos" || 0
"" || 20
}
Results in:
[INFO] Running com.spring.boot.demo.repositories.MovieRepositoryTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.spring.boot.demo.repositories.MovieRepositoryTest
[ERROR] initializationError(com.spring.boot.demo.repositories.MovieRepositoryTest) Time elapsed: 0.003 s <<< ERROR!
java.lang.Exception: Method $spock_feature_0_0 should have no parameters
I'm out of clues to what may cause this.
Any help is welcome.
Thank you
Edit 1:
Well, this is interesting. I've tried the following:
* Remove #Test -> java.lang.Exception: No runnable methods
* Remove #RunWith and #ContextConfiguration -> Unroll works, but movieRepository is not injected / wired: java.lang.NullPointerException: Cannot invoke method findByNameContaining() on null object
Fiddling with the different annotations hasn't resulted in a working scenario though. Any guesses?
Yesss, I've got it:
The RunWith was the culprit. In my Edit1, I noticed the difference with removing #Test. That got me thinking that I may be confusing JUnit testing with Spock testing. Also, the No runnable methods got me thinking. And leaving out the #RunWith since it is mostly absent in other Spock & Spring examples seemed like a good idea. And having Spring beans wired up with #ContextConfiguration is quite nice ;-). Apparently, #SpringBootTest doesn't do this?

How to register custom validator class in Grails 3

I want to implement a custom validator class. There are some tutorials out in the internet e.g. http://blog.swwomm.com/2011/02/custom-grails-constraints.html In these tutorials is described that you must register the validator class in the Config.groovy
The problem is that the Config.groovy is replaced by the application.groovy in Grails 3. My application.groovy looks like this:
import at.byte_code.businessSuite.core.NamespaceValidatorConstraint
import grails.validation.ConstrainedProperty
ConstrainedProperty.removeConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT)
ConstrainedProperty.registerNewConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT, NamespaceValidatorConstraint.class)
But when I try to run the app i get the following error:
| Error Error occurred running Grails CLI: startup failed:
script1481056327870569414787.groovy: 1: unable to resolve class at.byte_code.businessSuite.core.NamespaceValidatorConstraint
# line 1, column 1.
import at.byte_code.businessSuite.core.NamespaceValidatorConstraint
^
script1481056327870569414787.groovy: 2: unable to resolve class grails.validation.ConstrainedProperty
# line 2, column 1.
import grails.validation.ConstrainedProperty
^
How can I register my custom validator class?
This is due to the fact that in Grails 3 it is forbidden to import third-party classes in application.groovy. To use third-party classes, create a runtime.groovy file.
One solution (maybe there are others) was to add this code to the Application class itself:
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
void doWithApplicationContext() {
ConstrainedProperty.removeConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT)
ConstrainedProperty.registerNewConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT, NamespaceValidatorConstraint.class)
}
}

Spring boot #RestController

Is there a step needed, like adding jars to classpath?
I followed this:
10.2.6 Quick start Spring CLI example
Here’s a really simple web application that you can use to test your installation. Create a file called app.groovy:
#RestController
class ThisWillActuallyRun {
#RequestMapping("/")
String home() {
"Hello World!"
}
}
Then simply run it from a shell:
$ spring run app.groovy
and get:
startup failed:
file:/home/rgupta/Documents/products/microservices/postabook/hola-springboot/app.groovy: 1: unable to resolve class ResttController, unable to find class for annotation
# line 1, column 1.
#ResttController
^
file:/home/rgupta/Documents/products/microservices/postabook/hola-springboot/app.groovy: 4: unable to resolve class RequestMapping , unable to find class for annotation
# line 4, column 5.
#RequestMapping("/")
^
2 errors
[rgupta#rgupta hola-springboot]$
Change #ResttController in line 1 of app.groovy in your IDE to #RestController.
As #da_mp said, correct the spelling of #RestController. Once you've done that, you will also need to add the following import statements to the top of the file:
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
And that should solve your errors.

Add lombok plugin to IntelliJ 12.1.4. on Mac

I'm just trying to add the Lombok plugin to IntelliJ IDEA 12.1.4. on a Mac machine, see:
The documentation on this page (https://code.google.com/p/lombok-intellij-plugin/) said:
...just download, unzip to IntelliJ plugin directory and try out!
First I had to use EasyFind to find out this folder, because it was invisible...
than I put the plugin at the directory:
~/Library/Application Support/IntelliJIdeaXX
as specified on this page (http://devnet.jetbrains.com/docs/DOC-181), and redeployed the app. But the code keeps showing compile errors on every line that uses Lombok's features, even the simple #Getter and #Setter annotations.
so I've restarted the IDE, the highlighted compile time errors were gone, but when I run it on Glassfish it caught the compile time error, on the same line of code: trying to get a property using getter method that is generated by Lombok.
Exception:
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at com.codepianist.model.Model.<clinit>(Model.java:40)
... 50 more
Error:
// Error code
#Getter
private static Map<String, Language> LANGUAGES_MAP = new HashMap();
static {
for(Language l : LANGUAGES)
LANGUAGES_MAP.put(l.getId(), l); // line 40
}
Language Class:
// Language class
public class Language implements Serializable{
public Language(){}
public Language(String id, String flag) {
this.id = id;
this.flag = flag;
this.locale = new Locale(id);
}
#Getter private String id;
#Getter private String flag;
#Getter private Locale locale;
}
For information: Tried to call mvn clean install -e from my Terminal and not a single error. And the same app, runs fine with Netbeans.
May I have to configure the plugin on any IDE section?
I'm migrating from Netbeans, and just installed IntelliJ IDE, so I'm pretty new to it.
Thanks in advance.
Try installing the plugin using the following guide: http://www.jetbrains.com/idea/features/open_api_plugin_manager.html

Resources