V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  xuanyu66  ›  全部回复第 1 页 / 共 1 页
回复总数  17
2019-10-13 16:38:22 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 核心技术我也看过了,那本书对入门者还不错的。
2019-10-13 16:18:39 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 之前囫囵吞枣地看过,估计以后要重读这本书
2019-10-13 16:02:33 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 是在学 java 嘛,以后随时有问题都可以交流交流
2019-10-13 16:01:42 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 明白就 ok,我也是先跑测试了解了一下,共同学习了
2019-10-13 15:20:54 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
我其实也不是很懂为啥把”匿名内部类“规定为非静态内部类。静态内部类就不能匿名了吗
```
public enum MyEnum {

A {
void doSomething() { }
},


B {
void doSomethingElse() { }
};

static class SS {

}

public static void countDown(){
new Thread(){
@Override
public void run() {

}
}.start();
}

public void countDown1(){
new Thread(){
@Override
public void run() {

}
}.start();
}

public static void main(String[] args) {
System.out.println(MyEnum.A.getDeclaringClass());
System.out.println(MyEnum.A.getClass());
System.out.println(MyEnum.A.getClass().getSuperclass());
SS s = new SS();
System.out.println(s.getClass());
System.out.println(s.getClass().getSuperclass());
System.out.println(Enum.valueOf(MyEnum.class,"A"));
}
}
```
用这个对比更方便
2019-10-13 15:16:33 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 别纠结这些定义吧。其实静态内部类也可以是没名字的啊。
```
public enum MyEnum {

A {
void doSomething() { }
},


B {
void doSomethingElse() { }
};

static class SS {

}

public void countDown(){
new Thread(){
@Override
public void run() {

}
}.start();
}

public static void main(String[] args) {
System.out.println(MyEnum.A.getDeclaringClass());
System.out.println(MyEnum.A.getClass());
System.out.println(MyEnum.A.getClass().getSuperclass());
SS s = new SS();
System.out.println(s.getClass());
System.out.println(s.getClass().getSuperclass());
System.out.println(Enum.valueOf(MyEnum.class,"A"));
}
}
```
你看这段代码运行后会生成 MyEnum$3.class,就是你指的所谓的”匿名内部类“。你会发现底层实现不区分这些区别。没有指定名字的类就是从 1 开始编排,如果你是 static 就不会传外部类的引用,不是 static 就传引用。
class org.bupt.pms.consistence.MyEnum$3 extends java.lang.Thread {
final org.bupt.pms.consistence.MyEnum this$0;

org.bupt.pms.consistence.MyEnum$3(org.bupt.pms.consistence.MyEnum); //看这里
Code:
0: aload_0
1: aload_1
2: putfield #1 // Field this$0:Lorg/bupt/pms/consistence/MyEnum;
5: aload_0
6: invokespecial #2 // Method java/lang/Thread."<init>":()V
9: return

public void run();
Code:
0: return
}
2019-10-13 14:52:29 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 16 楼的话没懂什么意思
2019-10-13 14:30:36 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
public static org.bupt.pms.consistence.MyEnum valueOf(java.lang.String);
Code:
0: ldc #5 // class org/bupt/pms/consistence/My Enum
2: aload_0
3: invokestatic #6 // Method java/lang/Enum.valueOf:(Lj ava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
6: checkcast #5 // class org/bupt/pms/consistence/My Enum
9: areturn
2019-10-13 14:28:11 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 对于第一个问题,就是在生成 MyEnum 的时候会给你生成一个 public static T valueOf ( String )的方法,他其实是在内部调用了 Enum 的 public static <T extends Enum<T>> T valueOf(Class<T> enumType,String name)方法
2019-10-13 14:13:52 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
你如果要在枚举常量添加方法,或者实现一个 myEnum 的抽象方法,其实本质上都是用静态内部类加继承实现的。但是其实 java 的静态内部类也是一个 trick,真正生成的时候还是会有外部类的单独文件。如果是匿名的内部类就会是$1,$2
2019-10-13 14:10:18 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
λ javap -c MyEnum$1.class
Compiled from "MyEnum.java"
final class org.bupt.pms.consistence.MyEnum$1 extends org.bupt.pms.consistence.MyEnum {
org.bupt.pms.consistence.MyEnum$1(java.lang.String, int);
Code:
0: aload_0
1: aload_1
2: iload_2
3: aconst_null
4: invokespecial #1 // Method org/bupt/pms/consistence/MyEnum."<init>":(Ljava/lang/String;ILorg/bupt/pms/consistence/MyEnum$1;)V
7: return

void doSomething();
Code:
0: return
}
2019-10-13 14:06:31 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
@amiwrong123 不是的,也会生成 MyEnum$1.class 类的。你去本地的 targe 目录里可以看到的,ide 里面可能看不到。
2019-10-13 11:08:28 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
楼主可以试一下代码,如果在枚举常量里添加了方法的话,应该是会生成一个静态内部类继承你的枚举类。这样子的话调用 getclass 没法判断类型是否一致。
https://stackoverflow.com/questions/5758660/java-enum-getdeclaringclass-vs-getclass

