private void findTestedAndInjectableFields(@NotNull Field[] fieldsDeclaredInTestClass) { for (Field field : fieldsDeclaredInTestClass) { if (field.isAnnotationPresent(Injectable.class)) { MockedType mockedType = new MockedType(field); injectableFields.add(mockedType); } else { addAsTestedFieldIfApplicable(field); } } }
private Field findSqlMapExecutorDelegate(SqlMapClient sqlMapClient) { Class<?> searchType = sqlMapClient.getClass(); while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (SqlMapExecutorDelegate.class.isAssignableFrom(field.getType())) return field; } searchType = searchType.getSuperclass(); } return null; }
void buildConfigPanel() { try { Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded Class<?> fieldType = field.getType(); Method getMethod = getGetMethod(config.getClass(), field.getType(), field.getName()); if (getMethod != null) { Object value = getMethod.invoke(config); if (fieldType == String.class) { addTextBox(field.getName(), (String) value); } if (fieldType == boolean.class || fieldType == Boolean.class) { addBooleanComponent(field.getName(), (Boolean) value); } if (fieldType == float.class || fieldType == Float.class) { addTextBox(field.getName(), "" + value); } if (fieldType == int.class || fieldType == Integer.class) { addTextBox(field.getName(), "" + value); } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } configGridLayout.setRows(configGridLayout.getRows() + 2); configRevertButton = new JButton("Revert"); configReloadButton = new JButton("Reload"); configApplyButton = new JButton("Apply"); configSaveButton = new JButton("Save"); configRevertButton.addActionListener(new ConfigRevert()); configReloadButton.addActionListener(new ConfigReload()); configApplyButton.addActionListener(new ConfigApply()); configSaveButton.addActionListener(new ConfigSave()); configPanel.add(configRevertButton); configPanel.add(configReloadButton); configPanel.add(configApplyButton); configPanel.add(configSaveButton); }
public SqlMapRepositoryFactory(SqlMapClient sqlMapClient) { Assert.notNull(sqlMapClient, "SqlMapClient must not be null!"); this.sqlMapClient = (SqlMapClientImpl) sqlMapClient; this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient); if (ExtendedSqlMapClient.class.isAssignableFrom(sqlMapClient.getClass())) { this.sqlMapExecutorDelegate = ((ExtendedSqlMapClient) sqlMapClient).getDelegate(); } else if (hasSqlMapExecutorDelegate(sqlMapClient)) { Field field = findSqlMapExecutorDelegate(sqlMapClient); field.setAccessible(true); this.sqlMapExecutorDelegate = (SqlMapExecutorDelegate) ReflectionUtils.getField(field, sqlMapClient); } else { throw new IllegalArgumentException("not found SqlMapExecutorDelegate in SqlMapClient."); } }
public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) { Class clazz = classNode.getTypeClass(); Field[] fields = clazz.getDeclaredFields(); for (Field f : fields) { ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType()); classNode.addField(f.getName(), f.getModifiers(), ret, null); } Method[] methods = clazz.getDeclaredMethods(); for (Method m : methods) { ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType()); Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes()); ClassNode[] exceptions = makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes()); MethodNode mn = new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null); setMethodDefaultValue(mn, m); setAnnotationMetaData(m.getAnnotations(), mn); mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters())); classNode.addMethod(mn); } Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor ctor : constructors) { Parameter[] params = makeParameters(compileUnit, ctor.getGenericParameterTypes(), ctor.getParameterTypes()); ClassNode[] exceptions = makeClassNodes(compileUnit, ctor.getGenericExceptionTypes(), ctor.getExceptionTypes()); classNode.addConstructor(ctor.getModifiers(), params, exceptions, null); } Class sc = clazz.getSuperclass(); if (sc != null) classNode.setUnresolvedSuperClass( makeClassNode(compileUnit, clazz.getGenericSuperclass(), sc)); makeInterfaceTypes(compileUnit, classNode, clazz); setAnnotationMetaData(classNode.getTypeClass().getAnnotations(), classNode); PackageNode packageNode = classNode.getPackage(); if (packageNode != null) { setAnnotationMetaData(classNode.getTypeClass().getPackage().getAnnotations(), packageNode); } }
public int compare(Field a, Field b) { if (a == b) return 0; else if (a == null) return -1; else if (b == null) return 1; else if (a.equals(b)) return 0; int cmp = a.getName().compareTo(b.getName()); if (cmp != 0) return cmp; cmp = a.getDeclaringClass().getName().compareTo(b.getDeclaringClass().getName()); if (cmp != 0) return cmp; return a.getType().getName().compareTo(b.getType().getName()); }
private void addAsTestedFieldIfApplicable(@NotNull Field fieldFromTestClass) { for (Annotation fieldAnnotation : fieldFromTestClass.getDeclaredAnnotations()) { Tested testedMetadata; if (fieldAnnotation instanceof Tested) { testedMetadata = (Tested) fieldAnnotation; } else { testedMetadata = fieldAnnotation.annotationType().getAnnotation(Tested.class); } if (testedMetadata != null) { TestedField testedField = new TestedField(injectionState, fieldFromTestClass, testedMetadata); testedFields.add(testedField); break; } } }
void revertConfig() { try { debug("reverting config panel"); Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded debug("field " + field.getName()); Class<?> fieldType = field.getType(); Method getMethod = getGetMethod(config.getClass(), field.getType(), field.getName()); if (getMethod != null) { debug(" ... found accessor method"); Object value = getMethod.invoke(config); String fieldname = field.getName(); Component component = componentByName.get(fieldname); if (component != null) { debug(" ... found component"); if (fieldType == String.class) { ((JTextField) component).setText((String) value); } if (fieldType == boolean.class || fieldType == Boolean.class) { ((JCheckBox) component).setSelected((Boolean) value); } if (fieldType == float.class || fieldType == Float.class) { ((JTextField) component).setText("" + value); } if (fieldType == int.class || fieldType == Integer.class) { ((JTextField) component).setText("" + value); } } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } }
/** Calculates a MD5 digest of the class. */ public String getDigest() { try { if (_className == null || "".equals(_className)) return ""; DynamicClassLoader loader = (DynamicClassLoader) Thread.currentThread().getContextClassLoader(); ClassLoader tmpLoader = loader.getNewTempClassLoader(); Class cl = Class.forName(_className, false, tmpLoader); if (cl == null) return ""; MessageDigest digest = MessageDigest.getInstance("MD5"); addDigest(digest, cl.getName()); addDigest(digest, cl.getModifiers()); Class superClass = cl.getSuperclass(); if (superClass != null) addDigest(digest, superClass.getName()); Class[] interfaces = cl.getInterfaces(); for (int i = 0; i < interfaces.length; i++) addDigest(digest, interfaces[i].getName()); Field[] fields = cl.getDeclaredFields(); Arrays.sort(fields, new FieldComparator()); if (_checkFields) { for (Field field : fields) { if (Modifier.isPrivate(field.getModifiers()) && !_checkPrivate) continue; if (Modifier.isProtected(field.getModifiers()) && !_checkProtected) continue; addDigest(digest, field.getName()); addDigest(digest, field.getModifiers()); addDigest(digest, field.getType().getName()); addDigest(digest, field.getAnnotations()); } } Method[] methods = cl.getDeclaredMethods(); Arrays.sort(methods, new MethodComparator()); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (Modifier.isPrivate(method.getModifiers()) && !_checkPrivate) continue; if (Modifier.isProtected(method.getModifiers()) && !_checkProtected) continue; if (Modifier.isStatic(method.getModifiers()) && !_checkStatic) continue; addDigest(digest, method.getName()); addDigest(digest, method.getModifiers()); addDigest(digest, method.getName()); Class[] param = method.getParameterTypes(); for (int j = 0; j < param.length; j++) addDigest(digest, param[j].getName()); addDigest(digest, method.getReturnType().getName()); Class[] exn = method.getExceptionTypes(); for (int j = 0; j < exn.length; j++) addDigest(digest, exn[j].getName()); addDigest(digest, method.getAnnotations()); } byte[] digestBytes = new byte[256]; int len = digest.digest(digestBytes, 0, digestBytes.length); return digestToBase64(digestBytes, len); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); return ""; } }
void applyConfig() { debug("applying config from panel"); Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded debug("field " + field.getName()); Class<?> fieldType = field.getType(); Method setMethod = getSetMethod(config.getClass(), field.getType(), field.getName()); if (setMethod != null) { debug(" ... found accessor method"); String fieldname = field.getName(); Component component = componentByName.get(fieldname); if (component != null) { debug(" ... found component"); Object value = null; if (fieldType == String.class) { value = ((JTextField) component).getText(); } if (fieldType == boolean.class || fieldType == Boolean.class) { value = ((JCheckBox) component).isSelected(); } if (fieldType == float.class || fieldType == Float.class) { String stringvalue = (String) ((JTextField) component).getText(); try { value = Float.parseFloat(stringvalue); } catch (Exception e) { } } if (fieldType == int.class || fieldType == Integer.class) { String stringvalue = (String) ((JTextField) component).getText(); try { value = Integer.parseInt(stringvalue); } catch (Exception e) { } } if (value != null) { try { setMethod.invoke(config, value); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } } } if (fieldType == boolean.class || fieldType == Boolean.class) { // addBooleanComponent( field.getName(), (Boolean)value ); } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } revertConfig(); // in case some parses and stuff didn't work, so // user can see what is actually being read. playerObjects .getMainUI() .showInfo( "Config updated. Note that most changes require an AI restart. You can click on 'reloadAI' in 'Actions' tab to do so."); playerObjects.getConfig().configUpdated(); }