英文:
How to access getter method in calling Java class or Java interface using lombok?
问题
使用JDK 1.8和以下lombok版本:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
具有以下Java接口:
package com.myapp.bridge;
public interface MyBridge {
public void run();
}
实现类:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
}
调用类:
package com.myapp.bridge;
import lombok.Getter;
public class App {
@Getter
private MyBridge bridge = new MyBridgeHandler();
public void printValue() {
// IntelliJ IDEA不似乎允许我访问这个getter?
String aValue = getBridge().getValue();
System.out.println(aValue);
}
public static void main(String args []) {
App app = new App();
app.printValue();
}
}
问题:
-
我如何在调用类中获取/访问
MyBridgetHandler.getValue()
方法(在我在printValue
方法中在getBridge().
后面加上点之后,IntelliJ IDEA甚至不会显示或选择此方法)? -
它应该放在
MyBridge
接口中吗?如果是这样,实现会是什么样子?
请注意,这不是一个特定于IntelliJ的问题,因为我的其他POJO使用了基于lombok的@Getter
和@Setter
注释,并且可以通过IntelliJ IDEA在调用类中工作。
可以看到hello
被打印到stdout 10次,所以它是被设置了的。
似乎是一个可见性/访问权限问题。
英文:
Using JDK 1.8 and the following lombok version:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
Have the following Java interface:
package com.myapp.bridge;
public interface MyBridge {
public void run();
}
Implementing class:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
}
Calling class:
package com.myapp.bridge;
import lombok.Getter;
public class App {
@Getter
private MyBridge bridge = new MyBridgeHandler();
public void printValue() {
// IntelliJ IDEA doesn't seem to let me access this getter?
String aValue = getBridge().getValue();
System.out.println(aValue);
}
public static void main(String args []) {
App app = new App();
app.printValue();
}
}
Question(s):
-
How do I obtain / access the
MyBridgetHandler.getValue()
method inside my calling class
(IntelliJ IDEA's code completion doesn't even display / or opt for this method after I put a dot aftergetBridge().
inside myprintValue
method)? -
Should it be placed inside the
MyBridge
interface? If so, how would the implementation look like?
Note, this is not an IntelliJ specific question as my other POJOs have lombok-based @getter
& @setter
annotations and work inside calling classes via IntelliJ IDEA.
Am able to see hello
be printed to stdout 10 times so it is getting set.
Seems like a visibility / accessibility issue.
答案1
得分: 0
已修复/找到解决方案...
在MyBridge
接口内,我添加了以下方法:
package com.myapp.bridge;
public interface MyBridge {
public void run();
String getValue();
}
在MyBridgeHandler
Java类内实现如下:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
public String getValue() {
return value;
}
}
大功告成!我现在能够在App.printValue()
方法内访问它。
英文:
Fixed / found the solution...
Inside the MyBridge
interface, I added the following method:
package com.myapp.bridge;
public interface MyBridge {
public void run();
String getValue();
}
Implemented it like this inside MyBridgeHandler
Java class:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
public String getValue() {
return value;
}
}
Voila! I am able to access it now inside the App.printValue()
method.
专注分享java语言的经验与见解,让所有开发者获益!
评论