google-java-format
Google Java 스타일을 준수하도록 Java 소스 코드를 다시 포맷하는 프로그램입니다.
포맷터를 다운로드하고 다음을 사용하여 실행하십시오.
java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...]
포맷터는 전체 파일, 제한된 줄( --lines
), 특정 오프셋( --offset
), 표준 출력(기본값) 또는 변경된 내부( --replace
)로 통과하여 작동할 수 있습니다.
특정 패치에서 변경된 줄의 형식을 다시 지정하려면 google-java-format-diff.py
사용하세요.
참고: 포맷터의 포맷 알고리즘은 구성할 수 없습니다. 이는 코드 형식을 단일 형식으로 통합하기 위한 의도적인 디자인 결정입니다.
google-java 형식 IntelliJ 플러그인은 플러그인 저장소에서 사용할 수 있습니다. 설치하려면 IDE 설정으로 이동하여 Plugins
카테고리를 선택하세요. Marketplace
탭을 클릭하고 google-java-format
플러그인을 검색한 후 Install
버튼을 클릭하세요.
플러그인은 기본적으로 비활성화됩니다. 활성화하려면 프로젝트 설정을 연 다음 "google-java-format 설정"을 클릭하고 "google-java-format 활성화" 확인란을 선택하세요.
새 프로젝트에서 기본적으로 활성화하려면 새 프로젝트의 기본 설정을 열고 '기타 설정/google-java-format 설정'에서 구성하세요.
활성화되면 일반적인 Reformat Code
및 Optimize Imports
작업을 대체합니다.
google-java-format 플러그인은 추가 구성 없이는 사용할 수 없는 일부 내부 클래스를 사용합니다. 플러그인을 사용하려면 IDE의 Java 런타임에 몇 가지 옵션을 추가해야 합니다. 그렇게 하려면 Help→Edit Custom VM Options...
으로 이동하여 다음 줄을 붙여넣습니다.
--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
이 작업을 완료한 후 IDE를 다시 시작하세요.
google-java-format
Eclipse 플러그인의 최신 버전은 릴리스 페이지에서 다운로드할 수 있습니다. 플러그인을 활성화하려면 Eclipse 드롭인 폴더에 넣으세요.
플러그인은 Window > Preferences > Java > Code Style > Formatter > Formatter Implementation
에서 구성할 수 있는 google-java-format
포맷터 구현을 추가합니다.
포맷터는 보다 읽기 쉬운 Java 코드를 출력하기 위해 Java를 생성하는 소프트웨어에서 사용할 수 있습니다. maven/gradle/etc에 라이브러리를 포함시키면 됩니다. 구성.
google-java-format
Java 소스를 구문 분석하기 위해 내부 javac API를 사용합니다. JEP 396: 기본적으로 JDK 내부를 강력하게 캡슐화하므로 JDK 16 이상에서 실행하는 경우 다음 JVM 플래그가 필요합니다.
--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
< dependency >
< groupId >com.google.googlejavaformat</ groupId >
< artifactId >google-java-format</ artifactId >
< version >${google-java-format.version}</ version >
</ dependency >
dependencies {
implementation ' com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion '
}
그런 다음 formatSource
메소드를 통해 포맷터를 사용할 수 있습니다. 예:
String formattedSource = new Formatter (). formatSource ( sourceString );
또는
CharSource source = ...
CharSink output = ...
new Formatter (). formatSource ( source , output );
시작점은 com.google.googlejavaformat.java.Formatter
의 인스턴스 메소드여야 합니다.
mvn install
자세한 내용은 기여자 가이드를 참조하세요.
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.