Author: rfscholte Date: Sun Oct 14 11:18:22 2012 New Revision: 1398053 URL: http://svn.apache.org/viewvc?rev=1398053&view=rev Log: evaluate() should never throw Exception, all exceptions are now wrapped in a new IntrospectionException
Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/IntrospectionException.java Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractor.java Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/IntrospectionException.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/IntrospectionException.java?rev=1398053&view=auto ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/IntrospectionException.java (added) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/IntrospectionException.java Sun Oct 14 11:18:22 2012 @@ -0,0 +1,46 @@ +package org.apache.maven.shared.utils.introspection; + +/* + * 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. + */ + +public class IntrospectionException + extends Exception +{ + + public IntrospectionException() + { + super(); + } + + public IntrospectionException( String message, Throwable cause ) + { + super( message, cause ); + } + + public IntrospectionException( String message ) + { + super( message ); + } + + public IntrospectionException( Throwable cause ) + { + super( cause ); + } + +} Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractor.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractor.java?rev=1398053&r1=1398052&r2=1398053&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractor.java (original) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractor.java Sun Oct 14 11:18:22 2012 @@ -26,6 +26,7 @@ import java.util.WeakHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.maven.shared.utils.StringUtils; +import org.apache.maven.shared.utils.introspection.MethodMap.AmbiguousException; /** @@ -41,7 +42,7 @@ import org.apache.maven.shared.utils.Str */ public class ReflectionValueExtractor { - private static final Class[] CLASS_ARGS = new Class[0]; + private static final Class<?>[] CLASS_ARGS = new Class[0]; private static final Object[] OBJECT_ARGS = new Object[0]; @@ -79,10 +80,10 @@ public class ReflectionValueExtractor * @param expression not null expression * @param root not null object * @return the object defined by the expression - * @throws Exception if any + * @throws IntrospectionException if any */ public static Object evaluate( String expression, Object root ) - throws Exception + throws IntrospectionException { return evaluate( expression, root, true ); } @@ -100,11 +101,10 @@ public class ReflectionValueExtractor * @param expression not null expression * @param root not null object * @return the object defined by the expression - * @throws Exception if any + * @throws IntrospectionException if any */ - // TODO: don't throw Exception public static Object evaluate( String expression, Object root, boolean trimRootToken ) - throws Exception + throws IntrospectionException { // if the root token refers to the supplied root object parameter, remove it. if ( trimRootToken ) @@ -142,8 +142,32 @@ public class ReflectionValueExtractor { String methodBase = StringUtils.capitalizeFirstLetter( matcher.group( 1 ) ); String methodName = "get" + methodBase; - method = classMap.findMethod( methodName, CLASS_ARGS ); - value = method.invoke( value, OBJECT_ARGS ); + try + { + method = classMap.findMethod( methodName, CLASS_ARGS ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } + + try + { + value = method.invoke( value, OBJECT_ARGS ); + } + catch ( IllegalArgumentException e ) + { + throw new IntrospectionException( e ); + } + catch ( IllegalAccessException e ) + { + throw new IntrospectionException( e ); + } + catch ( InvocationTargetException e ) + { + throw new IntrospectionException( e ); + } + classMap = getClassMap( value.getClass() ); if ( classMap.getCachedClass().isArray() ) @@ -157,11 +181,18 @@ public class ReflectionValueExtractor // use get method on List interface localParams = new Object[1]; localParams[0] = Integer.valueOf( matcher.group( 2 ) ); - method = classMap.findMethod( "get", localParams ); + try + { + method = classMap.findMethod( "get", localParams ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } } else { - throw new Exception( "The token '" + token + throw new IntrospectionException( "The token '" + token + "' refers to a java.util.List or an array, but the value seems is an instance of '" + value.getClass() + "'." ); } @@ -174,8 +205,31 @@ public class ReflectionValueExtractor { String methodBase = StringUtils.capitalizeFirstLetter( matcher.group( 1 ) ); String methodName = "get" + methodBase; - method = classMap.findMethod( methodName, CLASS_ARGS ); - value = method.invoke( value, OBJECT_ARGS ); + try + { + method = classMap.findMethod( methodName, CLASS_ARGS ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } + + try + { + value = method.invoke( value, OBJECT_ARGS ); + } + catch ( IllegalArgumentException e ) + { + throw new IntrospectionException( e ); + } + catch ( IllegalAccessException e ) + { + throw new IntrospectionException( e ); + } + catch ( InvocationTargetException e ) + { + throw new IntrospectionException( e ); + } classMap = getClassMap( value.getClass() ); if ( value instanceof Map ) @@ -183,11 +237,18 @@ public class ReflectionValueExtractor // use get method on List interface localParams = new Object[1]; localParams[0] = matcher.group( 2 ); - method = classMap.findMethod( "get", localParams ); + try + { + method = classMap.findMethod( "get", localParams ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } } else { - throw new Exception( "The token '" + token + throw new IntrospectionException( "The token '" + token + "' refers to a java.util.Map, but the value seems is an instance of '" + value.getClass() + "'." ); } @@ -196,14 +257,28 @@ public class ReflectionValueExtractor { String methodBase = StringUtils.capitalizeFirstLetter( token ); String methodName = "get" + methodBase; - method = classMap.findMethod( methodName, CLASS_ARGS ); + try + { + method = classMap.findMethod( methodName, CLASS_ARGS ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } if ( method == null ) { // perhaps this is a boolean property?? methodName = "is" + methodBase; - method = classMap.findMethod( methodName, CLASS_ARGS ); + try + { + method = classMap.findMethod( methodName, CLASS_ARGS ); + } + catch ( AmbiguousException e ) + { + throw new IntrospectionException( e ); + } } } } @@ -225,7 +300,15 @@ public class ReflectionValueExtractor return null; } - throw e; + throw new IntrospectionException( e ); + } + catch ( IllegalArgumentException e ) + { + throw new IntrospectionException( e ); + } + catch ( IllegalAccessException e ) + { + throw new IntrospectionException( e ); } }