private static void setFieldValue(InspectionData data) throws Throwable { if (data.isStatic) { data.fieldValue = data.field.get(null); } else { data.fieldValue = data.field.get(data.fieldContainerInstance); } }
private static InspectionData getInjectedField(String encodedField) throws Throwable { InspectionData data = new InspectionData(); Class lookupClass = null; if (encodedField.contains("|")) { StringTokenizer tokenizer = new StringTokenizer(encodedField, "|"); if (tokenizer.countTokens() != 2) { throw new InspectionException("invalid encoded field: " + encodedField); } lookupClass = Class.forName(tokenizer.nextToken()); encodedField = tokenizer.nextToken(); } StringTokenizer t = new StringTokenizer(encodedField, ","); if (t.countTokens() != 2) { throw new InspectionException("invalid encoded field: " + encodedField); } Class clazz = Class.forName(t.nextToken()); Field field = clazz.getDeclaredField(t.nextToken()); field.setAccessible(true); data.encodedField = encodedField; data.lookupClass = lookupClass; data.actualClass = clazz; data.field = field; return data; }
private static InspectionData createInspectionData(String encodedField) throws Throwable { InspectionData data; if (encodedField.contains(":")) { data = getStaticField(encodedField); } else { data = getInjectedField(encodedField); } data.annotations = Arrays.asList(data.field.getAnnotations()); return data; }
private static InspectionData getStaticField(String encodedField) throws Throwable { StringTokenizer t = new StringTokenizer(encodedField, ":"); if (t.countTokens() != 2) { throw new InspectionException("invalid encoded field: " + encodedField); } Class clazz = Class.forName(t.nextToken()); Field field = clazz.getDeclaredField(t.nextToken()); field.setAccessible(true); InspectionData data = new InspectionData(); data.encodedField = encodedField; data.isStatic = true; data.fieldValue = clazz; data.field = field; return data; }
private static void setFieldContainerInstance(InspectionData data, Injector injector) throws Throwable { // no container instance for static data. if (!data.isStatic) { if (data.lookupClass == null) { // if the container had an enclosing class and it's not static, make sure the lookup // points to the enclosing class. if (data.actualClass.getEnclosingClass() != null && !Modifier.isStatic(data.actualClass.getModifiers())) { data.lookupClass = data.actualClass.getEnclosingClass(); } } // check if this is an enclosed class if (data.lookupClass == null) { Binding<?> binding = injector.getBinding(data.actualClass); validateBindingIsSingleton(injector, binding); data.fieldContainerInstance = binding.getProvider().get(); } else { // inner classes must be annotated properly Binding<?> binding = injector.getBinding(data.lookupClass); validateBindingIsSingleton(injector, binding); Object lookupObj = binding.getProvider().get(); // If this was an enclosed class, validate that it has InspectableContainer. if (data.actualClass.getEnclosingClass() != null && !Modifier.isStatic(data.actualClass.getModifiers())) { if (data.actualClass.getAnnotation(InspectableContainer.class) == null) { throw new InspectionException("container must be annotated with InspectableContainer"); } Constructor[] constructors = data.actualClass.getDeclaredConstructors(); if (constructors.length != 1) { throw new InspectionException("wrong constructors length: " + constructors.length); } Class[] parameters = constructors[0].getParameterTypes(); if (parameters.length != 1 || !data.lookupClass.isAssignableFrom(parameters[0])) { throw new InspectionException("wrong parameter count or type for constructor"); } constructors[0].setAccessible(true); data.fieldContainerInstance = constructors[0].newInstance(lookupObj); } else { data.fieldContainerInstance = lookupObj; } } } }