From 3c058fed102907e68c964d82cce213afc6643d39 Mon Sep 17 00:00:00 2001 From: Matthew Gill Date: Tue, 22 Dec 2020 09:39:43 +0000 Subject: [PATCH] Add exception type parsing Exception types are now parsed from MethodModels. Signed-off-by: Matthew Gill --- .../hk2/classmodel/reflect/MethodModel.java | 6 ++++++ .../reflect/impl/MethodModelImpl.java | 19 +++++++++++++++++++ .../impl/MethodSignatureVisitorImpl.java | 10 ++++++++++ .../reflect/impl/ModelClassVisitor.java | 11 +++++++++++ .../reflect/test/method/MethodTest.java | 5 +++++ .../test/method/SimpleAnnotatedMethod.java | 2 +- 6 files changed, 52 insertions(+), 1 deletion(-) diff --git a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/MethodModel.java b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/MethodModel.java index 306f817aae..94b351229c 100755 --- a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/MethodModel.java +++ b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/MethodModel.java @@ -47,6 +47,12 @@ public interface MethodModel extends Member, AnnotatedElement { */ String[] getArgumentTypes(); + /** + * @return the checked exception types, or an empty array if the method doesn't + * declare any thrown exceptions + */ + String[] getExceptionTypes(); + /** * Returns the list of parameter * diff --git a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodModelImpl.java b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodModelImpl.java index b622517aad..ff1634b616 100755 --- a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodModelImpl.java +++ b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodModelImpl.java @@ -26,6 +26,7 @@ public class MethodModelImpl extends AnnotatedElementImpl implements MethodModel { private List parameters; + private List exceptionTypes; private ParameterizedType returnType; final ExtensibleType owner; private final String signature; @@ -74,6 +75,24 @@ public String[] getArgumentTypes() { return stringTypes; } + @Override + public String[] getExceptionTypes() { + String[] stringTypes; + if (exceptionTypes != null) { + stringTypes = new String[exceptionTypes.size()]; + for (int i = 0; i < exceptionTypes.size(); i++) { + stringTypes[i] = exceptionTypes.get(i).getTypeName(); + } + } else { + stringTypes = new String[0]; + } + return stringTypes; + } + + public void setExceptionTypes(List exceptionTypes) { + this.exceptionTypes = exceptionTypes; + } + @Override public List getParameters() { return parameters; diff --git a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodSignatureVisitorImpl.java b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodSignatureVisitorImpl.java index 25fe9c111c..0ffc0d7458 100644 --- a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodSignatureVisitorImpl.java +++ b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/MethodSignatureVisitorImpl.java @@ -36,6 +36,7 @@ public class MethodSignatureVisitorImpl extends SignatureVisitor { private final MethodModel methodModel; private final List parameters = new ArrayList<>(); + private final List exceptionTypes = new ArrayList<>(); private final ParameterizedType returnType = new ParameterizedTypeImpl(); private final ArrayDeque parentType = new ArrayDeque<>(); @@ -50,6 +51,10 @@ public List getParameters() { return parameters; } + public List getExceptionTypes() { + return exceptionTypes; + } + public ParameterizedType getReturnType() { return returnType; } @@ -62,6 +67,11 @@ public SignatureVisitor visitParameterType() { return this; } + @Override + public SignatureVisitor visitExceptionType() { + return this; + } + @Override public SignatureVisitor visitReturnType() { parentType.add(returnType); diff --git a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/ModelClassVisitor.java b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/ModelClassVisitor.java index 02410509a6..35ac364580 100755 --- a/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/ModelClassVisitor.java +++ b/class-model/src/main/java/org/glassfish/hk2/classmodel/reflect/impl/ModelClassVisitor.java @@ -281,6 +281,17 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si org.objectweb.asm.Type type = org.objectweb.asm.Type.getReturnType(desc); returnType.setType(type); + final List exceptionTypes = new ArrayList<>(); + if (exceptions != null) { + for (int i = 0; i < exceptions.length; i++) { + final String exception = exceptions[i]; + final ParameterizedTypeImpl exceptionType = new ParameterizedTypeImpl(exception); + exceptionType.setType(org.objectweb.asm.Type.getObjectType(exception)); + exceptionTypes.add(exceptionType); + } + } + methodModel.setExceptionTypes(exceptionTypes); + org.objectweb.asm.Type[] types = org.objectweb.asm.Type.getArgumentTypes(desc); for (int i = 0; i < methodModel.getParameters().size(); i++) { ParameterImpl parameter = (ParameterImpl) methodModel.getParameter(i); diff --git a/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/MethodTest.java b/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/MethodTest.java index 58d16d0a9d..8ee0a62bb3 100755 --- a/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/MethodTest.java +++ b/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/MethodTest.java @@ -82,6 +82,11 @@ public void simpleTest() throws IOException, InterruptedException { Assert.assertEquals("yellow", gradientColor2.get(0).getValues().get("name")); Assert.assertEquals("orange", gradientColor2.get(1).getValues().get("name")); + // Exception types + final String[] exceptionTypes = mm.getExceptionTypes(); + Assert.assertEquals(1, exceptionTypes.length); + Assert.assertEquals(IllegalArgumentException.class.getName(), exceptionTypes[0]); + // Parameter annotations, type and generic types check Assert.assertEquals(5, mm.getParameters().size()); diff --git a/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/SimpleAnnotatedMethod.java b/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/SimpleAnnotatedMethod.java index b221be5959..ec08f93dda 100755 --- a/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/SimpleAnnotatedMethod.java +++ b/class-model/src/test/java/org/glassfish/hk2/classmodel/reflect/test/method/SimpleAnnotatedMethod.java @@ -44,7 +44,7 @@ public SampleType setFoo( List input, SampleType> sampleType, int count, - Object value) { + Object value) throws IllegalArgumentException { return null; } }