Skip to content

Commit c4c80e8

Browse files
committed
fix: @ConfigProperty for java.util.Duration with defaultValue flagged as
error Fixes #1207 Signed-off-by: azerr <[email protected]>
1 parent 869ead4 commit c4c80e8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

projects/lsp4mp/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.ws.rs.Path;
44
import org.eclipse.microprofile.config.inject.ConfigProperty;
55

6+
import java.time.Duration;
7+
68
@Path("/greeting")
79
public class DefaultValueResource {
810

@@ -35,4 +37,30 @@ public class DefaultValueResource {
3537

3638
@ConfigProperty(name = "greeting10", defaultValue="AB")
3739
char greeting10;
40+
41+
@ConfigProperty(name = "greeting11", defaultValue="AB")
42+
char greeting11;
43+
44+
@ConfigProperty(name = "greeting1_1", defaultValue="1")
45+
int greeting1_1;
46+
47+
@ConfigProperty(name = "greeting2_1", defaultValue="1")
48+
Integer greeting2_1;
49+
50+
public static enum Profile {
51+
admin,
52+
user
53+
}
54+
55+
@ConfigProperty(name = "app.duration", defaultValue = "PT15M")
56+
Duration duration;
57+
58+
@ConfigProperty(name = "app.duration", defaultValue = "PT15")
59+
Duration durationWithError;
60+
61+
@ConfigProperty(name = "profile", defaultValue = "user")
62+
Profile profile;
63+
64+
@ConfigProperty(name = "profile", defaultValue = "userXXX")
65+
Profile profileWithError;
3866
}

src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java

+19
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
import org.eclipse.lsp4mp.commons.utils.AntPathMatcher;
3232

3333
import java.text.MessageFormat;
34+
import java.time.Duration;
3435
import java.util.List;
3536
import java.util.logging.Logger;
3637
import java.util.regex.Pattern;
38+
import java.util.stream.Stream;
3739

3840
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants.*;
3941
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils.getAnnotationMemberValueExpression;
@@ -274,7 +276,24 @@ private static boolean isAssignable(String typeFqn, String value, Module javaPro
274276
return PsiTypeUtils.findType(javaProject, value) != null;
275277
case "java.lang.String":
276278
return true;
279+
case "java.time.Duration":
280+
try {
281+
Duration.parse(value);
282+
return true;
283+
}
284+
catch(Exception e) {
285+
return false;
286+
}
277287
default:
288+
PsiClass type = PsiTypeUtils.findType(javaProject, typeFqn);
289+
if (type != null) {
290+
if (type.isEnum()) {
291+
return Stream.of(type.getFields())
292+
.anyMatch(e -> e.getName().equals(value));
293+
294+
295+
}
296+
}
278297
return false;
279298
}
280299
} catch (NumberFormatException e) {

0 commit comments

Comments
 (0)