void addIfTestMethod(Method m) { if (m.getAnnotation(Test.class) == null) return; if (!(m.getReturnType().equals(boolean.class) || m.getReturnType().equals(void.class))) throw new RuntimeException("@Test method" + " must return boolean or void"); m.setAccessible(true); // In case it's private, etc. add(m); }
public static Method[] findMethodsByAnnotation( Class<?> owner, Class<? extends Annotation> annotationClass) { Method[] methods = owner.getMethods(); ArrayBuilder<Method> builder = new ArrayBuilder<Method>(Method.class); for (Method method : methods) if (method.getAnnotation(annotationClass) != null) builder.add(method); return builder.toArray(); }
private static Method checkForCreatorMethod(Method m) { if (m.getAnnotation(TestObjectCreate.class) == null) return null; if (!m.getReturnType().equals(testClass)) throw new RuntimeException( "@TestObjectCreate " + "must return instance of Class to be tested"); if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) < 1) throw new RuntimeException("@TestObjectCreate " + "must be static."); m.setAccessible(true); return m; }
/** * {@link InvocationHandler} implementation that allows strongly-typed access to the * configuration. * * <p>TODO: it might be a great performance improvement to have APT generate code that does this * during the development time by looking at the interface. */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // serve java.lang.Object methods by ourselves Class<?> clazz = method.getDeclaringClass(); if (clazz == Object.class) { try { return method.invoke(this, args); } catch (InvocationTargetException e) { throw e.getTargetException(); } } if (method.getAnnotation(DuckTyped.class) != null) { return invokeDuckMethod(method, proxy, args); } if (method.getAnnotation(ConfigExtensionMethod.class) != null) { ConfigExtensionMethod cem = method.getAnnotation(ConfigExtensionMethod.class); ConfigExtensionHandler handler = (ConfigExtensionHandler) ((cem.value() != null) ? getServiceLocator().getService(ConfigExtensionHandler.class, cem.value()) : getServiceLocator().getService(ConfigExtensionHandler.class)); return invokeConfigExtensionMethod(handler, this, model.getProxyType(), args); } ConfigModel.Property p = model.toProperty(method); if (p == null) throw new IllegalArgumentException("No corresponding property found for method: " + method); if (args == null || args.length == 0) { // getter return getter(p, method.getGenericReturnType()); } else { throw new PropertyVetoException( "Instance of " + getImplementation() + " named '" + getKey() + "' is not locked for writing when invoking method " + method.getName() + " you must use transaction semantics to access it.", null); } }
private static Method checkForCleanupMethod(Method m) { if (m.getAnnotation(TestObjectCleanup.class) == null) return null; if (!m.getReturnType().equals(void.class)) throw new RuntimeException("@TestObjectCleanup " + "must return void"); if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) < 1) throw new RuntimeException("@TestObjectCleanup " + "must be static."); if (m.getParameterTypes().length == 0 || m.getParameterTypes()[0] != testClass) throw new RuntimeException( "@TestObjectCleanup " + "must take an argument of the tested type."); m.setAccessible(true); return m; }
/** * If a {@link Model} is part of a composition, and is the weak part (is part of other models), * this method returns all the fields containing connection of {@link ConnectionType.BelongsTo} * type. It's useful in the <code>INSERT</code> of the record, for preventing foreign key * costraint inconsistency. * * @param model The given {@link Model}. * @return A list of fields that represent the Models that owns <code>model</code>. */ protected List<String> belongsTo(Class model) { if (belongsToFields.containsKey(model)) return belongsToFields.get(model); List<Class> sC = getSupers(model); List<String> ownerFields = new ArrayList<String>(); Method[] getters = CommonStatic.getDeclaredGetters(model); for (Method g : getters) /* has a belongs to annotation */ if (g.isAnnotationPresent(Connection.class) && g.getAnnotation(Connection.class).type().equals(ConnectionType.BelongsTo)) { ownerFields.add(fieldName(g)); } for (Class s : sC) ownerFields.addAll(belongsTo(s)); belongsToFields.put(model, ownerFields); return ownerFields; }
protected void generateView( Map<String, org.ektorp.support.DesignDocument.View> views, Method me) { DocumentReferences referenceMetaData = me.getAnnotation(DocumentReferences.class); if (referenceMetaData == null) { LOG.warn("No DocumentReferences annotation found in method: ", me.getName()); return; } if (!me.getName().startsWith("get")) { throw new ViewGenerationException( String.format( "The method: %s in %s annotated with DocumentReferences does not conform to the naming convention of 'getXxxx'", me.getName(), me.getDeclaringClass())); } if (Set.class.isAssignableFrom(me.getReturnType())) { generateSetBasedDocRefView(views, me, referenceMetaData); } else { throw new ViewGenerationException( String.format( "The return type of: %s in %s annotated with DocumentReferences is not supported. (Must be assignable from java.util.Set)", me.getName(), me.getDeclaringClass())); } }
@SuppressWarnings("unchecked") LDAPObjectHandler(final Class<T> type) throws LDAPPersistException { this.type = type; final Class<? super T> superclassType = type.getSuperclass(); if (superclassType == null) { superclassHandler = null; } else { final LDAPObject superclassAnnotation = superclassType.getAnnotation(LDAPObject.class); if (superclassAnnotation == null) { superclassHandler = null; } else { superclassHandler = new LDAPObjectHandler(superclassType); } } final TreeMap<String, FieldInfo> fields = new TreeMap<String, FieldInfo>(); final TreeMap<String, GetterInfo> getters = new TreeMap<String, GetterInfo>(); final TreeMap<String, SetterInfo> setters = new TreeMap<String, SetterInfo>(); ldapObject = type.getAnnotation(LDAPObject.class); if (ldapObject == null) { throw new LDAPPersistException(ERR_OBJECT_HANDLER_OBJECT_NOT_ANNOTATED.get(type.getName())); } final LinkedHashMap<String, String> objectClasses = new LinkedHashMap<String, String>(10); final String oc = ldapObject.structuralClass(); if (oc.length() == 0) { structuralClass = getUnqualifiedClassName(type); } else { structuralClass = oc; } final StringBuilder invalidReason = new StringBuilder(); if (PersistUtils.isValidLDAPName(structuralClass, invalidReason)) { objectClasses.put(toLowerCase(structuralClass), structuralClass); } else { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_STRUCTURAL_CLASS.get( type.getName(), structuralClass, invalidReason.toString())); } auxiliaryClasses = ldapObject.auxiliaryClass(); for (final String auxiliaryClass : auxiliaryClasses) { if (PersistUtils.isValidLDAPName(auxiliaryClass, invalidReason)) { objectClasses.put(toLowerCase(auxiliaryClass), auxiliaryClass); } else { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_AUXILIARY_CLASS.get( type.getName(), auxiliaryClass, invalidReason.toString())); } } superiorClasses = ldapObject.superiorClass(); for (final String superiorClass : superiorClasses) { if (PersistUtils.isValidLDAPName(superiorClass, invalidReason)) { objectClasses.put(toLowerCase(superiorClass), superiorClass); } else { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_SUPERIOR_CLASS.get( type.getName(), superiorClass, invalidReason.toString())); } } if (superclassHandler != null) { for (final String s : superclassHandler.objectClassAttribute.getValues()) { objectClasses.put(toLowerCase(s), s); } } objectClassAttribute = new Attribute("objectClass", objectClasses.values()); final String parentDNStr = ldapObject.defaultParentDN(); try { defaultParentDN = new DN(parentDNStr); } catch (LDAPException le) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_DEFAULT_PARENT.get( type.getName(), parentDNStr, le.getMessage()), le); } final String postDecodeMethodName = ldapObject.postDecodeMethod(); if (postDecodeMethodName.length() > 0) { try { postDecodeMethod = type.getDeclaredMethod(postDecodeMethodName); postDecodeMethod.setAccessible(true); } catch (Exception e) { debugException(e); throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_POST_DECODE_METHOD.get( type.getName(), postDecodeMethodName, getExceptionMessage(e)), e); } } else { postDecodeMethod = null; } final String postEncodeMethodName = ldapObject.postEncodeMethod(); if (postEncodeMethodName.length() > 0) { try { postEncodeMethod = type.getDeclaredMethod(postEncodeMethodName, Entry.class); postEncodeMethod.setAccessible(true); } catch (Exception e) { debugException(e); throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_POST_ENCODE_METHOD.get( type.getName(), postEncodeMethodName, getExceptionMessage(e)), e); } } else { postEncodeMethod = null; } try { constructor = type.getDeclaredConstructor(); constructor.setAccessible(true); } catch (Exception e) { debugException(e); throw new LDAPPersistException( ERR_OBJECT_HANDLER_NO_DEFAULT_CONSTRUCTOR.get(type.getName()), e); } Field tmpDNField = null; Field tmpEntryField = null; final LinkedList<FieldInfo> tmpRFilterFields = new LinkedList<FieldInfo>(); final LinkedList<FieldInfo> tmpAAFilterFields = new LinkedList<FieldInfo>(); final LinkedList<FieldInfo> tmpCAFilterFields = new LinkedList<FieldInfo>(); final LinkedList<FieldInfo> tmpRDNFields = new LinkedList<FieldInfo>(); for (final Field f : type.getDeclaredFields()) { final LDAPField fieldAnnotation = f.getAnnotation(LDAPField.class); final LDAPDNField dnFieldAnnotation = f.getAnnotation(LDAPDNField.class); final LDAPEntryField entryFieldAnnotation = f.getAnnotation(LDAPEntryField.class); if (fieldAnnotation != null) { f.setAccessible(true); final FieldInfo fieldInfo = new FieldInfo(f, type); final String attrName = toLowerCase(fieldInfo.getAttributeName()); if (fields.containsKey(attrName)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_ATTR_CONFLICT.get(type.getName(), fieldInfo.getAttributeName())); } else { fields.put(attrName, fieldInfo); } switch (fieldInfo.getFilterUsage()) { case REQUIRED: tmpRFilterFields.add(fieldInfo); break; case ALWAYS_ALLOWED: tmpAAFilterFields.add(fieldInfo); break; case CONDITIONALLY_ALLOWED: tmpCAFilterFields.add(fieldInfo); break; case EXCLUDED: default: break; } if (fieldInfo.includeInRDN()) { tmpRDNFields.add(fieldInfo); } } if (dnFieldAnnotation != null) { f.setAccessible(true); if (fieldAnnotation != null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_CONFLICTING_FIELD_ANNOTATIONS.get( type.getName(), "LDAPField", "LDAPDNField", f.getName())); } if (tmpDNField != null) { throw new LDAPPersistException(ERR_OBJECT_HANDLER_MULTIPLE_DN_FIELDS.get(type.getName())); } final int modifiers = f.getModifiers(); if (Modifier.isFinal(modifiers)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_DN_FIELD_FINAL.get(f.getName(), type.getName())); } else if (Modifier.isStatic(modifiers)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_DN_FIELD_STATIC.get(f.getName(), type.getName())); } final Class<?> fieldType = f.getType(); if (fieldType.equals(String.class)) { tmpDNField = f; } else { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_DN_FIELD_TYPE.get( type.getName(), f.getName(), fieldType.getName())); } } if (entryFieldAnnotation != null) { f.setAccessible(true); if (fieldAnnotation != null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_CONFLICTING_FIELD_ANNOTATIONS.get( type.getName(), "LDAPField", "LDAPEntryField", f.getName())); } if (tmpEntryField != null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_MULTIPLE_ENTRY_FIELDS.get(type.getName())); } final int modifiers = f.getModifiers(); if (Modifier.isFinal(modifiers)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_ENTRY_FIELD_FINAL.get(f.getName(), type.getName())); } else if (Modifier.isStatic(modifiers)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_ENTRY_FIELD_STATIC.get(f.getName(), type.getName())); } final Class<?> fieldType = f.getType(); if (fieldType.equals(ReadOnlyEntry.class)) { tmpEntryField = f; } else { throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_ENTRY_FIELD_TYPE.get( type.getName(), f.getName(), fieldType.getName())); } } } dnField = tmpDNField; entryField = tmpEntryField; requiredFilterFields = Collections.unmodifiableList(tmpRFilterFields); alwaysAllowedFilterFields = Collections.unmodifiableList(tmpAAFilterFields); conditionallyAllowedFilterFields = Collections.unmodifiableList(tmpCAFilterFields); rdnFields = Collections.unmodifiableList(tmpRDNFields); final LinkedList<GetterInfo> tmpRFilterGetters = new LinkedList<GetterInfo>(); final LinkedList<GetterInfo> tmpAAFilterGetters = new LinkedList<GetterInfo>(); final LinkedList<GetterInfo> tmpCAFilterGetters = new LinkedList<GetterInfo>(); final LinkedList<GetterInfo> tmpRDNGetters = new LinkedList<GetterInfo>(); for (final Method m : type.getDeclaredMethods()) { final LDAPGetter getter = m.getAnnotation(LDAPGetter.class); final LDAPSetter setter = m.getAnnotation(LDAPSetter.class); if (getter != null) { m.setAccessible(true); if (setter != null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_CONFLICTING_METHOD_ANNOTATIONS.get( type.getName(), "LDAPGetter", "LDAPSetter", m.getName())); } final GetterInfo methodInfo = new GetterInfo(m, type); final String attrName = toLowerCase(methodInfo.getAttributeName()); if (fields.containsKey(attrName) || getters.containsKey(attrName)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_ATTR_CONFLICT.get(type.getName(), methodInfo.getAttributeName())); } else { getters.put(attrName, methodInfo); } switch (methodInfo.getFilterUsage()) { case REQUIRED: tmpRFilterGetters.add(methodInfo); break; case ALWAYS_ALLOWED: tmpAAFilterGetters.add(methodInfo); break; case CONDITIONALLY_ALLOWED: tmpCAFilterGetters.add(methodInfo); break; case EXCLUDED: default: // No action required. break; } if (methodInfo.includeInRDN()) { tmpRDNGetters.add(methodInfo); } } if (setter != null) { m.setAccessible(true); final SetterInfo methodInfo = new SetterInfo(m, type); final String attrName = toLowerCase(methodInfo.getAttributeName()); if (fields.containsKey(attrName) || setters.containsKey(attrName)) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_ATTR_CONFLICT.get(type.getName(), methodInfo.getAttributeName())); } else { setters.put(attrName, methodInfo); } } } requiredFilterGetters = Collections.unmodifiableList(tmpRFilterGetters); alwaysAllowedFilterGetters = Collections.unmodifiableList(tmpAAFilterGetters); conditionallyAllowedFilterGetters = Collections.unmodifiableList(tmpCAFilterGetters); rdnGetters = Collections.unmodifiableList(tmpRDNGetters); if (rdnFields.isEmpty() && rdnGetters.isEmpty()) { throw new LDAPPersistException(ERR_OBJECT_HANDLER_NO_RDN_DEFINED.get(type.getName())); } fieldMap = Collections.unmodifiableMap(fields); getterMap = Collections.unmodifiableMap(getters); setterMap = Collections.unmodifiableMap(setters); final TreeSet<String> attrSet = new TreeSet<String>(); final TreeSet<String> lazySet = new TreeSet<String>(); if (ldapObject.requestAllAttributes()) { attrSet.add("*"); attrSet.add("+"); } else { for (final FieldInfo i : fields.values()) { if (i.lazilyLoad()) { lazySet.add(i.getAttributeName()); } else { attrSet.add(i.getAttributeName()); } } for (final SetterInfo i : setters.values()) { attrSet.add(i.getAttributeName()); } } attributesToRequest = new String[attrSet.size()]; attrSet.toArray(attributesToRequest); lazilyLoadedAttributes = new String[lazySet.size()]; lazySet.toArray(lazilyLoadedAttributes); }
void doTest(String name) throws Exception { Method m = tests.get(name); Method m_check = tests.get(name + "_check"); Class[] paramTypes = m.getParameterTypes(); Object[] params = new Object[paramTypes.length]; Class retType = m.getReturnType(); boolean isIntArray = (retType.isPrimitive() && !retType.equals(Void.TYPE)) || (retType.equals(Void.TYPE) && paramTypes[0].getComponentType().isPrimitive()) || (retType.isArray() && retType.getComponentType().isPrimitive()); Args args = m.getAnnotation(Args.class); Object src = null; switch (args.src()) { case SMALL: { if (isIntArray) { src = small_int_src; } else { src = small_a_src; } break; } case LARGE: { if (isIntArray) { src = large_int_src; } else { src = large_a_src; } break; } case ZERO: { if (isIntArray) { src = zero_int_src; } else { src = zero_a_src; } break; } } for (int i = 0; i < 20000; i++) { boolean failure = false; int p = 0; if (params.length > 0) { if (isIntArray) { params[0] = ((int[]) src).clone(); } else { params[0] = ((A[]) src).clone(); } p++; } if (params.length > 1) { switch (args.dst()) { case NEW: { if (isIntArray) { params[1] = new int[((int[]) params[0]).length]; } else { params[1] = new A[((A[]) params[0]).length]; } p++; break; } case SRC: { params[1] = params[0]; p++; break; } case NONE: break; } } for (int j = 0; j < args.extra_args().length; j++) { params[p + j] = args.extra_args()[j]; } Object res = m.invoke(null, params); if (retType.isPrimitive() && !retType.equals(Void.TYPE)) { int s = (int) res; int sum = 0; int[] int_res = (int[]) src; for (int j = 0; j < int_res.length; j++) { sum += int_res[j]; } failure = (s != sum); if (failure) { System.out.println("Test " + name + " failed: result = " + s + " != " + sum); } } else { Object dest = null; if (!retType.equals(Void.TYPE)) { dest = res; } else { dest = params[1]; } if (m_check != null) { failure = (boolean) m_check.invoke(null, new Object[] {src, dest}); } else { if (isIntArray) { int[] int_res = (int[]) src; int[] int_dest = (int[]) dest; for (int j = 0; j < int_res.length; j++) { if (int_res[j] != int_dest[j]) { System.out.println( "Test " + name + " failed for " + j + " src[" + j + "]=" + int_res[j] + ", dest[" + j + "]=" + int_dest[j]); failure = true; } } } else { Object[] object_res = (Object[]) src; Object[] object_dest = (Object[]) dest; for (int j = 0; j < object_res.length; j++) { if (object_res[j] != object_dest[j]) { System.out.println( "Test " + name + " failed for " + j + " src[" + j + "]=" + object_res[j] + ", dest[" + j + "]=" + object_dest[j]); failure = true; } } } } } if (failure) { success = false; break; } } }
private String resolveFieldName(Method me, String finderName) { GenerateView g = me.getAnnotation(GenerateView.class); String field = g.field(); return field.length() == 0 ? finderName : g.field(); }
private boolean isTestNotApplicableInCurrentTestRun( Class<? extends Annotation> testAnnotation, Method testMethod) { return (testAnnotation == null || testMethod.getAnnotation(testAnnotation) != null) && new TestFilter(coverageMap).shouldIgnoreTestInCurrentTestRun(testMethod); }