org.jvnet.hk2.internal.ClazzCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache Show documentation
Show all versions of ehcache Show documentation
Ehcache is an open source, standards-based cache used to boost performance,
offload the database and simplify scalability. Ehcache is robust, proven and full-featured and
this has made it the most widely-used Java-based cache.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.hk2.internal;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyFactory;
import org.aopalliance.intercept.ConstructorInterceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.ClassAnalyzer;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.InjectionResolver;
import org.glassfish.hk2.api.InstanceLifecycleEventType;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.PreDestroy;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.utilities.reflection.Logger;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
/**
* @author jwells
* @param The type of object this creator creates
*
*/
public class ClazzCreator implements Creator {
private final static MethodFilter METHOD_FILTER = new MethodFilter() {
@Override
public boolean isHandled(Method method) {
// We do not allow interception of finalize
if (method.getName().equals("finalize")) return false;
return true;
}
};
private final ServiceLocatorImpl locator;
private final Class> implClass;
private final Set myInitializers = new LinkedHashSet();
private final Set myFields = new LinkedHashSet();
private ActiveDescriptor> selfDescriptor;
private ResolutionInfo myConstructor;
private List allInjectees;
private Method postConstructMethod;
private Method preDestroyMethod;
/* package */ ClazzCreator(ServiceLocatorImpl locator,
Class> implClass) {
this.locator = locator;
this.implClass = implClass;
}
/* package */ void initialize(
ActiveDescriptor> selfDescriptor,
String analyzerName,
Collector collector) {
this.selfDescriptor = selfDescriptor;
if ((selfDescriptor != null) &&
selfDescriptor.getAdvertisedContracts().contains(
ClassAnalyzer.class.getName())) {
String descriptorAnalyzerName = selfDescriptor.getName();
if (descriptorAnalyzerName == null) descriptorAnalyzerName = locator.getDefaultClassAnalyzerName();
String incomingAnalyzerName = analyzerName;
if (incomingAnalyzerName == null) incomingAnalyzerName = locator.getDefaultClassAnalyzerName();
if (descriptorAnalyzerName.equals(incomingAnalyzerName)) {
collector.addThrowable(new IllegalArgumentException(
"The ClassAnalyzer named " + descriptorAnalyzerName +
" is its own ClassAnalyzer. Ensure that an implementation of" +
" ClassAnalyzer is not its own ClassAnalyzer"));
myConstructor = null;
return;
}
}
ClassAnalyzer analyzer = Utilities.getClassAnalyzer(locator, analyzerName, collector);
if (analyzer == null) {
myConstructor = null;
return;
}
List baseAllInjectees = new LinkedList();
AnnotatedElement element;
List injectees;
element = Utilities.getConstructor(implClass, analyzer, collector);
if (element == null) {
myConstructor = null;
return;
}
injectees = Utilities.getConstructorInjectees((Constructor>) element, selfDescriptor);
if (injectees == null) {
myConstructor = null;
return;
}
baseAllInjectees.addAll(injectees);
myConstructor = new ResolutionInfo(element, injectees);
Set initMethods = Utilities.getInitMethods(implClass, analyzer, collector);
for (Method initMethod : initMethods) {
element = initMethod;
injectees = Utilities.getMethodInjectees(initMethod, selfDescriptor);
if (injectees == null) return;
baseAllInjectees.addAll(injectees);
myInitializers.add(new ResolutionInfo(element, injectees));
}
Set fields = Utilities.getInitFields(implClass, analyzer, collector);
for (Field field : fields) {
element = field;
injectees = Utilities.getFieldInjectees(field, selfDescriptor);
if (injectees == null) return;
baseAllInjectees.addAll(injectees);
myFields.add(new ResolutionInfo(element, injectees));
}
postConstructMethod = Utilities.getPostConstruct(implClass, analyzer, collector);
preDestroyMethod = Utilities.getPreDestroy(implClass, analyzer, collector);
allInjectees = Collections.unmodifiableList(baseAllInjectees);
Utilities.validateSelfInjectees(selfDescriptor, allInjectees, collector);
}
/* package */ void initialize(
ActiveDescriptor> selfDescriptor,
Collector collector) {
initialize(selfDescriptor, (selfDescriptor == null) ? null :
selfDescriptor.getClassAnalysisName(), collector);
}
/**
* This is done because sometimes when creating the creator we do not know
* what the true system descriptor will be
*
* @param selfDescriptor The descriptor that should replace our self descriptor
*/
/* package */ void resetSelfDescriptor(ActiveDescriptor> selfDescriptor) {
this.selfDescriptor = selfDescriptor;
for (Injectee injectee : allInjectees) {
if (!(injectee instanceof InjecteeImpl)) continue;
((InjecteeImpl) injectee).resetInjecteeDescriptor(selfDescriptor);
}
}
private void resolve(Map addToMe,
InjectionResolver> resolver,
Injectee injectee,
ServiceHandle> root,
Collector errorCollection) {
if (injectee.isSelf()) {
addToMe.put(injectee, selfDescriptor);
return;
}
Object addIn = null;
try {
addIn = resolver.resolve(injectee, root);
} catch (Throwable th) {
errorCollection.addThrowable(th);
}
if (addIn != null) {
addToMe.put(injectee, addIn);
}
}
private Map resolveAllDependencies(final ServiceHandle> root) throws MultiException, IllegalStateException {
Collector errorCollector = new Collector();
final Map retVal = new LinkedHashMap();
for (Injectee injectee : myConstructor.injectees) {
InjectionResolver> resolver = Utilities.getInjectionResolver(locator, injectee);
resolve(retVal, resolver, injectee, root, errorCollector);
}
for (ResolutionInfo fieldRI : myFields) {
InjectionResolver> resolver = Utilities.getInjectionResolver(locator, fieldRI.baseElement);
for (Injectee injectee : fieldRI.injectees) {
resolve(retVal, resolver, injectee, root, errorCollector);
}
}
for (ResolutionInfo methodRI : myInitializers) {
for (Injectee injectee : methodRI.injectees) {
InjectionResolver> resolver = Utilities.getInjectionResolver(locator, injectee);
resolve(retVal, resolver, injectee, root, errorCollector);
}
}
if (errorCollector.hasErrors()) {
errorCollector.addThrowable(new IllegalArgumentException("While attempting to resolve the dependencies of "
+ implClass.getName() + " errors were found"));
errorCollector.throwIfErrors();
}
return retVal;
}
private Object createMe(Map resolved) throws Throwable {
final Constructor> c = (Constructor>) myConstructor.baseElement;
List injectees = myConstructor.injectees;
final Object args[] = new Object[injectees.size()];
for (Injectee injectee : injectees) {
args[injectee.getPosition()] = resolved.get(injectee);
}
Utilities.Interceptors interceptors = Utilities.getAllInterceptors(locator, selfDescriptor, implClass, c);
final Map> methodInterceptors = interceptors.getMethodInterceptors();
List constructorInterceptors = interceptors.getConstructorInterceptors();
if ((methodInterceptors == null || methodInterceptors.isEmpty()) &&
((constructorInterceptors == null) || constructorInterceptors.isEmpty())) {
// No need for any kind of interception
return ReflectionHelper.makeMe(c, args, locator.getNeutralContextClassLoader());
}
final boolean neutral = locator.getNeutralContextClassLoader();
if (methodInterceptors == null || methodInterceptors.isEmpty()) {
// No method interceptors means no need for proxy at all
return ConstructorInterceptorHandler.construct(c, args, neutral, constructorInterceptors);
}
return ConstructorInterceptorHandler.construct(c,
args,
neutral,
constructorInterceptors,
new ConstructorInterceptorHandler.ConstructorAction() {
@Override
public Object makeMe(final Constructor> c, final Object[] args, final boolean neutralCCL)
throws Throwable {
final MethodInterceptorHandler methodInterceptor = new MethodInterceptorHandler(locator, methodInterceptors);
final ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(implClass);
proxyFactory.setFilter(METHOD_FILTER);
return AccessController.doPrivileged(new PrivilegedExceptionAction