I imported everything according to the Projectlombok site into my project.
I wonder if it is possible to call the getters and other things generated by Lombok right in the source code? I've tested that the JSON serializer can access these after compiling, but I can't use them in the project.
For example:
#Getter
class Person {
String name;
}
class MyClass {
void f(Person p) {
p.getName(); // doesn't compile for me
}
}
Tell me if I am wrong that Lombok can be used for this purpose.
Related
I am trying to use Proguard to keep my private fields, but it won't work.
I stole most of this from Proguard keep classmembers because that question is similar to what I'm asking, and also followed this link How to tell ProGuard to keep private fields without specifying each field
But it still doesn't work.
I want to make a library for another company and still keep my access level modifiers fields and methods.
Proguard:
-keepclassmembers class com.example.mylibrary.Bedika {
private <fields>;
}
-keep class com.example.mylibrary.Bedika {
*;
}
My AAR library
public class Bedika {
private String stam;
public Bedika(String stam) {
this.stam = stam;
}
public void print() {
System.out.println(stam);
}
}
output after Proguard:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.example.mylibrary;
public class Bedika {
public String stam;
public Bedika(String var1) {
this.stam = var1;
}
public void print() {
System.out.println(this.stam);
}
}
It seems like R8 is causing this issue and not Proguard.
Go into gradle.properties, and set android.enableR8=false. Next time you build, it will use Proguard.
R8 is Google's answer to Proguard and in the recent versions of The Android Gradle Plugin (3.4.0+) it defaults to R8's code shrinker/obfuscator. There are some pros and cons to using Google's version instead of Guardsquare's technology.
For more information, look at the documentation
You can usually add the #Keep annotation above the class or object (in Java or Kotlin). This is pretty self-explanatory, and will work.
So looking at my class files the getters/setters are being generated just fine, but I'm trying to write a copy method that looks like this in the same jar.
#Data
public class SoftwareVersions {
private String applicationVersion;
void copyTo( MonitorFoleyConnection mfc ) {
mfc.setApplicationVersion( applicationVersion );
}
}
in gradle
annotationProcessor("org.projectlombok:lombok:1.+")
compileOnly("org.projectlombok:lombok:1.+")
is it possible to get intellij to recognize the existance of this method?
Yes, you have. You just need to do two things:
Install the Lombok Plugin for Intellij:
Enable the annotation processing:
I've a class Product:
#Data
#SuperBuilder
public class Product {
private String name;
private String manufacturer;
}
and an extended class
#Data
#SuperBuilder
public class Frame extends Product{
private String model;
}
I'm trying to create a Frame object using the builder:
return Frame.builder()
.name("Frame ABC")
.manufacturer("Manufacturer")
.model("Model 1")
.build();
I'm using IntelliJ 2019.1.1 with Lombok plugin but unfortunately the compiler marks as error the .name() and .manufacturer() methods.
I saw this issue opened and I'm wondering if there is a workaround to make my code to work.
No, not until the issue is resolved.
Its a chicken and egg issue. Until the classes with the #SuperBuilder annotations are compiled, the actual generated builder methods do not exist. The plugin (once updated/fixed) works with the IDE for these methods so that even though they don't exist yet, the plugin tells the IDE what they will be when compilation takes place.
There are ways to 'cheat' but they are all hacks - for example, you could compile your (super)builder classes in their own jar and then import that jar into your project. As you compiled the SuperBuilder classes they now contain all the generated methods, thus the IDE will see the actual methods and will therefore propose them if you try use them. Functional but not very useful... if you need to update the SuperBuilder annotated classes, you now have to compile them each time before the changes become visible. Obviously you could create build tasks to do this for you, but you are always working around the actual issue which is plugin support.
This workaround works for me:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Product {
private String name;
private String manufacturer;
}
#Data
#NoArgsConstructor
public class Frame extends Product{
private String model;
#Builder
public Frame(String name, String manufacturer, String model){
super(name, manufacturer);
this.model = model;
}
}
The only problem I see is when you have a lot of fields in the class it is becoming annoying to write such constructors, but still I think it worth it cause at the end you can access parent and child fields.
return Frame.builder()
.name("Frame ABC")
.manufacturer("Manufacturer")
.model("Model 1")
.build();
Build with child member fields first, then parent member fields, with a type casting seems to work for me:
return (Frame) Frame.builder()
.model("Model 1")
.name("Frame ABC")
.manufacturer("Manufacturer")
.build();
I'm using lombok in intellij, and have the plugin installed.
My problem is that when I use the #Date notation in my class, only that class can see the methods created by lombok. So if my class declaration looks like this:
#Document
#Data
public class dbDocument {
#Id
private String uniqueId;
And the method
public String testGetter (dbDocument doc) {
return doc.getUniqueId;
}
Will work inside the dbDocument class, but not in any other class. (where I get a Java: cannot find symbol error)
How can I fix/debug this?
Oops.
Looks like I was spelling the method wrong while invoking it. Nothing to see here.
I am working on a project that is having a layered Architecture.
I have a interface ABC and in that interface I have a enum XYZ
For ex
public interface ABC {
public enum XYZ {
CONSTANT1("SOMETHING"),
CONSTANT2("SOMETHING3");
final String name;
private TYPE(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
}
}
I am compiling this using ant and using this jar file in other layer. In that layer I am trying to access it like
String name=ABC.XYZ.CONSTANT1.getName();
I am getting symbol not found error during compile. I verified classpath is set properly.
I am using ant v 1.8 and java 1.6.
Why I am getting this error ?
First of all, don't nest the enum in an interface. It's perfectly fine that it has its own source file named after the enum.
Second, I assume you mean XYZ instead of TYPE in your private constructor?
Last, you should be able to use it in that way, no matter if compiled via ant or within eclipse or directly using javac. Probably you have not compiled everything - the way you did it, there should be ABC.class (the interface), ABC$XYZ.class (the inner enum) and the class file of your calling class.