private Field getField(FieldInjectionSite site, String implementationClass, ClassLoader classLoader) throws NoSuchFieldException, ClassNotFoundException { Class<?> clazz = classLoader.loadClass(implementationClass); String name = site.getName(); while (clazz != null) { try { return clazz.getDeclaredField(name); } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } } throw new NoSuchFieldException(name); }
public String getMessage() { if (site instanceof FieldInjectionSite) { FieldInjectionSite field = (FieldInjectionSite) site; return "Unknown injection type " + type + " on field " + field.getName() + " in class " + clazz; } else if (site instanceof MethodInjectionSite) { MethodInjectionSite method = (MethodInjectionSite) site; return "Unknown injection type " + type + " on method " + method + " in class " + clazz; } else if (site instanceof ConstructorInjectionSite) { ConstructorInjectionSite ctor = (ConstructorInjectionSite) site; return "Unknown injection type " + type + " on constructor " + ctor.getConstructor() + " in class " + clazz; } else { return "Unknown injection type " + type + " found in class " + clazz; } } }
/** * Obtain the fields and methods for injection sites associated with the instance * * @param mappings the mappings of injection sites */ private void initializeInjectionSites(Map<InjectionSite, Injectable> mappings) { this.sites = new HashMap<>(); for (Map.Entry<InjectionSite, Injectable> entry : mappings.entrySet()) { InjectionSite site = entry.getKey(); if (site instanceof FieldInjectionSite) { try { Field field = getField(((FieldInjectionSite) site).getName()); sites.put(field, entry.getValue()); } catch (NoSuchFieldException e) { // programming error throw new AssertionError(e); } } else if (site instanceof MethodInjectionSite) { MethodInjectionSite methodInjectionSite = (MethodInjectionSite) site; Method method = methodInjectionSite.getMethod(); sites.put(method, entry.getValue()); } else { // ignore other injection sites } } }
public Class<?> getMemberType(Injectable injectable) { InjectionSite site = findInjectionSite(injectable); if (site == null) { throw new AssertionError("No injection site for " + injectable + " in " + implementationClass); } if (site instanceof FieldInjectionSite) { try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } else if (site instanceof MethodInjectionSite) { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getMethod(); return method.getParameterTypes()[methodSite.getParam()]; } else if (site instanceof ConstructorInjectionSite) { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getConstructor(); return method.getParameterTypes()[methodSite.getParam()]; } else { throw new AssertionError("Invalid injection site type: " + site.getClass()); } }
public Type getGenericType(Injectable injectable) { InjectionSite site = findInjectionSite(injectable); if (site == null) { throw new AssertionError("No injection site for " + injectable + " in " + implementationClass); } if (site instanceof FieldInjectionSite) { try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getGenericType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } else if (site instanceof MethodInjectionSite) { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getMethod(); return method.getGenericParameterTypes()[methodSite.getParam()]; } else if (site instanceof ConstructorInjectionSite) { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getConstructor(); return method.getGenericParameterTypes()[methodSite.getParam()]; } else { throw new AssertionError("Invalid injection site type " + site.getClass()); } }
public Class<?> getMemberType(Injectable injectable) { InjectionSite site = findInjectionSite(injectable); if (site == null) { throw new AssertionError("No injection site for " + injectable + " in " + implementationClass); } if (site instanceof FieldInjectionSite) { try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } else if (site instanceof MethodInjectionSite) { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getMethod(); return method.getParameterTypes()[methodSite.getParam()]; } else if (site instanceof ConstructorInjectionSite) { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getConstructor(); return method.getParameterTypes()[methodSite.getParam()]; } else { throw new AssertionError("Invalid injection site type: " + site.getClass()); } }
public Type getGenericType(Injectable injectable) { InjectionSite site = findInjectionSite(injectable); if (site == null) { throw new AssertionError("No injection site for " + injectable + " in " + implementationClass); } if (site instanceof FieldInjectionSite) { try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getGenericType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } else if (site instanceof MethodInjectionSite) { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getMethod(); return method.getGenericParameterTypes()[methodSite.getParam()]; } else if (site instanceof ConstructorInjectionSite) { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getConstructor(); return method.getGenericParameterTypes()[methodSite.getParam()]; } else { throw new AssertionError("Invalid injection site type " + site.getClass()); } }
Field field = getField(fieldSite.getName()); Injector<?> injector = reflectionFactory.createInjector(field, supplier); injectors.put(attribute, injector);
Field field = getField(fieldSite.getName()); Injector<?> injector = reflectionFactory.createInjector(field, supplier); injectors.put(attribute, injector);
try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getType(); } catch (NoSuchFieldException e) {
try { FieldInjectionSite fieldSite = (FieldInjectionSite) site; Field field = getField(fieldSite.getName()); return field.getGenericType(); } catch (NoSuchFieldException e) {
Field field = getField(fieldSite.getName()); Injector<?> injector = reflectionFactory.createInjector(field, factory); injectors.put(attribute, injector);