Any method to calculate constant operations?


There is a lot of codes using our old platform need to be adapted to our new platform. So, I have to make out a plugin to help developers to simplify this work.

There is a requirement that make the annotations on method from the old style to the new style:
OLD:
@Location("/sys/getUser")
NEW:
@Uri("cmbus:///sys/getUser")
This is quite easy. I just scan the method and add new annotations on those method.
The old value can be achieved by calling PsiLiteralExpression.getValue().
The perfix-value is added by:
 
private PsiExpression getExpressionFromText(String value, PsiAnnotation annotation) {
    return JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText("\"" + PREFIX + value + "\"",
            annotation);
}


But there are some cases making the situation quite difficult. E.g.:
OLD:
private static final String BASE_LOCATION = "/mgr/mer"; // in some other class
@Location(BASE_LOCATION+"/addCer")
NEW:
@Uri("cmbus://"+BASE_LOCATION+"/addCer")
OLD:
private static final Integer TOP_USER_LIMIT=10 //in some other class
private static final Integer TOP_GUEST_LIMIT=10 //in some other class
@Location({"/sys/topUser?limit="+TOP_USER_LIMIT, /sys/topGuest?limit="+TOP_GUEST_LIMIT})
NEW:
@Uri({"cmbus:///sys/topUser?limit="+TOP_USER_LIMIT, cmbus:///sys/topGuest?limit="+TOP_GUEST_LIMIT})


So, I can't get the value directly, and don't know how to generate the PsiExpression in these situations.

Is there any method to calculate these constants, so I can put the target values into the annotations easily?
2 comments
Comment actions Permalink

Hi Meilun,

There's an intention that determines the value of String concatenation: CopyConcatenatedStringToClipboardIntention.

You may be able to use the logic as a starting point.

It will even work if there are values like true or 4 in the concatenation.

https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/CopyConcatenatedStringToClipboardIntention.java

0
Comment actions Permalink

Thanks, it looks very helpful.

0

Please sign in to leave a comment.