Skip to content

Commit

Permalink
Merge pull request #925 from KyoriPowered/fix/919
Browse files Browse the repository at this point in the history
fix(key): fix combined namespace+value pattern + extract to own annotations
  • Loading branch information
kashike authored May 15, 2023
2 parents 66ef0e7 + 33c1e57 commit d2dd619
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@
-->

<!-- https://gitlab.com/stellardrift/stylecheck/-/blob/trunk/src/main/java/ca/stellardrift/stylecheck/AnnotationsCloseToType.java -->
<module name="AnnotationsCloseToType"/>
<module name="AnnotationsCloseToType">
<property name="typeUseAnnotations" value="NotNull, Nullable, Pattern, RegExp, KeyPattern, KeyPattern.Namespace, KeyPattern.Value"/>
</module>

<!-- https://checkstyle.org/config_javadoc.html#WriteTag -->
<!-- https://gitlab.com/stellardrift/stylecheck/-/blob/trunk/src/main/java/ca/stellardrift/stylecheck/FilteringWriteTag.java -->
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ truth-java8 = { module = "com.google.truth.extensions:truth-java8-extension", ve
# https://github.com/zml2008/contract-validator
contractValidator = "ca.stellardrift:contract-validator:1.0.1"
errorprone = { module = "com.google.errorprone:error_prone_core", version.ref = "errorprone" }
stylecheck = "ca.stellardrift:stylecheck:0.2.0"
stylecheck = "ca.stellardrift:stylecheck:0.2.1"
autoService-annotations = "com.google.auto.service:auto-service-annotations:1.0.1"
autoService-processor = "com.google.auto.service:auto-service:1.0.1"

Expand Down
11 changes: 5 additions & 6 deletions key/src/main/java/net/kyori/adventure/key/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.stream.Stream;
import net.kyori.examination.Examinable;
import net.kyori.examination.ExaminableProperty;
import org.intellij.lang.annotations.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -85,7 +84,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
* @throws InvalidKeyException if the namespace or value contains an invalid character
* @since 4.0.0
*/
static @NotNull Key key(final @NotNull @Pattern("(" + KeyImpl.NAMESPACE_PATTERN + ":)?" + KeyImpl.VALUE_PATTERN) String string) {
static @NotNull Key key(final @NotNull @KeyPattern String string) {
return key(string, DEFAULT_SEPARATOR);
}

Expand Down Expand Up @@ -121,7 +120,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
* @throws InvalidKeyException if the namespace or value contains an invalid character
* @since 4.4.0
*/
static @NotNull Key key(final @NotNull Namespaced namespaced, final @NotNull @Pattern(KeyImpl.VALUE_PATTERN) String value) {
static @NotNull Key key(final @NotNull Namespaced namespaced, final @NotNull @KeyPattern.Value String value) {
return key(namespaced.namespace(), value);
}

Expand All @@ -134,7 +133,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
* @throws InvalidKeyException if the namespace or value contains an invalid character
* @since 4.0.0
*/
static @NotNull Key key(final @NotNull @Pattern(KeyImpl.NAMESPACE_PATTERN) String namespace, final @NotNull @Pattern(KeyImpl.VALUE_PATTERN) String value) {
static @NotNull Key key(final @NotNull @KeyPattern.Namespace String namespace, final @NotNull @KeyPattern.Value String value) {
return new KeyImpl(namespace, value);
}

Expand Down Expand Up @@ -250,15 +249,15 @@ static boolean allowedInValue(final char character) {
* @since 4.0.0
*/
@Override
@NotNull @Pattern(KeyImpl.NAMESPACE_PATTERN) String namespace();
@NotNull @KeyPattern.Namespace String namespace();

/**
* Gets the value.
*
* @return the value
* @since 4.0.0
*/
@NotNull @Pattern(KeyImpl.VALUE_PATTERN) String value();
@NotNull @KeyPattern.Value String value();

/**
* Returns the string representation of this key.
Expand Down
51 changes: 51 additions & 0 deletions key/src/main/java/net/kyori/adventure/key/KeyPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.key;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.intellij.lang.annotations.Pattern;

@Documented
@Pattern("(?:(" + KeyImpl.NAMESPACE_PATTERN + ":)?|:)" + KeyImpl.VALUE_PATTERN)
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.PARAMETER})
@interface KeyPattern {
@Documented
@Pattern(KeyImpl.NAMESPACE_PATTERN)
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.PARAMETER})
@interface Namespace {
}

@Documented
@Pattern(KeyImpl.VALUE_PATTERN)
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.PARAMETER})
@interface Value {
}
}
3 changes: 1 addition & 2 deletions key/src/main/java/net/kyori/adventure/key/Namespaced.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package net.kyori.adventure.key;

import org.intellij.lang.annotations.Pattern;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -38,5 +37,5 @@ public interface Namespaced {
* @return the namespace
* @since 4.4.0
*/
@NotNull @Pattern(KeyImpl.NAMESPACE_PATTERN) String namespace();
@NotNull @KeyPattern.Namespace String namespace();
}
5 changes: 5 additions & 0 deletions key/src/test/java/net/kyori/adventure/key/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ void testParseableValue() {
void testNulChar() {
assertThrows(InvalidKeyException.class, () -> Key.key("carbon:global\0\0\0"));
}

@Test
void testParseWithEmptyNamespace() {
assertEquals(Key.key(Key.MINECRAFT_NAMESPACE, "test"), Key.key(":test"));
}
}

0 comments on commit d2dd619

Please sign in to comment.