[英]Java: “attempting to assign weaker access privilege error” (Compiling JDK 1.6 Source with 1.8)
Using Gradle, we are trying to compile legacy Java code, which was developed for JDK 1.6 with a JDK 1.8 compiler. 我们正在使用Gradle尝试编译遗留Java代码,该Java代码是使用JDK 1.8编译器为JDK 1.6开发的。 At some point the compilation process quits with the error
在某个时候,编译过程因错误而退出
attempting to assign weaker access privileges;
试图分配较弱的访问权限; was public
是公开的
(The cause of the error itself is obvious: we have a method in an abstract class, which is declared public , but the implementing class declares it as protected .) (错误的原因很明显:我们在抽象类中有一个方法,将其声明为public ,但实现类将其声明为protected 。)
Using JDK 1.6 for compiling, we never had any issues with this. 使用JDK 1.6进行编译,我们从来没有遇到任何问题。 Now for several reasons, we have to compile our code with Java 8, having us run into this issue.
现在由于多种原因,我们不得不使用Java 8来编译代码,从而使我们遇到这个问题。
We already tried project setting -PsourceCompatibility=1.6
(also -PtargetCompatibility=1.8
) when building, without effect. 我们已经在构建时尝试了项目设置
-PsourceCompatibility=1.6
(也有-PtargetCompatibility=1.8
),但没有效果。
At the moment, refactoring the whole product code (expecting more and similar errors to follow) is no option, so we are looking for a solution to build the old code with the new JDK. 目前,无法重构整个产品代码(可能会出现更多类似错误),因此我们正在寻找一种使用新JDK构建旧代码的解决方案。
Any help for this? 有什么帮助吗?
The only explanation to the fact that your system used to work with Java 1.6 is that the method access in the superclass has been changed to public
without recompiling the subclass. 您的系统曾经使用过Java 1.6的事实的唯一解释是,超类中的方法访问已更改为
public
而无需重新编译子类。 Lowering accessibility in a subclass has been prohibited from the beginning. 从一开始就禁止降低子类的可访问性。
Java Language Specification 1.6 provides this explanation on page 344: Java语言规范1.6在第344页提供了以下说明:
if the package points defines the
class Point
:如果包点定义了
class Point
:package points; public class Point { public int x, y; protected void print() { System.out.println("(" + x + "," + y + ")"); } }
used by the Test program:
由测试程序使用:
class Test extends points.Point { protected void print() { System.out.println("Test"); } public static void main(String[] args) { Test t = new Test(); t.print(); } }
then these classes compile and Test executes to produce the output:
然后将这些类编译并执行Test以产生输出:
Test
If the method
Point
is changed to bepublic
, and then only thePoint
class is recompiled, and then executed with the previously existing binary forTest
then no linkage error occurs, even though it is improper, at compile time, for a public method to be overridden by a protected method ( as shown by the fact that the classTest
could not be recompiled using this newPoint
class unless print were changed to bepublic
.) (emphasis added)如果将
Point
类中的public
,然后仅重新编译Point
类,然后使用先前存在的二进制文件执行Test
则即使在编译时不正确,也不会发生任何链接错误。 public方法将由受保护的方法覆盖( 如以下事实所示:除非将print更改为public
否则无法使用此新Point
类重新编译Test
类 。)(强调)
If you must re-create the exact behavior with Java 1.8 compiler, change accessibility in the superclass to protected
, compile the superclass and subclass, then change accessibility in the superclass back to public
, and compile only the superclass. 如果必须使用Java 1.8编译器重新创建确切的行为,请将超类中的可访问性更改为
protected
,编译超类和子类,然后将超类中的可访问性更改回public
,仅编译超类。 However, at this point I would strongly recommend changing the subclass to provide proper accessibility. 但是,在这一点上,我强烈建议更改子类以提供适当的可访问性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.