in uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java [1721:1868]
public void visit(MethodDeclaration n, Object ignore) {
String name = n.getNameAsString();
isGetter = isArraySetter = false;
do { // to provide break exit
if (name.length() >= 4 && ((isGetter = name.startsWith("get")) || name.startsWith("set"))
&& Character.isUpperCase(name.charAt(3)) && !name.equals("getTypeIndexID")) {
List<Parameter> ps = n.getParameters();
if (isGetter) {
if (ps.size() > 1) {
break;
}
} else { // is setter
if (ps.size() > 2 || ps.size() == 0) {
break;
}
if (ps.size() == 2) {
if (!getParmTypeName(ps, 0).equals("int")) {
break;
}
isArraySetter = true;
}
}
// get the range-part-name and convert to v3 range ("Ref" changes to "Feature")
String bodyString = n.getBody().get().toString(printWithoutComments);
int i = bodyString.indexOf("jcasType.ll_cas.ll_");
if (i < 0) {
break;
}
String s = bodyString.substring(i + "jcasType.ll_cas.ll_get".length()); // also for
// ...ll_set - same
// length!
if (s.startsWith("FSForRef(")) { // then it's the wrapper and the wrong instance.
i = s.indexOf("jcasType.ll_cas.ll_");
if (i < 0) {
reportUnrecognizedV2Code(
"Found \"jcasType.ll_cas.ll_[set or get]...FSForRef(\" but didn't find following \"jcasType.ll_cas_ll_\"\n"
+ n.toString());
break;
}
s = s.substring(i + "jcasType.ll_cas.ll_get".length());
}
i = s.indexOf("Value");
if (i < 0) {
reportUnrecognizedV2Code(
"Found \"jcasType.ll_cas.ll_[set or get]\" but didn't find following \"Value\"\n"
+ n.toString());
break; // give up
}
s = Character.toUpperCase(s.charAt(0)) + s.substring(1, i);
rangeNameV2Part = s;
rangeNamePart = s.equals("Ref") ? "Feature" : s;
// get feat name following ")jcasType).casFeatCode_xxxxx,
i = bodyString.indexOf("jcasType).casFeatCode_");
if (i == -1) {
reportUnrecognizedV2Code("Didn't find \"...jcasType).casFeatCode_\"\n" + n.toString());
break;
}
Matcher m = word1.matcher(bodyString.substring(i + "jcasType).casFeatCode_".length()));
if (!m.find()) {
reportUnrecognizedV2Code(
"Found \"...jcasType).casFeatCode_\" but didn't find subsequent word\n"
+ n.toString());
break;
}
featName = m.group(1);
String fromMethod = Character.toLowerCase(name.charAt(3)) + name.substring(4);
if (!featName.equals(fromMethod)) {
// don't report if the only difference is the first letter captialization
if (!(Character.toLowerCase(featName.charAt(0)) + featName.substring(1))
.equals(fromMethod)) {
reportMismatchedFeatureName(String.format("%-25s %s", featName, name));
}
}
// add _FI_xxx = TypeSystemImpl.getAdjustedFeatureOffset("xxx");
// replaced Sept 2017
// NodeList<Expression> args = new NodeList<>();
// args.add(new StringLiteralExpr(featName));
// VariableDeclarator vd = new VariableDeclarator(
// intType,
// "_FI_" + featName,
// new MethodCallExpr(new NameExpr("TypeSystemImpl"), new
// SimpleName("getAdjustedFeatureOffset"), args));
// if (featNames.add(featName)) { // returns true if it was added, false if already in the
// set of featNames
// fi_fields.add(new FieldDeclaration(public_static_final, vd));
// }
// add _FC_xxx = TypeSystemImpl.createCallSite(ccc.class, "xxx");
// add _FH_xxx = _FC_xxx.dynamicInvoker();
// add _FeatName_xxx = "xxx" // https://issues.apache.org/jira/browse/UIMA-5575
if (featNames.add(featName)) { // returns true if it was added, false if already in the set
// of featNames
// _FC_xxx = TypeSystemImpl.createCallSite(ccc.class, "xxx");
MethodCallExpr initCallSite = new MethodCallExpr(new NameExpr("TypeSystemImpl"),
"createCallSite");
initCallSite.addArgument(new FieldAccessExpr(new NameExpr(className), "class"));
initCallSite.addArgument(new StringLiteralExpr(featName));
VariableDeclarator vd_FC = new VariableDeclarator(callSiteType, "_FC_" + featName,
initCallSite);
fi_fields.add(new FieldDeclaration(private_static_final, vd_FC));
// _FH_xxx = _FC_xxx.dynamicInvoker();
MethodCallExpr initInvoker = new MethodCallExpr(new NameExpr(vd_FC.getName()),
"dynamicInvoker");
VariableDeclarator vd_FH = new VariableDeclarator(methodHandleType, "_FH_" + featName,
initInvoker);
fi_fields.add(new FieldDeclaration(private_static_final, vd_FH));
// _FeatName_xxx = "xxx" // https://issues.apache.org/jira/browse/UIMA-5575
VariableDeclarator vd_fn = new VariableDeclarator(stringType, "_FeatName_" + featName,
new StringLiteralExpr(featName));
fi_fields.add(new FieldDeclaration(public_static_final, vd_fn));
}
/**
* add missing cast stmt for return stmts where the value being returned: - doesn't have a
* cast already - has the expression be a methodCallExpr with a name which looks like:
* ll_getRefValue or ll_getRefArrayValue
*/
if (isGetter && "Feature".equals(rangeNamePart)) {
for (Statement stmt : n.getBody().get().getStatements()) {
if (stmt instanceof ReturnStmt) {
Expression e = getUnenclosedExpr(((ReturnStmt) stmt).getExpression().get());
if ((e instanceof MethodCallExpr)) {
String methodName = ((MethodCallExpr) e).getNameAsString();
if (refGetter.matcher(methodName).matches()) { // ll_getRefValue or
// ll_getRefArrayValue
addCastExpr(stmt, n.getType());
}
}
}
}
}
get_set_method = n; // used as a flag during inner "visits" to signal
// we're inside a likely feature setter/getter
} // end of test for getter or setter method
} while (false); // do once, provide break exit
super.visit(n, ignore);
get_set_method = null; // after visiting, reset the get_set_method to null
}