本文主要是介绍java8: 因为lambda表达写法造成的编译错误: java.lang.AssertionError: isSubClas,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天在命令行编译的时间遇到了一个以前从来没有遇到的编译错误:
编译器 (1.8.0_401) 中出现异常错误。如果在 Bug Database (http://bugs.java.com) 中没有找到该错误, 请通过 Java Bug 报告页 (http://bugreport.java.com) 建立该 Java 编译器 Bug。请在报告中附上您的程序和以下诊断信息。谢谢。
java.lang.AssertionError: isSubClass DEVICE_T
at com.sun.tools.javac.code.Symbol.isSubClass(Symbol.java:447)
at com.sun.tools.javac.code.Symbol.isMemberOf(Symbol.java:456)
at com.sun.tools.javac.code.Symbol M e t h o d S y m b o l . o v e r r i d e s ( S y m b o l . j a v a : 1479 ) a t c o m . s u n . t o o l s . j a v a c . c o d e . T y p e s MethodSymbol.overrides(Symbol.java:1479) at com.sun.tools.javac.code.Types MethodSymbol.overrides(Symbol.java:1479)atcom.sun.tools.javac.code.TypesImplementationCache.implementationInternal(Types.java:2693)
at com.sun.tools.javac.code.Types I m p l e m e n t a t i o n C a c h e . g e t ( T y p e s . j a v a : 2675 ) a t c o m . s u n . t o o l s . j a v a c . c o d e . T y p e s . i m p l e m e n t a t i o n ( T y p e s . j a v a : 2704 ) a t c o m . s u n . t o o l s . j a v a c . c o d e . S y m b o l ImplementationCache.get(Types.java:2675) at com.sun.tools.javac.code.Types.implementation(Types.java:2704) at com.sun.tools.javac.code.Symbol ImplementationCache.get(Types.java:2675)atcom.sun.tools.javac.code.Types.implementation(Types.java:2704)atcom.sun.tools.javac.code.SymbolMethodSymbol.implementation(Symbol.java:1539)
at com.sun.tools.javac.code.Symbol M e t h o d S y m b o l . i m p l e m e n t a t i o n ( S y m b o l . j a v a : 1528 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . n o t O v e r r i d d e n I n ( R e s o l v e . j a v a : 419 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . i s A c c e s s i b l e ( R e s o l v e . j a v a : 405 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . i s A c c e s s i b l e ( R e s o l v e . j a v a : 365 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . s e l e c t B e s t ( R e s o l v e . j a v a : 1463 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . f i n d M e t h o d I n S c o p e ( R e s o l v e . j a v a : 1633 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . f i n d M e t h o d ( R e s o l v e . j a v a : 1725 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e . f i n d M e t h o d ( R e s o l v e . j a v a : 1677 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . R e s o l v e MethodSymbol.implementation(Symbol.java:1528) at com.sun.tools.javac.comp.Resolve.notOverriddenIn(Resolve.java:419) at com.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:405) at com.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:365) at com.sun.tools.javac.comp.Resolve.selectBest(Resolve.java:1463) at com.sun.tools.javac.comp.Resolve.findMethodInScope(Resolve.java:1633) at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1725) at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1677) at com.sun.tools.javac.comp.Resolve MethodSymbol.implementation(Symbol.java:1528)atcom.sun.tools.javac.comp.Resolve.notOverriddenIn(Resolve.java:419)atcom.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:405)atcom.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:365)atcom.sun.tools.javac.comp.Resolve.selectBest(Resolve.java:1463)atcom.sun.tools.javac.comp.Resolve.findMethodInScope(Resolve.java:1633)atcom.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1725)atcom.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1677)atcom.sun.tools.javac.comp.ResolveMethodReferenceLookupHelper.lookup(Resolve.java:3192)
at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3348)
at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3333)
at com.sun.tools.javac.comp.Resolve.getMemberReference(Resolve.java:2741)
at com.sun.tools.javac.comp.DeferredAttr D e f e r r e d C h e c k e r . v i s i t R e f e r e n c e ( D e f e r r e d A t t r . j a v a : 1183 ) a t c o m . s u n . t o o l s . j a v a c . t r e e . J C T r e e DeferredChecker.visitReference(DeferredAttr.java:1183) at com.sun.tools.javac.tree.JCTree DeferredChecker.visitReference(DeferredAttr.java:1183)atcom.sun.tools.javac.tree.JCTreeJCMemberReference.accept(JCTree.java:1973)
at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
at com.sun.tools.javac.comp.DeferredAttr F i l t e r S c a n n e r . s c a n ( D e f e r r e d A t t r . j a v a : 913 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . D e f e r r e d A t t r . i s D e f e r r e d ( D e f e r r e d A t t r . j a v a : 1100 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . a t t r i b A r g s ( A t t r . j a v a : 670 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . v i s i t A p p l y ( A t t r . j a v a : 1816 ) a t c o m . s u n . t o o l s . j a v a c . t r e e . J C T r e e FilterScanner.scan(DeferredAttr.java:913) at com.sun.tools.javac.comp.DeferredAttr.isDeferred(DeferredAttr.java:1100) at com.sun.tools.javac.comp.Attr.attribArgs(Attr.java:670) at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1816) at com.sun.tools.javac.tree.JCTree FilterScanner.scan(DeferredAttr.java:913)atcom.sun.tools.javac.comp.DeferredAttr.isDeferred(DeferredAttr.java:1100)atcom.sun.tools.javac.comp.Attr.attribArgs(Attr.java:670)atcom.sun.tools.javac.comp.Attr.visitApply(Attr.java:1816)atcom.sun.tools.javac.tree.JCTreeJCMethodInvocation.accept(JCTree.java:1465)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)
at com.sun.tools.javac.comp.Attr.visitReturn(Attr.java:1686)
at com.sun.tools.javac.tree.JCTree J C R e t u r n . a c c e p t ( J C T r e e . j a v a : 1384 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . a t t r i b T r e e ( A t t r . j a v a : 576 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . a t t r i b S t a t ( A t t r . j a v a : 645 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . a t t r i b S t a t s ( A t t r . j a v a : 661 ) a t c o m . s u n . t o o l s . j a v a c . c o m p . A t t r . v i s i t B l o c k ( A t t r . j a v a : 1124 ) a t c o m . s u n . t o o l s . j a v a c . t r e e . J C T r e e JCReturn.accept(JCTree.java:1384) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:645) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:661) at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1124) at com.sun.tools.javac.tree.JCTree JCReturn.accept(JCTree.java:1384)atcom.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)atcom.sun.tools.javac.comp.Attr.attribStat(Attr.java:645)atcom.sun.tools.javac.comp.Attr.attribStats(Attr.java:661)atcom.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1124)atcom.sun.tools.javac.tree.JCTreeJCBlock.accept(JCTree.java:909)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)
at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:645)
at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1013)
at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)
at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:645)
at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4364)
at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4272)
at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4201)
at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4176)
at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:169)
at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:785)
at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
DEVICE_T
是我的代码中一个泛型类的泛型参数,在代码中被广泛使用,代码有几千行,一时很难知道到底是哪一行代码出了问题。
同样的代码在Eclipse等IDE中编译可以通过,但是在命令行却不行(同样使用JDK8编译器),这一点我也很不解。
关于这个异常,网上是可以查到很多报告的,如:
https://github.com/oracle/graal/issues/4156
https://bugs.openjdk.org/browse/JDK-8273234
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8071783
按照 《Exception java.lang.AssertionError: isSubClass when building Ghidra》的说明,这是个编译器的BUG,将在JDK 17解决。
我的项目是基于JDK8的,为了这个问题换编译器我可换不起。
只能用最笨的排除法,来尝试查找到底哪一部分的代码触发了这个BUG.
很幸运,最终找到了。
示例如下:
import com.google.common.base.Predicate;public abstract class DaoManagement<DEVICE_T extends DeviceBean> extends BaseDao<DEVICE_T>{public abstract <B extends DEVICE_T> B copy(B bean, Predicate<Integer> fieldFilter,int... fieldList);public DEVICE_T copy(DEVICE_T deviceBean, DEVICE_T old){/** 在这种情况下,简化的lambda表达写法会引起编译错误 */ return copy(deviceBean, deviceBean::isModified,1,2,3);}
}
public interface BaseBean {public boolean isModified(int i);public boolean isModified(int... columnIDs);
}
public abstract class DeviceBean extends BaseRow{}
public abstract class BaseRow implements BaseBean{@Overridepublic boolean isModified(int i) {return false;}
}public abstract class BaseDao<DEVICE_T extends DeviceBean> {public DEVICE_T copy(DEVICE_T deviceBean, DEVICE_T old){return deviceBean;}
}
如上代码中使用了lambda表达式deviceBean::isModified
,这这种写法在命令行编译时会抛出异常:java.lang.AssertionError: isSubClass DEVICE_T
改一下写法就行:v->deviceBean.isModified(v)
,即:
public abstract class DaoManagement<DEVICE_T extends DeviceBean> extends BaseDao<DEVICE_T>{public abstract <B extends DEVICE_T> B copy(B bean, Predicate<Integer> fieldFilter,int... fieldList);public DEVICE_T copy(DEVICE_T deviceBean, DEVICE_T old){return copy(deviceBean, v->deviceBean.isModified(v),1,2,3);}
}
这是一种非常偶然问题,一般的项目如果不涉及复杂的泛型参数,不会遇到。记下来备查。
这篇关于java8: 因为lambda表达写法造成的编译错误: java.lang.AssertionError: isSubClas的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!