@SuppressWarnings("unused") public static boolean doIdentityEquals(@This Object thiz, @Argument(0) Object other) { return thiz == other; } }
/** * {@inheritDoc} */ public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<Argument> annotation, MethodDescription source, ParameterDescription target, Implementation.Target implementationTarget, Assigner assigner, Assigner.Typing typing) { Argument argument = annotation.loadSilent(); if (argument.value() < 0) { throw new IllegalArgumentException("@Argument annotation on " + target + " specifies negative index"); } else if (source.getParameters().size() <= argument.value()) { return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE; } return argument.bindingMechanic().makeBinding(source.getParameters().get(argument.value()).getType(), target.getType(), argument.value(), assigner, typing, source.getParameters().get(argument.value()).getOffset()); } }
@Override public boolean equals(Object other) { return this == other || other instanceof Argument && parameterIndex == ((Argument) other).value(); }
@SuppressWarnings("unused") public static void doReadObject(@Identifier String identifier, @This MockAccess thiz, @Argument(0) ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); MockMethodAdvice mockMethodAdvice = (MockMethodAdvice) MockMethodDispatcher.get(identifier, thiz); if (mockMethodAdvice != null) { mockMethodAdvice.interceptors.put(thiz, thiz.getMockitoInterceptor()); } } }
public static boolean intercept(@This Object self, @Argument(0) Object other) { if (null == other) { return false; } //we are being strict about class equality final Class<?> selfClass = self.getClass(); final Class<?> otherClass = other.getClass(); if (selfClass != otherClass) { return false; } //take advantage of HashMap's built-in equals method try { return FieldUtil.toMap(self, selfClass.getDeclaredFields()) .equals(FieldUtil.toMap(other, other.getClass().getDeclaredFields())); } catch (IllegalAccessException e) { return false; //this it not a good solution, but should never happen because fields are accessible } } }
/** * @param method * @param id * @return * @throws Throwable */ public static Model findById(@Origin Method method, @Argument(0) Serializable id) throws Throwable { return invoke(method, new Class[] {Class.class, Serializable.class}, new Object[]{method.getDeclaringClass(), id}); }
/** * @param method * @param id * @return * @throws Throwable */ public static boolean exists(@Origin Method method, @Argument(0) Serializable id) throws Throwable { return invoke(method, new Class[] {Class.class, Serializable.class}, new Object[]{method.getDeclaringClass(), id}); }
/** * @param method * @param filter * @return * @throws Throwable */ public static long count(@Origin Method method, @Argument(0) Filter filter) throws Throwable { return invoke(method, new Class[] {Class.class, Filter.class}, new Object[]{method.getDeclaringClass(), filter}); }
/** * @param method * @param paramValues * @return * @throws Throwable */ public static Model one(@Origin Method method, @Argument(0) Object[] paramValues) throws Throwable { return invoke(method, new Class[] {Class.class, Object[].class}, new Object[]{method.getDeclaringClass(), paramValues}); }
/** * @param method * @param paramValues * @return * @throws Throwable */ public static Model first(@Origin Method method, @Argument(0) Object[] paramValues) throws Throwable { return invoke(method, new Class[] {Class.class, Object[].class}, new Object[]{method.getDeclaringClass(), paramValues}); }
/** * @param method * @param filter * @return * @throws Throwable */ public static List<Model> where(@Origin Method method, @Argument(0) Filter filter) throws Throwable { return invoke(method, new Class[] {Class.class, Filter.class}, new Object[]{method.getDeclaringClass(), filter}); }
/** * @param method * @param paramValues * @return * @throws Throwable */ public static List<Model> where(@Origin Method method, @Argument(0) Object[] paramValues) throws Throwable { return invoke(method, new Class[] {Class.class, Object[].class}, new Object[]{method.getDeclaringClass(), paramValues}); }
/** * @param method * @param filter * @throws Throwable */ public static void deleteAll(@Origin Method method, @Argument(0) Filter filter) throws Throwable { invoke(method, new Class[] {Class.class, Filter.class}, new Object[]{method.getDeclaringClass(), filter}); }
@RuntimeType public static Object intercept(@This Object thiz, @Argument(0) Object other, @FieldValue("bean") Object bean) throws IllegalAccessException, NoSuchFieldException { if (thiz == other) { return true; } else if (other instanceof EnhancedProxy) { return bean.equals(ReflectionUtils.getValue(other, "bean")); } else { return bean.equals(other); } } }
@RuntimeType public static void setProperty(@This final ElementFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final Object obj) { assert thiz instanceof CachesReflection; final Property annotation = ((CachesReflection) thiz).getReflectionCache().getAnnotation(method, Property.class); final String value = annotation.value(); if ((obj != null) && (obj.getClass().isEnum())) thiz.setProperty(value, ((Enum<?>) obj).name()); else thiz.setProperty(value, obj); } }
@RuntimeType public static void setProperty(@This final ElementFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final Object obj) { assert thiz instanceof CachesReflection; final Property annotation = ((CachesReflection) thiz).getReflectionCache().getAnnotation(method, Property.class); final String value = annotation.value(); if ((obj != null) && (obj.getClass().isEnum())) thiz.setProperty(value, ((Enum<?>) obj).name()); else thiz.setProperty(value, obj); } }
@RuntimeType public static Object addVertex(@This final VertexFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final VertexFrame newVertex, @RuntimeType @Argument(1) final ClassInitializer edgeType) { assert thiz instanceof CachesReflection; final Incidence annotation = ((CachesReflection) thiz).getReflectionCache().getAnnotation(method, Incidence.class); final Direction direction = annotation.direction(); final String label = annotation.label(); switch (direction) { case BOTH: throw new IllegalStateException(method.getName() + " is annotated with direction BOTH, this is not allowed for add methods annotated with @Incidence."); case IN: return thiz.getGraph().addFramedEdge(newVertex, thiz, label, edgeType); case OUT: return thiz.getGraph().addFramedEdge(thiz, newVertex, label, edgeType); default: throw new IllegalStateException(method.getName() + " is annotated with a direction other than BOTH, IN, or OUT."); } } }
@RuntimeType public static Object addVertex(@This final VertexFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final VertexFrame newVertex, @RuntimeType @Argument(1) final ClassInitializer edgeType) { assert thiz instanceof CachesReflection; final Incidence annotation = ((CachesReflection) thiz).getReflectionCache().getAnnotation(method, Incidence.class); final Direction direction = annotation.direction(); final String label = annotation.label(); switch (direction) { case BOTH: throw new IllegalStateException(method.getName() + " is annotated with direction BOTH, this is not allowed for add methods annotated with @Incidence."); case IN: return thiz.getGraph().addFramedEdge(newVertex, thiz, label, edgeType); case OUT: return thiz.getGraph().addFramedEdge(thiz, newVertex, label, edgeType); default: throw new IllegalStateException(method.getName() + " is annotated with a direction other than BOTH, IN, or OUT."); } } }
@RuntimeType public static void removeEdge(@This final VertexFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final EdgeFrame edge) { edge.remove(); } }
@RuntimeType public static void removeEdge(@This final VertexFrame thiz, @Origin final Method method, @RuntimeType @Argument(0) final EdgeFrame edge) { edge.remove(); } }