You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2019/05/16 15:06:57 UTC

[tomee] branch master updated: Eliminate duplicate method generation in Validation

This is an automated email from the ASF dual-hosted git repository.

dblevins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/master by this push:
     new 16ca728  Eliminate duplicate method generation in Validation
16ca728 is described below

commit 16ca7284c492fa2c7995210cf8f4ddaf43443826
Author: David Blevins <da...@gmail.com>
AuthorDate: Fri May 17 00:04:00 2019 +0900

    Eliminate duplicate method generation in Validation
---
 .../jwt/bval/GeneratedConstraintsMissingException.java  | 12 ++++++------
 .../microprofile/jwt/bval/ValidationConstraints.java    |  5 ++---
 .../microprofile/jwt/bval/ValidationGenerator.java      | 15 +++++++++------
 .../apache/tomee/microprofile/jwt/bval/Asmifier.java    |  2 +-
 .../microprofile/jwt/bval/ValidationGeneratorTest.java  | 17 +++++++++++++----
 .../jwt/bval/data/Colors$$JwtConstraints.java           |  2 ++
 .../apache/tomee/microprofile/jwt/bval/data/Colors.java |  3 +++
 7 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/GeneratedConstraintsMissingException.java b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/GeneratedConstraintsMissingException.java
index 9dc50b8..a1ddf83 100644
--- a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/GeneratedConstraintsMissingException.java
+++ b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/GeneratedConstraintsMissingException.java
@@ -17,25 +17,25 @@
 package org.apache.tomee.microprofile.jwt.bval;
 
 import java.lang.reflect.Method;