https://blog.csdn.net/mhmyqn/article/details/48087247

v2ex 的 markdown 不会用
2019-10-13 11:01:30 +08:00
回复了 amiwrong123 创建的主题 Java [不懂就问] Java .lang.Enum 源码的两个疑问
```
public enum MyEnum {

A ,

B ;

static class SS {

}
public static void main(String[] args) {
System.out.println(MyEnum.A.getDeclaringClass());
System.out.println(MyEnum.A.getClass());
System.out.println(MyEnum.A.getClass().getSuperclass());
SS s = new SS();
System.out.println(s.getClass());
System.out.println(s.getClass().getSuperclass());
}
}
```
```
public enum MyEnum {

A {
void doSomething() { }
},


B {
void doSomethingElse() { }
};

static class SS {

}
public static void main(String[] args) {
System.out.println(MyEnum.A.getDeclaringClass());
System.out.println(MyEnum.A.getClass());
System.out.println(MyEnum.A.getClass().getSuperclass());
SS s = new SS();
System.out.println(s.getClass());
System.out.println(s.getClass().getSuperclass());
}
}

```
2019-05-10 16:40:34 +08:00
回复了 xuanyu66 创建的主题 Java 关于 maven-shade 的一个问题
debug 信息
```
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.4.1:shade (default) on project atomix-shaded: Error creating shaded jar: null: IllegalArgumentException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.4.1:shade (default) on project atomix-shaded: Error creating shaded jar: null
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
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:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at java.lang.reflect.Method.invoke (Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error creating shaded jar: null
at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute (ShadeMojo.java:539)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
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:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at java.lang.reflect.Method.invoke (Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.IllegalArgumentException
at org.objectweb.asm.ClassReader.<init> (Unknown Source)
at org.objectweb.asm.ClassReader.<init> (Unknown Source)
at org.objectweb.asm.ClassReader.<init> (Unknown Source)
at org.apache.maven.plugins.shade.DefaultShader.addRemappedClass (DefaultShader.java:362)
at org.apache.maven.plugins.shade.DefaultShader.shade (DefaultShader.java:145)
at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute (ShadeMojo.java:454)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
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:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at java.lang.reflect.Method.invoke (Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

```
2019-05-10 16:30:38 +08:00
回复了 xuanyu66 创建的主题 Java 关于 maven-shade 的一个问题
求助啊
2019-05-10 16:20:21 +08:00
回复了 xuanyu66 创建的主题 Java 关于 maven-shade 的一个问题
我是按照网上的方法,并且亲自测试有效,但是应用到我的项目上就报错
https://blog.csdn.net/zyc88888/article/details/80620724
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   978 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 15ms · UTC 19:55 · PVG 03:55 · LAX 11:55 · JFK 14:55
Developed with CodeLauncher
♥ Do have faith in what you're doing.