Skip to content

Commit

Permalink
remove direct dependency on ReflectionFactory, refactor into a separa…
Browse files Browse the repository at this point in the history
…te utility class
  • Loading branch information
mbogoevici committed Dec 14, 2010
1 parent 563af98 commit aaea739
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
Expand Up @@ -29,8 +29,7 @@
import org.jboss.interceptor.spi.metadata.ClassMetadata;
import org.jboss.interceptor.spi.model.InterceptionModel;
import org.jboss.interceptor.util.InterceptionUtils;

import sun.reflect.ReflectionFactory;
import org.jboss.interceptor.util.ReflectionFactoryUtils;

/**
* @author <a href="mailto:mariusb@redhat.com">Marius Bogoevici</a>
Expand Down Expand Up @@ -81,7 +80,7 @@ public <T> T createProxyInstance(Class<T> proxyClass, MethodHandler interceptorM
constructor = getNoArgConstructor(proxyClass);
if (constructor == null)
{
constructor = getReflectionFactoryConstructor(proxyClass);
constructor = ReflectionFactoryUtils.getReflectionFactoryConstructor(proxyClass);
}
}
catch (Exception e)
Expand Down Expand Up @@ -116,8 +115,6 @@ public <T> MethodHandler createSubclassingMethodHandler(Object targetInstance, C
return new InterceptorMethodHandler(targetInstance, proxyClass, interceptionModel, interceptorInstantiator, invocationContextFactory);
}



private <T> Constructor<T> getNoArgConstructor(Class<T> clazz)
{
Constructor<T> constructor;
Expand All @@ -132,25 +129,6 @@ private <T> Constructor<T> getNoArgConstructor(Class<T> clazz)
return constructor;
}

private <T> Constructor<T> getReflectionFactoryConstructor(Class<T> proxyClass)
throws NoSuchMethodException
{
try
{
Constructor<T> constructor;
ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
constructor = reflectionFactory.newConstructorForSerialization(proxyClass, Object.class.getDeclaredConstructor());
return constructor;
}
catch (NoSuchMethodException e)
{
return null;
}
catch (SecurityException e)
{
return null;
}
}
}


@@ -0,0 +1,82 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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.jboss.interceptor.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author Marius Bogoevici
*/
public class ReflectionFactoryUtils
{
private static Object reflectionFactory = null;
private static Method newConstructorForSerialization = null;

static
{
try
{
Class <?> reflectionFactoryClass = Class.forName("sun.reflect.ReflectionFactory");
Method getReflectionFactory = reflectionFactoryClass.getDeclaredMethod("getReflectionFactory");
ReflectionUtils.ensureAccessible(getReflectionFactory);
reflectionFactory = getReflectionFactory.invoke(null);
newConstructorForSerialization = reflectionFactoryClass.getDeclaredMethod("newConstructorForSerialization", Class.class, Constructor.class);
ReflectionUtils.ensureAccessible(newConstructorForSerialization);
}
catch (Exception e)
{
// ignore exceptions, no reflection factory will be available
}
}

public static boolean isAvailable()
{
return reflectionFactory != null && newConstructorForSerialization != null;
}

public static <T> Constructor<T> getReflectionFactoryConstructor(Class<T> proxyClass)
throws NoSuchMethodException
{
if (isAvailable())
{
try
{
return (Constructor<T>) newConstructorForSerialization.invoke(reflectionFactory, proxyClass, Object.class.getDeclaredConstructor());
}
catch (NoSuchMethodException e)
{
return null;
}
catch (SecurityException e)
{
return null;
}
catch (IllegalAccessException e)
{
return null;
}
catch (InvocationTargetException e)
{
return null;
}
}
return null;
}
}

0 comments on commit aaea739

Please sign in to comment.