-import java.util.List;
+import java.util.Collection;
 
 public class GeneratedConstraintsMissingException extends IllegalStateException {
 
-    private final List<Method> original;
-    private final List<Method> generated;
+    private final Collection<Method> original;
+    private final Collection<Method> generated;
 
-    public GeneratedConstraintsMissingException(final List<Method> original, final List<Method> generated) {
+    public GeneratedConstraintsMissingException(final Collection<Method> original, final Collection<Method> generated) {
         super(String.format("Expected %s constrained methods, found %s", original.size(), generated.size()));
 
         this.original = original;
         this.generated = generated;
     }
 
-    public List<Method> getOriginal() {
+    public Collection<Method> getOriginal() {
         return original;
     }
 
-    public List<Method> getGenerated() {
+    public Collection<Method> getGenerated() {
         return generated;
     }
 }
diff --git a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationConstraints.java b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationConstraints.java
index 82c024d..e866d2b 100644
--- a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationConstraints.java
+++ b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationConstraints.java
@@ -27,7 +27,6 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -54,8 +53,8 @@ public class ValidationConstraints {
 
         if (constraintsClazz == null) return null;
 
-        final List<Method> original = ValidationGenerator.getConstrainedMethods(componentClass);
-        final List<Method> generated = ValidationGenerator.getConstrainedMethods(constraintsClazz);
+        final Set<Method> original = ValidationGenerator.getConstrainedMethods(componentClass);
+        final Set<Method> generated = ValidationGenerator.getConstrainedMethods(constraintsClazz);
 
         if (original.size() != generated.size()) {
             throw new GeneratedConstraintsMissingException(original, generated);
diff --git a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationGenerator.java b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationGenerator.java
index 42a23d9..ae32039 100644
--- a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationGenerator.java
+++ b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/bval/ValidationGenerator.java
@@ -28,9 +28,11 @@ import javax.validation.Constraint;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * We allow CDI and EJB beans to use BeanValidation to validate a JsonWebToken
@@ -93,7 +95,7 @@ import java.util.Map;
 public class ValidationGenerator implements Opcodes {
 
     public static byte[] generateFor(final Class<?> target) throws ProxyGenerationException {
-        final List<Method> constrainedMethods = getConstrainedMethods(target);
+        final Set<Method> constrainedMethods = getConstrainedMethods(target);
 
         if (constrainedMethods.size() == 0) return null;
 
@@ -148,17 +150,18 @@ public class ValidationGenerator implements Opcodes {
         return cw.toByteArray();
     }
 
-    private static List<Method> sort(final List<Method> constrainedMethods) {
-        constrainedMethods.sort((a, b) -> a.toString().compareTo(b.toString()));
-        return constrainedMethods;
+    private static List<Method> sort(final Set<Method> constrainedMethods) {
+        final List<Method> methods = new ArrayList<>(constrainedMethods);
+        methods.sort((a, b) -> a.toString().compareTo(b.toString()));
+        return methods;
     }
 
     public static String getName(final Class<?> target) {
         return target.getName() + "$$JwtConstraints";
     }
 
-    public static List<Method> getConstrainedMethods(final Class<?> clazz) {
-        final List<Method> constrained = new ArrayList<Method>();
+    public static Set<Method> getConstrainedMethods(final Class<?> clazz) {
+        final Set<Method> constrained = new HashSet<>();
 
         // we could have been doing this long before Streams
         for (Method method : clazz.getMethods())
diff --git a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/Asmifier.java b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/Asmifier.java
index 71ec9b0..5ab2dcd 100644
--- a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/Asmifier.java
+++ b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/Asmifier.java
@@ -65,7 +65,7 @@ public class Asmifier {
         return readBytes(resource);
     }
 
-    public static byte[] readClassFile(final Class<Colors$$JwtConstraints> clazz) throws IOException {
+    public static byte[] readClassFile(final Class<?> clazz) throws IOException {
         return readClassFile(clazz.getClassLoader(), clazz);
     }
 
diff --git a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/ValidationGeneratorTest.java b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/ValidationGeneratorTest.java
index 46ca86b..816a64b 100644
--- a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/ValidationGeneratorTest.java
+++ b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/ValidationGeneratorTest.java
@@ -1,14 +1,19 @@
 package org.apache.tomee.microprofile.jwt.bval;
 
+import org.apache.openejb.util.proxy.ProxyGenerationException;
 import org.apache.tomee.microprofile.jwt.bval.data.Colors;
 import org.apache.tomee.microprofile.jwt.bval.data.Colors$$JwtConstraints;
+import org.apache.tomee.microprofile.jwt.bval.data.Shapes;
+import org.apache.tomee.microprofile.jwt.bval.data.Shapes$$JwtConstraints;
 import org.junit.Assert;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 public class ValidationGeneratorTest {
@@ -16,7 +21,7 @@ public class ValidationGeneratorTest {
     @Test
     public void testGetConstrainedMethods() throws Exception {
 
-        final List<Method> methods = ValidationGenerator.getConstrainedMethods(Colors.class);
+        final Set<Method> methods = ValidationGenerator.getConstrainedMethods(Colors.class);
         final Map<String, Method> map = methods.stream().collect(Collectors.toMap(Method::getName, method -> method));
 
         Assert.assertTrue(map.containsKey("red"));
@@ -25,9 +30,13 @@ public class ValidationGeneratorTest {
     }
 
     @Test
-    public void test() throws Exception {
-        final String actual = Asmifier.asmify(ValidationGenerator.generateFor(Colors.class));
-        final String expected = Asmifier.asmify(Asmifier.readClassFile(Colors$$JwtConstraints.class));
+    public void testSimple() throws Exception {
+        assertGeneration(Colors.class, Colors$$JwtConstraints.class);
+    }
+
+    private void assertGeneration(final Class<?> target, final Class<?> expectedClass) throws IOException, ProxyGenerationException {
+        final String actual = Asmifier.asmify(ValidationGenerator.generateFor(target));
+        final String expected = Asmifier.asmify(Asmifier.readClassFile(expectedClass));
 
         Assert.assertEquals(expected, actual);
     }
diff --git a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors$$JwtConstraints.java b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors$$JwtConstraints.java
index afc456b..91f5568 100644
--- a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors$$JwtConstraints.java
+++ b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors$$JwtConstraints.java
@@ -17,6 +17,7 @@
 package org.apache.tomee.microprofile.jwt.bval.data;
 
 import org.apache.tomee.microprofile.jwt.bval.Name;
+import org.apache.tomee.microprofile.jwt.bval.ann.Audience;
 import org.apache.tomee.microprofile.jwt.bval.ann.Issuer;
 import org.eclipse.microprofile.jwt.JsonWebToken;
 
@@ -32,6 +33,7 @@ public class Colors$$JwtConstraints {
     }
 
     @Name("public void org.apache.tomee.microprofile.jwt.bval.data.Colors.red()")
+    @Audience("bar")
     @Issuer("http://foo.bar.com")
     public JsonWebToken red$$1() {
         return null;
diff --git a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors.java b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors.java
index 3d667b7..b133257 100644
--- a/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors.java
+++ b/mp-jwt/src/test/java/org/apache/tomee/microprofile/jwt/bval/data/Colors.java
@@ -16,9 +16,12 @@
  */
 package org.apache.tomee.microprofile.jwt.bval.data;
 
+import org.apache.tomee.microprofile.jwt.bval.ann.Audience;
 import org.apache.tomee.microprofile.jwt.bval.ann.Issuer;
 
 public class Colors {
+
+    @Audience("bar")
     @Issuer("http://foo.bar.com")
     public void red() {
     }