如何使用 Lombok 在调用 Java 类或 Java 接口时访问 getter 方法?

huangapple 未分类评论46阅读模式
英文:

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();

	}
} 

问题:

  1. 我如何在调用类中获取/访问MyBridgetHandler.getValue()方法(在我在printValue方法中在getBridge().后面加上点之后,IntelliJ IDEA甚至不会显示或选择此方法)?

  2. 它应该放在MyBridge接口中吗?如果是这样,实现会是什么样子?


请注意,这不是一个特定于IntelliJ的问题,因为我的其他POJO使用了基于lombok的@Getter@Setter注释,并且可以通过IntelliJ IDEA在调用类中工作。

可以看到hello被打印到stdout 10次,所以它是被设置了的。

似乎是一个可见性/访问权限问题。

英文:

Using JDK 1.8 and the following lombok version:

&lt;dependency&gt;
	&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;version&gt;1.18.12&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

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 = &quot;hello&quot;;
		setValue(msg);
        
        // This works and prints hello ten times.
        for (int i = 0; i &lt; 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&#39;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):

  1. 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 after getBridge(). inside my printValue method)?

  2. 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 = &quot;hello&quot;;
        setValue(msg);
        
        // This works and prints hello ten times.
        for (int i = 0; i &lt; 10; i++) {
           System.out.println(getValue());
        }
    } 

    public String getValue() {
        return value;
    } 
}

Voila! I am able to access it now inside the App.printValue() method.

huangapple
  • 本文由 发表于 2020年7月23日 09:47:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63045633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定