@Mock annotation expanded to local variables

969 views
Skip to first unread message

BenjaminPLee

unread,
Jan 8, 2010, 3:43:03 PM1/8/10
to mockito, benjam...@asolutions.com
All,

Has there been any thought toward having the @Mock annotation expanded
to also work with local variable declarations?

I have used Mockito on a few projects now and think this would be a
nice little feature to go along with the existing @Mock annotation.
This would be my first crack at processing annotations but I am
considering implementing this feature.

If anyone has any suggestions or gotchas I should look for, please
send them my way.

Thanks and thanks for a great library,

Benjamin Lee

szczepiq

unread,
Jan 8, 2010, 6:04:28 PM1/8/10
to moc...@googlegroups.com
Sure, give it a go!

As far as I understand annotations, your idea is not implementable.
However, you might try with parameters passed to methods instead of
local variables.

Cheers,
Szczepan Faber

> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To post to this group, send email to moc...@googlegroups.com.
> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
>
>
>
>

Mahmood Ali

unread,
Jan 9, 2010, 12:12:04 AM1/9/10
to moc...@googlegroups.com
Greetings Benjamin,

>> Has there been any thought toward having the @Mock annotation expanded
>> to also work with local variable declarations?
>

> As far as I understand annotations, your idea is not implementable.
> However, you might try with parameters passed to methods instead of
> local variables.

It's actually possible but with a bit of work.

There are many technical challenges (that I would be happy to share
with you if you are interested).

As a pointer though, I would look into Project Lombok. They provide
limited support for tree manipulation extensions. Look for
implementations of Cleanup handler in the source (for both javac and
eclipse), as a hint for your implementation.

The basic idea is when the annotation processor gets invoked, method
bodies aren't type checked yet. The annotation processor would cast
the public interface to the concrete underlying implementation. Then
you would manipulate the tree itself, before type-checking occur.
Annotation processors need to write hooks for each compiler (because
each uses its own AST).

Regards,
Mahmood

Reply all
Reply to author
Forward
0 new messages