Java-编译器-javac-笔记

原文:http://nullwy.me/2017/05/java...如果觉得我的文章对你有用,请随意赞赏javac 是 Java 代码的编译器 [openjdk, oracle ],初学 Java 的时候就应该接触过。本笔记整理一些 javac 相关的高级用法。 javac 命令行javac 命令行工具,官方文档有完整的使用说明,doc。当然也可以,运行 javac -help 或 man javac 查看帮助信息。下面是经典的 hello world 代码: package com.test.javac;public class Hello { public static void main(String[] args) { System.out.println("hello world"); }}编译与运行 $ tree # 代码目录结构.├── pom.xml└── src └── main ├── java │ └── com │ └── test │ └── javac │ └── Hello.java └── resources$ mkdir -p target/classes # 创建 class 文件的存放目录$ javac src/main/java/com/test/javac/Hello.java -d target/classes$ java -cp "target/classes" com.test.javac.Hello hello world javac 相关 API除了使用命令行工具编译 Java 代码,JDK 6 增加了规范 JSR-199 和 JSR-296,开始还提供相关的 API。Java 编译器的实现代码和 API 的整体结构如图所示[doc]: ...

May 25, 2019 · 4 min · jiezi

Java-运行时获取方法参数名

原文:http://nullwy.me/2017/04/java...如果觉得我的文章对你有用,请随意赞赏本文整理 Java 运行时获取方法参数名的两种方法,Java 8 的最新的方法和 Java 8 之前的方法。 Java 8 的新特性翻阅 Java 8 的新特性,可以看到有这么一条“JEP 118: Access to Parameter Names at Runtime”。这个特性就是为了能运行时获取参数名新加的。这个 JEP 只是功能增强的提案,并没有最终实现的 JDK 相关的 API 的介绍。查看“Enhancements to the Reflection API” 会看到如下介绍: Enhancements in Java SE 8Method Parameter Reflection: You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. However, .class files do not store formal parameter names by default. To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option of the javac compiler.javac 文档中关于 -parameters 的介绍如下 [doc man ]: ...

May 25, 2019 · 6 min · jiezi