Structural replace to make fields with specific annotation have default visibility

Answered

I wrote this SSR:

@org.mockito.Mock @Modifier("Instance") $FieldType$ $Field$ = $Init$;

with $Field$ having a script on it: 

!__context__.hasModifierProperty(com.intellij.psi.PsiModifier.PACKAGE_LOCAL)

and replacement

@org.mockito.Mock $FieldType$ $Field$ = $Init$;

My tests are:

@Mock private String f1;
private @Mock String f2;
@Mock protected String f3;
protected @Mock String f4;
@Mock String f5;
@Mock public String f6;
public @Mock String f7;
@Mock private String f1_init = "asdf";
private @Mock String f2_init = "asdf";
@Mock protected String f3_init = "asdf";
protected @Mock String f4_init = "asdf";
@Mock String f5_init = "asdf";
@Mock public String f6_init = "asdf";
public @Mock String f7_init = "asdf";
public @Mock static String ignoredField;
@SuppressWarnings("deprecation")
public @Mock String suppressedField = "asdf";
public @Mock volatile String volatileField = "asdf";
@Mock(name = "hello") public String named;
private @Mock(answer = Answers.CALLS_REAL_METHODS, name = "real") String real;

Expected output:

@Mock String f1;
@Mock String f2;
@Mock String f3;
@Mock String f4;
@Mock String f5;
@Mock String f6;
@Mock String f7;
@Mock String f1_init = "asdf";
@Mock String f2_init = "asdf";
@Mock String f3_init = "asdf";
@Mock String f4_init = "asdf";
@Mock String f5_init = "asdf";
@Mock String f6_init = "asdf";
@Mock String f7_init = "asdf";
public @Mock static String ignoredField;
@SuppressWarnings("deprecation")
@Mock String suppressedField = "asdf";
@Mock volatile String volatileField = "asdf";
@Mock(name = "hello") String named;
@Mock(answer = Answers.CALLS_REAL_METHODS, name = "real") String real;

actual output clears the extra @SuppressWarnings annotation and the volatile modifier, and the mock arguments.

How can I capture those unknown extra modifiers (0..∞), annotations (0..∞) and @Mock annotation params to keep them in replacement? (any of the above 3 would be an improvement)

To dig to the root cause of this question note that it's not possible to use @Modifier("packageLocal") in the replacement string (2017.1 EAP).

Please sign in to leave a comment.