You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2018/11/05 21:35:50 UTC
[jspwiki] 14/21: * (breaking) ClassUtil#getMappedObject methods now
throw ReflectiveOperationException,
IllegalArgumentException instead of WikiException * getMappedClass method
now throws ClassNotFoundException instead of WikiException
This is an automated email from the ASF dual-hosted git repository.
juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit d75b50b4246fbff0b31301d0230835883964b4a0
Author: juanpablo <ju...@apache.org>
AuthorDate: Sun Nov 4 21:51:08 2018 +0100
* (breaking) ClassUtil#getMappedObject methods now throw ReflectiveOperationException, IllegalArgumentException instead of WikiException
* getMappedClass method now throws ClassNotFoundException instead of WikiException
---
.../src/main/java/org/apache/wiki/WikiEngine.java | 20 ++--
.../org/apache/wiki/render/RenderingManager.java | 4 +-
.../main/java/org/apache/wiki/util/ClassUtil.java | 126 ++++++++-------------
3 files changed, 60 insertions(+), 90 deletions(-)
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
index 3ac5559..d0aa34d 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
@@ -727,6 +727,10 @@ public class WikiEngine
{
log.fatal("PageProvider is unable to list pages: ", e);
}
+ catch( ReflectiveOperationException | IllegalArgumentException e )
+ {
+ throw new WikiException( "Could not instantiate ReferenceManager: " + e.getMessage(), e );
+ }
}
/**
@@ -837,6 +841,7 @@ public class WikiEngine
*
* @since 2.0.3
*/
+ @Deprecated
public String getEditURL( String pageName )
{
return m_urlConstructor.makeURL( WikiContext.EDIT, pageName, false, null );
@@ -853,6 +858,7 @@ public class WikiEngine
* @deprecated
* @return An URI.
*/
+ @Deprecated
public String getAttachmentURL( String attName )
{
return m_urlConstructor.makeURL( WikiContext.ATTACH, attName, false, null );
@@ -920,6 +926,7 @@ public class WikiEngine
* the near future.
*/
+ @Deprecated
public String safeGetParameter( ServletRequest request, String name )
{
try
@@ -1010,7 +1017,7 @@ public class WikiEngine
*/
public Collection< String > getAllInterWikiLinks()
{
- ArrayList< String > list = new ArrayList< String >();
+ ArrayList< String > list = new ArrayList< >();
for( Enumeration< ? > i = m_properties.propertyNames(); i.hasMoreElements(); )
{
@@ -1033,7 +1040,7 @@ public class WikiEngine
public Collection< String > getAllInlinedImagePatterns()
{
Properties props = getWikiProperties();
- ArrayList<String> ptrnlist = new ArrayList<String>();
+ ArrayList<String> ptrnlist = new ArrayList<>();
for( Enumeration< ? > e = props.propertyNames(); e.hasMoreElements(); )
{
@@ -2320,15 +2327,14 @@ public class WikiEngine
{
try
{
- String s = m_properties.getProperty( PROP_ACL_MANAGER_IMPL,
- DefaultAclManager.class.getName() );
+ String s = m_properties.getProperty( PROP_ACL_MANAGER_IMPL, DefaultAclManager.class.getName() );
m_aclManager = (AclManager)ClassUtil.getMappedObject(s); // TODO: I am not sure whether this is the right call
m_aclManager.initialize( this, m_properties );
}
- catch ( WikiException we )
+ catch ( ReflectiveOperationException | IllegalArgumentException e )
{
- log.fatal( "unable to instantiate class for AclManager: " + we.getMessage() );
- throw new InternalWikiException("Cannot instantiate AclManager, please check logs.", we);
+ log.fatal( "unable to instantiate class for AclManager: " + e.getMessage() );
+ throw new InternalWikiException( "Cannot instantiate AclManager, please check logs.", e );
}
}
return m_aclManager;
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java
index 6af59f1..ef55fa3 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java
@@ -180,8 +180,8 @@ public class RenderingManager implements WikiEventListener, InternalModule
public MarkupParser getParser( WikiContext context, String pagedata ) {
try {
return ( MarkupParser )ClassUtil.getMappedObject( m_markupParserClass, context, new StringReader( pagedata ) );
- } catch( WikiException e ) {
- log.error( "unable to get an instance of " + m_markupParserClass + " (" + e.getMessage() + "), returning default markup parser." );
+ } catch( ReflectiveOperationException | IllegalArgumentException e ) {
+ log.error( "unable to get an instance of " + m_markupParserClass + " (" + e.getMessage() + "), returning default markup parser.", e );
return new JSPWikiMarkupParser( context, new StringReader( pagedata ) );
}
}
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/util/ClassUtil.java b/jspwiki-main/src/main/java/org/apache/wiki/util/ClassUtil.java
index c957766..58a628a 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/util/ClassUtil.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/util/ClassUtil.java
@@ -21,7 +21,6 @@ package org.apache.wiki.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@@ -57,7 +56,7 @@ public final class ClassUtil {
*/
public static final String MAPPINGS = "ini/classmappings.xml";
- private static Map<String, String> c_classMappings = new Hashtable<String, String>();
+ private static Map<String, String> c_classMappings = new Hashtable<>();
private static boolean classLoaderSetup = false;
private static ClassLoader loader = null;
@@ -67,7 +66,7 @@ public final class ClassUtil {
* Initialize the class mappings document.
*/
static {
- List< Element > nodes = XmlUtil.parse( MAPPINGS, "/classmappings/mapping" );
+ List< Element > nodes = XmlUtil.parse( MAPPINGS, "/classmappings/mapping" );
if( nodes.size() > 0 ) {
for( Iterator< Element > i = nodes.iterator(); i.hasNext(); ) {
@@ -183,7 +182,7 @@ public final class ClassUtil {
*/
public static List< String > classpathEntriesUnder( final String rootPackage )
{
- List< String > results = new ArrayList< String >();
+ List< String > results = new ArrayList< >();
Enumeration< URL > en = null;
if( StringUtils.isNotEmpty( rootPackage ) ) {
try
@@ -301,11 +300,12 @@ public final class ClassUtil {
*
* @param requestedClass The name of the class you wish to instantiate.
* @return An instantiated Object.
- * @throws WikiException If the class cannot be found or instantiated.
+ * @throws IllegalArgumentException If the class cannot be found or instantiated.
+ * @throws ReflectiveOperationException If the class cannot be found or instantiated.
* @since 2.5.40
*/
public static Object getMappedObject( String requestedClass )
- throws WikiException
+ throws ReflectiveOperationException, IllegalArgumentException
{
Object[] initargs = {};
return getMappedObject(requestedClass, initargs );
@@ -327,73 +327,46 @@ public final class ClassUtil {
* @param requestedClass The name of the class you wish to instantiate.
* @param initargs The parameters to be passed to the constructor. May be <code>null</code>.
* @return An instantiated Object.
- * @throws WikiException If the class cannot be found or instantiated. The error is logged.
+ * @throws IllegalArgumentException If the class cannot be found or instantiated.
+ * @throws ReflectiveOperationException If the class cannot be found or instantiated.
* @since 2.5.40
*/
public static Object getMappedObject( String requestedClass, Object... initargs )
- throws WikiException
+ throws ReflectiveOperationException, IllegalArgumentException
{
- try
+ Class<?> cl = getMappedClass( requestedClass );
+ Constructor<?>[] ctors = cl.getConstructors();
+
+ //
+ // Try to find the proper constructor by comparing the
+ // initargs array classes and the constructor types.
+ //
+ for( int c = 0; c < ctors.length; c++ )
{
- Class<?> cl = getMappedClass( requestedClass );
-
- Constructor<?>[] ctors = cl.getConstructors();
+ Class<?>[] params = ctors[c].getParameterTypes();
- //
- // Try to find the proper constructor by comparing the
- // initargs array classes and the constructor types.
- //
- for( int c = 0; c < ctors.length; c++ )
+ if( params.length == initargs.length )
{
- Class<?>[] params = ctors[c].getParameterTypes();
-
- if( params.length == initargs.length )
+ for( int arg = 0; arg < initargs.length; arg++ )
{
- for( int arg = 0; arg < initargs.length; arg++ )
+ if( params[arg].isAssignableFrom(initargs[arg].getClass()))
{
- if( params[arg].isAssignableFrom(initargs[arg].getClass()))
- {
- //
- // Ha, found it! Instantiating and returning...
- //
- return ctors[c].newInstance(initargs);
- }
+ //
+ // Ha, found it! Instantiating and returning...
+ //
+ return ctors[c].newInstance(initargs);
}
}
}
-
- //
- // No arguments, so we can just call a default constructor and
- // ignore the arguments.
- //
- Object o = cl.newInstance();
-
- return o;
- }
- catch( InstantiationException e )
- {
- log.info( "Cannot instantiate requested class "+requestedClass, e );
-
- throw new WikiException("Failed to instantiate class "+requestedClass, e );
- }
- catch (IllegalAccessException e)
- {
- log.info( "Cannot access requested class "+requestedClass, e );
-
- throw new WikiException("Failed to instantiate class "+requestedClass, e );
- }
- catch (IllegalArgumentException e)
- {
- log.info( "Illegal arguments when constructing new object", e );
-
- throw new WikiException("Failed to instantiate class "+requestedClass, e );
- }
- catch (InvocationTargetException e)
- {
- log.info( "You tried to instantiate an abstract class "+requestedClass, e );
-
- throw new WikiException("Failed to instantiate class "+requestedClass, e );
}
+
+ //
+ // No arguments, so we can just call a default constructor and
+ // ignore the arguments.
+ //
+ Object o = cl.newInstance();
+
+ return o;
}
/**
@@ -405,7 +378,7 @@ public final class ClassUtil {
* @throws WikiException
*/
private static Class< ? > getMappedClass( String requestedClass )
- throws WikiException
+ throws ClassNotFoundException
{
String mappedClass = c_classMappings.get( requestedClass );
@@ -414,18 +387,9 @@ public final class ClassUtil {
mappedClass = requestedClass;
}
- try
- {
- Class< ? > cl = Class.forName(mappedClass);
-
- return cl;
- }
- catch (ClassNotFoundException e)
- {
- log.info( "Cannot find requested class", e );
-
- throw new WikiException("Failed to instantiate class "+requestedClass, e );
- }
+ Class< ? > cl = Class.forName(mappedClass);
+
+ return cl;
}
/**
@@ -436,14 +400,14 @@ public final class ClassUtil {
* @return {@code true} if {@code srcClassName} is a subclass of {@code parentClassname}, {@code false} otherwise.
*/
public static boolean assignable( String srcClassName, String parentClassName ) {
- try {
- Class< ? > src = Class.forName( srcClassName );
- Class< ? > parent = Class.forName( parentClassName );
- return parent.isAssignableFrom( src );
- } catch( Exception e ) {
- log.error( e.getMessage(), e );
- }
- return false;
+ try {
+ Class< ? > src = Class.forName( srcClassName );
+ Class< ? > parent = Class.forName( parentClassName );
+ return parent.isAssignableFrom( src );
+ } catch( Exception e ) {
+ log.error( e.getMessage(), e );
+ }
+ return false;
}
}