简体   繁体   English

Java:“试图分配较弱的访问特权错误”(使用1.8编译JDK 1.6 Source)

[英]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 print in class Point is changed to be public , and then only the Point class is recompiled, and then executed with the previously existing binary for Test 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 class Test could not be recompiled using this new Point class unless print were changed to be public .) (emphasis added) 如果将Point类中的print方法更改为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.

相关问题 java试图分配较弱的访问权限错误 - java attempting to assign weaker access privilege error Android AsyncTask错误,尝试分配较弱的访问权限。 - Android AsyncTask error, attempting to assign weaker access privileges. 试图分配较弱的访问权限; 是isCancelled的公开错误 - attempting to assign weaker access privileges; was public error for isCancelled Android Studio:房间数据库实施时出现“尝试分配较弱的访问权限”错误 - Android Studio: "attempting to assign weaker access privileges" error on Room Database implementation 泛型问题:clone()尝试分配较弱的访问权限 - Generics issue: clone() attempting to assign weaker access privileges 无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限; 是公开的 - Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public 使用 JDK 1.8 java 编译:无法访问类文件...找不到类文件 - Compiling with JDK 1.8 java: cannot access class file… class file not found jdk1.8中的ExceptionInInitializerError不是jdk1.6中的 - ExceptionInInitializerError in jdk1.8 not in jdk1.6 Java 1.6-> 1.8,相同的擦除编译错误 - Java 1.6 -> 1.8, same erasure compile error 为什么我们不能在子类中分配较弱的权限 - why can't we assign weaker privilege in subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM