You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/10/03 19:28:02 UTC
[isis] branch v2 updated: ISIS-2158: introduces _Annotations
(internal API)
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/v2 by this push:
new bdb0f5c ISIS-2158: introduces _Annotations (internal API)
bdb0f5c is described below
commit bdb0f5cca4689b76ee4dade5f9455080e66db67e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Oct 3 21:27:52 2019 +0200
ISIS-2158: introduces _Annotations (internal API)
- utilizing Spring's brand new MergedAnnotations API
---
.../commons/internal/reflection/_Annotations.java | 78 ++++++++++++++++++++
.../Annotations_getAnnotations_on_Class_Test.java | 82 ++++++++++------------
2 files changed, 114 insertions(+), 46 deletions(-)
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Annotations.java b/core/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Annotations.java
new file mode 100644
index 0000000..1c6bb27
--- /dev/null
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Annotations.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.isis.commons.internal.reflection;
+
+import java.lang.annotation.Annotation;
+import java.util.Optional;
+
+import org.springframework.core.annotation.MergedAnnotation;
+import org.springframework.core.annotation.MergedAnnotations;
+import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import lombok.val;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ * </p>
+ * @since 2.0
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class _Annotations {
+
+ /**
+ * Optionally create a type-safe synthesized version of this annotation based on presence.
+ * @param <A>
+ * @param annotatedElement
+ * @param annotationType
+ * @return non-null
+ */
+ public static <A extends Annotation> Optional<A> synthesize(
+ Class<?> annotatedElement,
+ Class<A> annotationType) {
+
+ val synthesized = _Annotations
+ .merge(annotatedElement, annotationType)
+ .synthesize(ma->ma.isPresent());
+
+ return synthesized;
+ }
+
+ // -- HELPER
+
+ /**
+ * @apiNote don't expose Spring's MergedAnnotation
+ */
+ private static <A extends Annotation> MergedAnnotation<A> merge(
+ Class<?> annotatedElement,
+ Class<A> annotationType) {
+
+ //TODO use cache
+ val merged = MergedAnnotations.from(annotatedElement, SearchStrategy.SUPERCLASS);
+
+ return merged.get(annotationType);
+ }
+
+
+
+}
diff --git a/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/Annotations_getAnnotations_on_Class_Test.java b/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/Annotations_getAnnotations_on_Class_Test.java
index 1333304..1987051 100644
--- a/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/Annotations_getAnnotations_on_Class_Test.java
+++ b/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/Annotations_getAnnotations_on_Class_Test.java
@@ -23,13 +23,16 @@ import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
-import java.util.List;
import org.junit.Assert;
import org.junit.Test;
+import org.apache.isis.commons.internal.reflection._Annotations;
+
import static org.hamcrest.CoreMatchers.is;
+import lombok.val;
+
public class Annotations_getAnnotations_on_Class_Test {
@@ -76,12 +79,11 @@ public class Annotations_getAnnotations_on_Class_Test {
@DomainObj(publishng = DomainObj.Publishng.YES)
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
-
- Assert.assertThat(annotations.size(), is(1));
-
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.YES));
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
+
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.YES));
}
@Test
@@ -90,12 +92,11 @@ public class Annotations_getAnnotations_on_Class_Test {
@Published
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
-
- Assert.assertThat(annotations.size(), is(1));
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.YES));
}
@Test
@@ -104,12 +105,11 @@ public class Annotations_getAnnotations_on_Class_Test {
@MetaPublished
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
-
- Assert.assertThat(annotations.size(), is(1));
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.YES));
}
@Test
@@ -119,29 +119,25 @@ public class Annotations_getAnnotations_on_Class_Test {
@Published
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.size(), is(2));
-
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.YES));
- Assert.assertThat(annotations.get(1).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.YES));
}
@Test
public void meta_overrides_metaMeta() throws Exception {
@MetaPublished
- @NotPublished
+ @NotPublished // <-- should win over the other
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
-
- Assert.assertThat(annotations.size(), is(2));
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.NO));
- Assert.assertThat(annotations.get(1).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.NO));
}
@Test
@@ -149,17 +145,14 @@ public class Annotations_getAnnotations_on_Class_Test {
@MetaPublished
@Published
- @DomainObj(publishng = DomainObj.Publishng.NO)
+ @DomainObj(publishng = DomainObj.Publishng.NO) // <-- should win over the others
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.size(), is(3));
-
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.NO));
- Assert.assertThat(annotations.get(1).publishng(), is(DomainObj.Publishng.YES));
- Assert.assertThat(annotations.get(2).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.NO));
}
@@ -168,17 +161,14 @@ public class Annotations_getAnnotations_on_Class_Test {
@MetaPublished
@NotPublished
- @DomainObj(publishng = DomainObj.Publishng.YES)
+ @DomainObj(publishng = DomainObj.Publishng.YES) // <-- should win over the others
class SomeDomainObject {}
- final List<DomainObj> annotations = Annotations
- .getAnnotations(SomeDomainObject.class, DomainObj.class);
-
- Assert.assertThat(annotations.size(), is(3));
+ val synthesized = _Annotations
+ .synthesize(SomeDomainObject.class, DomainObj.class);
- Assert.assertThat(annotations.get(0).publishng(), is(DomainObj.Publishng.YES));
- Assert.assertThat(annotations.get(1).publishng(), is(DomainObj.Publishng.NO));
- Assert.assertThat(annotations.get(2).publishng(), is(DomainObj.Publishng.YES));
+ Assert.assertThat(synthesized.isPresent(), is(true));
+ Assert.assertThat(synthesized.get().publishng(), is(DomainObj.Publishng.YES));
}
}
\ No newline at end of file