# google-java-format
`google-java-format` is a program that reformats Java source code to comply with
[Google Java Style][].
[Google Java Style]: https://google.github.io/styleguide/javaguide.html
## Using the formatter
### from the command-line
[Download the formatter](https://github.com/google/google-java-format/releases)
and run it with:
```
java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...]
```
The formatter can act on whole files, on limited lines (`--lines`), on specific
offsets (`--offset`), passing through to standard-out (default) or altered
in-place (`--replace`).
To reformat changed lines in a specific patch, use
[`google-java-format-diff.py`](https://github.com/google/google-java-format/blob/master/scripts/google-java-format-diff.py).
***Note:*** *There is no configurability as to the formatter's algorithm for
formatting. This is a deliberate design decision to unify our code formatting on
a single format.*
### IntelliJ, Android Studio, and other JetBrains IDEs
A
[google-java-format IntelliJ plugin](https://plugins.jetbrains.com/plugin/8527)
is available from the plugin repository. To install it, go to your IDE's
settings and select the `Plugins` category. Click the `Marketplace` tab, search
for the `google-java-format` plugin, and click the `Install` button.
The plugin will be disabled by default. To enable it in the current project, go
to `File→Settings...→google-java-format Settings` (or `IntelliJ
IDEA→Preferences...→Other Settings→google-java-format Settings` on macOS) and
check the `Enable google-java-format` checkbox. (A notification will be
presented when you first open a project offering to do this for you.)
To enable it by default in new projects, use `File→Other Settings→Default
Settings...`.
When enabled, it will replace the normal `Reformat Code` and `Optimize Imports`
actions.
#### IntelliJ JRE Config
The google-java-format plugin uses some internal classes that aren't available
without extra configuration. To use the plugin, go to `Help→Edit Custom VM
Options...` and paste in these lines:
```
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
```
Once you've done that, restart the IDE.
### Eclipse
The latest version of the `google-java-format` Eclipse plugin can be downloaded
from the [releases page](https://github.com/google/google-java-format/releases).
Drop it into the Eclipse
[drop-ins folder](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fp2_dropins_format.html)
to activate the plugin.
The plugin adds a `google-java-format` formatter implementation that can be
configured in `Window > Preferences > Java > Code Style > Formatter > Formatter
Implementation`.
### Third-party integrations
* Gradle plugins
* [spotless](https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format)
* [sherter/google-java-format-gradle-plugin](https://github.com/sherter/google-java-format-gradle-plugin)
* Apache Maven plugins
* [spotless](https://github.com/diffplug/spotless/tree/main/plugin-maven#google-java-format)
* [spotify/fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin)
* [talios/googleformatter-maven-plugin](https://github.com/talios/googleformatter-maven-plugin)
* [Cosium/maven-git-code-format](https://github.com/Cosium/maven-git-code-format):
A maven plugin that automatically deploys google-java-format as a
pre-commit git hook.
* SBT plugins
* [sbt/sbt-java-formatter](https://github.com/sbt/sbt-java-formatter)
* [Github Actions](https://github.com/features/actions)
* [googlejavaformat-action](https://github.com/axel-op/googlejavaformat-action):
Automatically format your Java files when you push on github
### as a library
The formatter can be used in software which generates java to output more
legible java code. Just include the library in your maven/gradle/etc.
configuration.
`google-java-format` uses internal javac APIs for parsing Java source. The
following JVM flags are required when running on JDK 16 and newer, due to
[JEP 396: Strongly Encapsulate JDK Internals by Default](https://openjdk.java.net/jeps/396):
```
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
```
#### Maven
```xml
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>${google-java-format.version}</version>
</dependency>
```
#### Gradle
```groovy
dependencies {
implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion'
}
```
You can then use the formatter through the `formatSource` methods. E.g.
```java
String formattedSource = new Formatter().formatSource(sourceString);
```
or
```java
CharSource source = ...
CharSink output = ...
new Formatter().formatSource(source, output);
```
Your starting point should be the instance methods of
`com.google.googlejavaformat.java.Formatter`.
## Building from source
```
mvn install
```
## Contributing
Please see [the contributors guide](CONTRIBUTING.md) for details.
## License
```text
Copyright 2015 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
```
没有合适的资源?快使用搜索试试~ 我知道了~
谷歌java格式-重新格式化 Java 源代码以符合 Google Java 风格
共488个文件
input:191个
output:190个
java:78个
需积分: 5 1 下载量 156 浏览量
2023-10-25
18:32:51
上传
评论
收藏 431KB ZIP 举报
温馨提示
谷歌java格式 google-java-format是一个重新格式化 Java 源代码以符合 Google Java Style的程序。 使用格式化程序 从命令行 下载格式化程序 并运行它: java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...] 格式化程序可以作用于整个文件、有限行 ( --lines)、特定偏移量 ( --offset)、传递到标准输出(默认)或就地更改 ( --replace)。 要重新格式化特定补丁中更改的行,请使用 google-java-format-diff.py. 注意: 格式化程序的格式化算法不可配置。这是一个经过深思熟虑的设计决定,旨在将我们的代码格式统一为单一格式。
资源推荐
资源详情
资源评论
收起资源包目录
谷歌java格式-重新格式化 Java 源代码以符合 Google Java 风格 (488个子文件)
jvm.config 644B
google-java-format.el 4KB
GoogleJavaFormatConfigurable.form 2KB
A.formatting-and-import-sorting 598B
A.formatting-and-unused-import-removal 579B
A.formatting-only 579B
.gitignore 120B
.gitignore 40B
A.imports-and-formatting 576B
A.imports-only 579B
B24909927.input 21KB
M.input 15KB
B20701054.input 8KB
B20128760.input 4KB
A.input 3KB
B20128588.input 3KB
Unformatted.input 3KB
E.input 3KB
F.input 3KB
B20341001.input 2KB
LiteralReflow.input 2KB
B26207047.input 2KB
B20535125.input 2KB
B28750242.input 2KB
B21283374.input 2KB
C.input 2KB
PairedArguments.input 2KB
B21608216.input 2KB
B24543625.input 2KB
B26884608.input 1KB
I.input 1KB
B23749160.input 1KB
B21954779.input 1KB
B21305044.input 1KB
B23804934.input 1KB
U.input 1KB
B22815364.input 1KB
I1.input 1KB
B25787055.input 1KB
S.input 1KB
B24202287.input 1KB
B24702438.input 1KB
B25372815.input 1KB
B26293162.input 998B
ExpressionSwitch.input 885B
B26952926.input 859B
B20844369.input 789B
B30223986.input 786B
D.input 759B
B28066276.input 757B
B20569245.input 756B
TypeAnnotations.input 754B
B22166687.input 692B
B29368546.input 683B
B18479811.input 666B
ParameterComment.input 651B
M1.input 651B
B21192435.input 624B
TryWithResources.input 612B
B35644813.input 610B
A.input 604B
B26694550.input 589B
B37629610.input 574B
I603.input 571B
B20529113.input 545B
N.input 535B
Unformatted2.input 534B
B64493529.input 520B
P.input 519B
B21278211.input 517B
SuperQualifier.input 515B
B22873322.input 511B
T.input 500B
B20567842.input 485B
B65214682.input 482B
B20580212.input 479B
B23349153.input 469B
B21331232.input 459B
I643.input 432B
Records.input 429B
Multivariables.input 424B
NormalAnnotation.input 422B
B21465217.input 413B
B20578077.input 387B
B20531711.input 377B
B23708487.input 376B
B19950815.input 371B
B27078833.input 370B
B142553964.input 367B
B112853497.input 347B
I477.input 341B
I683.input 340B
B37895033.input 339B
B.input 333B
I684.input 326B
B33358723.input 326B
Q.input 304B
B24988078.input 302B
B21465477.input 296B
B22488373.input 295B
共 488 条
- 1
- 2
- 3
- 4
- 5
资源评论
Web面试那些事儿
- 粉丝: 5776
- 资源: 101
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言-leetcode题解之70-climbing-stairs.c
- C语言-leetcode题解之68-text-justification.c
- C语言-leetcode题解之66-plus-one.c
- C语言-leetcode题解之64-minimum-path-sum.c
- C语言-leetcode题解之63-unique-paths-ii.c
- C语言-leetcode题解之62-unique-paths.c
- C语言-leetcode题解之61-rotate-list.c
- C语言-leetcode题解之59-spiral-matrix-ii.c
- C语言-leetcode题解之58-length-of-last-word.c
- 计算机编程课程设计基础教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功