Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5aa6a77

Browse files
module-info 中static 关键字解读
1 parent ba91c1c commit 5aa6a77

File tree

9 files changed

+192
-0
lines changed

9 files changed

+192
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java9-action-static</artifactId>
7+
<groupId>com.dockerx.traffic</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dockerx-traffic-static-entity</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.dockerx.traffic.optional.anno;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.TYPE;
8+
9+
/**
10+
* @author Author 知秋
11+
* @email fei6751803@163.com
12+
* @time Created by Auser on 2017年11月21日 13:30.
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target(value={TYPE})
16+
public @interface Anno {
17+
boolean value() default true;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.dockerx.traffic.optional.entity;
2+
3+
import com.dockerx.traffic.optional.anno.Anno;
4+
5+
/**
6+
* @author Author 知秋
7+
* @email fei6751803@163.com
8+
* @time Created by Auser on 2017年11月20日 20:11.
9+
*/
10+
@Anno
11+
public class Person {
12+
private int mileage;
13+
private int money;
14+
15+
public Person() {
16+
}
17+
18+
public Person(int mileage, int money) {
19+
this.mileage = mileage;
20+
this.money = money;
21+
}
22+
23+
public int getMileage() {
24+
return mileage;
25+
}
26+
27+
public Person setMileage(int mileage) {
28+
this.mileage = mileage;
29+
return this;
30+
}
31+
32+
public int getMoney() {
33+
return money;
34+
}
35+
36+
public Person setMoney(int money) {
37+
this.money = money;
38+
return this;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @author Author 知秋
3+
* @email fei6751803@163.com
4+
* @time Created by Auser on 2017年11月21日 13:29.
5+
*/
6+
module dockerx.traffic.optional.entity {
7+
exports com.dockerx.traffic.optional.entity;
8+
exports com.dockerx.traffic.optional.anno;
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java9-action-static</artifactId>
7+
<groupId>com.dockerx.traffic</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dockerx-traffic-static-gui</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.dockerx.traffic</groupId>
16+
<artifactId>dockerx-traffic-static-entity</artifactId>
17+
<version>1.0-SNAPSHOT</version>
18+
</dependency>
19+
</dependencies>
20+
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.dockerx.traffic.optional.gui;
2+
3+
import com.dockerx.traffic.optional.anno.Anno;
4+
5+
/**
6+
* @author Author 知秋
7+
* @email fei6751803@163.com
8+
* @time Created by Auser on 2017年11月21日 14:31.
9+
*/
10+
@Anno
11+
public class Bike {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.dockerx.traffic.optional.gui;
2+
3+
import com.dockerx.traffic.optional.entity.Person;
4+
5+
/**
6+
* @author Author 知秋
7+
* @email fei6751803@163.com
8+
* @time Created by Auser on 2017年11月21日 13:37.
9+
*/
10+
public class Gui {
11+
public static void main(String[] args) {
12+
//module-info.java 文件中使用static修饰后,会在编译阶段直接忽略这个类,不会加载到文件系统中,
13+
// 不过想要使用可以通过反射获取,我们有requires的
14+
// 下面注释掉的两条语句都会报ClassNotFoundException
15+
/*Person person = new Person(100, 1);
16+
System.out.println(Person.class.getAnnotations().length);*/
17+
18+
System.out.println(Bike.class.getAnnotations().length);
19+
System.out.println("Running without annotation @Anno present.");
20+
21+
try {
22+
Class<?> clazz = Class.forName("com.dockerx.traffic.optional.entity.Person");
23+
Person instance =
24+
(Person) clazz.getConstructor().newInstance();
25+
instance.setMileage(10);
26+
//同样,添加下面注释这句,就会报下面catch到的错了
27+
// System.out.println(Person.class.getAnnotations().length);
28+
System.out.println("Using Person");
29+
} catch (ReflectiveOperationException e) {
30+
System.out.println("Oops, we need a fallback!");
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @author Author 知秋
3+
* @email fei6751803@163.com
4+
* @time Created by Auser on 2017年11月21日 13:37.
5+
*/
6+
module dockerx.traffic.optional.gui {
7+
requires static dockerx.traffic.optional.entity;
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.dockerx.traffic</groupId>
8+
<artifactId>java9-action-static</artifactId>
9+
<packaging>pom</packaging>
10+
<version>1.0-SNAPSHOT</version>
11+
<modules>
12+
<module>dockerx-traffic-optional-entity</module>
13+
<module>dockerx-traffic-optional-gui</module>
14+
</modules>
15+
16+
<properties>
17+
<java.version>1.9</java.version>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.6.1</version>
26+
<configuration>
27+
<source>${java.version}</source>
28+
<target>${java.version}</target>
29+
<showDeprecation>true</showDeprecation>
30+
<showWarnings>true</showWarnings>
31+
</configuration>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /