Files
snippets/java.code-snippets
2025-08-04 09:29:27 +08:00

488 lines
16 KiB
Plaintext

{
"Import Java Utils": {
"scope": "java",
"prefix": "import_java_util",
"body": [
"import java.util.${1|*,Objects,Collection,List,ArrayList,LinkedList,Map,HashMap,ConcurrentHashMap,Set,HashSet,Collections,Arrays|};",
],
"description": "Import classes in package java.util."
},
"Import Java Time": {
"scope": "java",
"prefix": "import_java_time",
"body": [
"import java.time.${1|*,LocalDateTime,LocalDate,LocalTime,ZoneDateTime,ZoneId,ZoneOffset,ZoneRegion,Year,Month,MonthDay,DayOfWeek,YearMonth|};",
],
"description": "Import classes in package java.time."
},
"const": {
"scope": "java",
"prefix": "const",
"body": "${1|private ,protected ,public |}static final ${2:String} ${3:CONST_NAME} = $4;"
},
"iterator": {
"scope": "java",
"prefix": "iterator",
"body": [
"final Iterator<${2|Integer,Long,String|}> ${3:iterator} = $1.iterator();",
"while (${3:iterator}.hasNext()) {",
"\tfinal ${2|Integer,Long,String|} ${4:item} = ${3:iterator}.next();",
"\t$5",
"}",
]
},
"Throw exception": {
"scope": "java",
"prefix": "thr",
"body": "throw $0;"
},
"Throw new exception": {
"scope": "java",
"prefix": ["thrownew", "thn"],
"body": "throw new ${1|IllegalArgumentException,IllegalStateException,NullPointerException,UnsupportedOperationException,ArithmeticException,ClassCastException,ArrayIndexOutOfBoundsException,NumberFormatException,RuntimeException,EnumConstantNotPresentException|}($2);",
"description": "Throw new exception"
},
"Return result": {
"scope": "java",
"prefix": "ret",
"body": [
"return ${1|this,result|};",
],
"description": "Return result"
},
"Return success": {
"scope": "java",
"prefix": "success",
"body": [
"return UnifiedResponses.success($1);",
],
"description": "return UnifiedResponses#success"
},
"Charset": {
"scope": "java",
"prefix": "charset",
"body": "StandardCharsets.${1|UTF_8,UTF_16,UTF_16BE,UTF_16LE,US_ASCII,ISO_8859_1|}"
},
"GetById": {
"scope": "java",
"prefix": "getById",
"body": [
"@GetMapping(\"{${2:id}}\")",
"public UnifiedResponse ${1:findById}(@PathVariable(\"${2:id}\") Long ${2:id}) {",
"\tvar result = $4;",
"\treturn UnifiedResponses.success(${3:\"查询成功\"}, result);",
"}",
]
},
"Constructor": {
"scope": "java",
"prefix": "constructor",
"body": [
"${1|public ,protected ,private |}${2:$TM_FILENAME_BASE}($3) {$4",
"}",
]
},
"Constructor for utility class": {
"scope": "java",
"prefix": "constructor-for-utility-class",
"body": [
"$2",
"",
"private ${1:$TM_FILENAME_BASE}() {",
"\tthrow new IllegalStateException(\"Utility class\");",
"}",
]
},
"Field": {
"scope": "java",
"prefix": "field",
"body": "${1|private ,protected ,public |}${4|@Getter ,@Setter ,@Getter @Setter |}${2:String} ${3:username};"
},
"Internal method": {
"scope": "java",
"prefix": "internal_method",
"body": [
"/*$5 */",
"${1|private ,protected |}${2:void} ${3:method}Internal($4) {",
"\t",
"}"
]
},
"Static internal method": {
"scope": "java",
"prefix": "static_internal_method",
"body": [
"/*$5 */",
"${1|private ,protected |}static ${2:void} ${3:method}Internal($4) {",
"\t",
"}"
]
},
"TODO": {
"scope": "java,javascript,typescript",
"prefix": ["TODO", "todo"],
"body": "// TODO [${1|修复,添加,优化,重构|}] ${2:msg}",
"description": "待办事项"
},
"FIXME": {
"scope": "java,javascript,typescript",
"prefix": ["FIXME", "fixme"],
"body": "// FIXME $1",
"description": "修复"
},
"XXX": {
"scope": "java,javascript,typescript",
"prefix": ["XXX", "xxx"],
"body": "// XXX $1",
"description": "待优化"
},
"NOSONAR": {
"scope": "java,javascript,typescript",
"prefix": [
"nosonar",
"sonarIgnore"
],
"body": "// NOSONAR$1"
},
"Slf4jLog": {
"scope": "java",
"prefix": "log-slf4j",
"body": "private static final Logger log = LoggerFactory.getLogger(${1:$TM_FILENAME_BASE}.class);"
},
"log a var": {
"scope": "java",
"prefix": "log-var",
"body": "log.${1|info,debug,error|}(\"${2:var}: {}\", ${2:var});"
},
"log a obj": {
"scope": "java",
"prefix": "log-obj",
"body": "log.${1|info,debug,error|}(\"{}\", ${2:var});"
},
"List": {
"scope": "java",
"prefix": "list",
"body": "List<${1:T}> ${2:list} = new ${3|ArrayList,LinkedList|}<>($4);"
},
"HashMap": {
"scope": "java",
"prefix": "map",
"body": "Map<${1:K}, ${2:V}> ${3:map} = new ${4|HashMap,ConcurrentHashMap|}<>($5);"
},
"collect": {
"scope": "java",
"prefix": "collect",
"body": ".collect(Collectors.${1|toSet(),toList()|})"
},
"toArray": {
"scope": "java",
"prefix": "toArray",
"body": ".toArray(${1|String,Integer,Long,SqlParameterSource|}[]::new)"
},
"SuppressWarnings": {
"scope": "java",
"prefix": "suppressWarnings",
"body": "@SuppressWarnings(\"${1|all,boxing,cast,dep-ann,deprecation,fallthrough,finally,hiding,incomplete-switch,nls,null,rawtypes,restriction,serial,static-access,synthetic-access,unchecked,unqualified-field-access,unused|}\")",
},
"TimeMillis' now": {
"scope": "java",
"prefix": "now",
"body": "${2|long,var|} ${1:now} = System.currentTimeMillis();"
},
"Date's now": {
"scope": "java",
"prefix": "now",
"body": "${2|Date,var|} ${1:now} = new Date();"
},
"LocalDateTime's now": {
"scope": "java",
"prefix": "now",
"body": "${2|LocalDateTime,var|} ${1:now} = LocalDateTime.now();"
},
"ZonedDateTime's now": {
"scope": "java",
"prefix": "now",
"body": "${2|ZonedDateTime,var|} ${1:now} = ZonedDateTime.now();"
},
"Instant's now": {
"scope": "java",
"prefix": "now",
"body": "${2|Instant,var|} ${1:now} = Instant.now();"
},
"today": {
"scope": "java",
"prefix": "today",
"body": "${2|LocalDate,var|} ${1|now,today|} = LocalDate.now();"
},
"Get Optional": {
"scope": "java",
"prefix": "optional_getter",
"body": [
"public Optional<${1:Email}> get${2:Email}() {",
"\treturn Optional.ofNullable(${3:email});",
"}",
]
},
"lombok-pojo": {
"scope": "java",
"prefix": "lombok-pojo",
"body": [
"@Getter",
"@Setter",
"@NoArgsConstructor",
"@ToString${1:(callSuper = true)}",
]
},
"Copyright Apache License 2.0": {
"scope": "java",
"prefix": "copyright-apache-license-2",
"body": [
"/*",
" * Copyright ${1:$CURRENT_YEAR} ${2:the original author or authors}.",
" *",
" * 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",
" *",
" * https://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.",
" */\n",
]
},
"Jdbc Template": {
"scope": "java",
"prefix": "jdbc",
"body": "private final ${1|NamedParameterJdbcTemplate,JdbcTemplate|} ${2:jdbcTemplate};"
},
"JUnit4 test class": {
"scope": "java",
"prefix": "test-class-junit4",
"isFileTemplate": true,
"body": [
"import org.junit.Test;",
"",
"import static org.junit.Assert.*;",
"",
"public class ${1:$TM_FILENAME_BASE} {",
"",
"\t@Test",
"\tpublic void test$2() {",
"\t\t$3",
"\t}",
"}",
"",
]
},
"JUnit4 test case": {
"scope": "java",
"prefix": "test-case-junit4",
"body": [
"@Test",
"public void test$1() {",
"\t$2",
"}"
]
},
"JUnit5 test class": {
"scope": "java",
"isFileTemplate": true,
"prefix": "test-class-junit5",
"body": [
"import static org.junit.jupiter.api.Assertions.*;",
"",
"import org.junit.jupiter.api.Test;",
"",
"public class ${1:$TM_FILENAME_BASE} {",
"",
"\t@Test",
"\tvoid test$2() {",
"\t\t$3",
"\t}",
"}",
""
]
},
"JUnit5 test case": {
"scope": "java",
"prefix": "test-case-junit5",
"body": [
"@Test",
"void test$1() {",
"\t$2",
"}"
]
},
"ParameterizedTest": {
"prefix": "parameterized-test",
"body": [
"@ParameterizedTest",
"@ValueSource(${2|strings,ints,shorts,bytes,longs,floats,doubles,chars,booleans,classes|} = { $3 })",
"void test$1(${4|String,int,short,byte,long,float,double,char,boolean,Class<?>|} $5) {",
"\t$6",
"}"
],
"description": "ParameterizedTest"
},
"SerializableUID": {
"scope": "java",
"prefix": "serializableUID",
"body": [
"private static final long serialVersionUID = ${1:${RANDOM/(0)//}$CURRENT_DATE$CURRENT_MONTH$CURRENT_YEAR_SHORT$CURRENT_SECOND$CURRENT_MINUTE$CURRENT_HOUR}L;"
]
},
"Exception": {
"scope": "java",
"prefix": "exception",
"body": [
"class ${1:$TM_FILENAME_BASE} extends ${2|RuntimeException,Exception|} {",
"\tprivate static final long serialVersionUID = ${3:${RANDOM/(0)//}$CURRENT_DATE$CURRENT_MONTH$CURRENT_YEAR_SHORT$CURRENT_SECOND$CURRENT_MINUTE$CURRENT_HOUR}L;",
"",
"\t/**",
"\t * Constructs a new {@code ${1:$TM_FILENAME_BASE}} with null as its detail message.",
"\t * The cause is not initialized, and may subsequently be initialized by a call to initCause.",
"\t */",
"\t${4|public,protected|} ${1:$TM_FILENAME_BASE}() {",
"\t\tsuper();",
"\t}",
"",
"\t/**",
"\t * Constructs a new {@code ${1:$TM_FILENAME_BASE}} with the specified detail message.",
"\t * The cause is not initialized, and may subsequently be initialized by a",
"\t * call to {@link #initCause}.",
"\t *",
"\t * @param message the detail message. The detail message is saved for",
"\t * later retrieval by the {@link #getMessage()} method.",
"\t */",
"\t${4|public,protected|} ${1:$TM_FILENAME_BASE}(String message) {",
"\t\tsuper(message);",
"\t}",
"",
"\t/**",
"\t * Constructs a new {@code ${1:$TM_FILENAME_BASE}} with the specified cause and a",
"\t * detail message of {@code (cause==null ? null : cause.toString())}",
"\t * (which typically contains the class and detail message of {@code cause}).",
"\t * This constructor is useful for exceptions",
"\t * that are little more than wrappers for other throwables.",
"\t *",
"\t * @param cause the cause (which is saved for later retrieval by the",
"\t * {@link #getCause()} method). (A {@code null} value is",
"\t * permitted, and indicates that the cause is nonexistent or",
"\t * unknown.)",
"\t */",
"\t${4|public,protected|} ${1:$TM_FILENAME_BASE}(Throwable cause) {",
"\t\tsuper(cause);",
"\t}",
"",
"\t/**",
"\t * Constructs a new {@code ${1:$TM_FILENAME_BASE}} with the specified detail message and",
"\t * cause. <p>Note that the detail message associated with",
"\t * {@code cause} is <i>not</i> automatically incorporated in",
"\t * this exception's detail message.",
"\t *",
"\t * @param message the detail message (which is saved for later retrieval",
"\t * by the {@link #getMessage()} method).",
"\t * @param cause the cause (which is saved for later retrieval by the",
"\t * {@link #getCause()} method). (A {@code null} value is",
"\t * permitted, and indicates that the cause is nonexistent or",
"\t * unknown.)",
"\t */",
"\t${4|public,protected|} ${1:$TM_FILENAME_BASE}(String message, Throwable cause) {",
"\t\tsuper(message, cause);",
"\t}",
"}",
],
},
"#region": {
"scope": "java",
"prefix": "#regoin",
"body": [
"// ================================",
"// #region - $1",
"// ================================",
"",
"$TM_SELECTED_TEXT",
"",
"// ================================",
"// #endregion - $1",
"// ================================",
]
},
"=if": {
"scope": "java",
"prefix": "=if",
"body": [
"if ($1) {",
"\t${2:var} = ${3:value};",
"}",
]
},
"String line": {
"scope": "java",
"prefix": "line",
"body": "+ \"\\n\" + \"$1\""
},
"Static factory method": {
"scope": "java",
"prefix": ["staticFactoryMethod", "of"],
"body": [
"@StaticFactoryMethod(${1:$TM_FILENAME_BASE}.class)",
"public static ${1:$TM_FILENAME_BASE} of($2) {",
"\treturn new ${1:$TM_FILENAME_BASE}($3);",
"}",
]
},
"Static factory method with type param": {
"scope": "java",
"prefix": ["staticFactoryMethodWithTypeParam", "of_T"],
"body": [
"@StaticFactoryMethod(${1:$TM_FILENAME_BASE}.class)",
"public static <${2:T}> ${1:$TM_FILENAME_BASE}<${2:T}> of($3) {",
"\treturn new ${1:$TM_FILENAME_BASE}<>($4);",
"}",
]
},
// assert
"Import AssertTools": {
"scope": "java",
"prefix": "import_assert_tools",
"body": [
"",
"import static xyz.zhouxy.plusone.commons.util.AssertTools.*;",
"",
]
},
"AssertTools.checkCondition": {
"scope": "java",
"prefix": "assert_checkCondition",
"body": [
"AssertTools.checkCondition($3, () -> new ${1|IllegalArgumentException,IllegalStateException,NullPointerException,UnsupportedOperationException,ArithmeticException,ClassCastException,ArrayIndexOutOfBoundsException,NumberFormatException,RuntimeException,EnumConstantNotPresentException|}($2));"
]
},
"AssertTools.checkArgument": {
"scope": "java",
"prefix": "assert_checkArgument",
"body": [
"AssertTools.checkArgument(${1|StringTools.isNotBlank,Objects.nonNull|}($2)${3:, \"$4\"});"
]
},
"Lock": {
"scope": "java",
"prefix": "lock",
"body": [
"${1:lock}.lock();",
"try {",
"\t// access the resource protected by this lock",
"\t$2",
"}",
"finally {",
"\t${1:lock}.unlock();",
"}",
]
}
}