use of org.checkerframework.javacutil.Resolver in project checker-framework by typetools.
the class FlowExpressionParseUtil method parseMemberSelect.
private static Receiver parseMemberSelect(String s, ProcessingEnvironment env, FlowExpressionContext context, TreePath path) throws FlowExpressionParseException {
Pair<String, String> select = parseMemberSelect(s);
assert select != null : "isMemberSelect must be called first";
Receiver receiver;
String memberSelected;
Resolver resolver = new Resolver(env);
// Attempt to match a package and class name first.
Pair<ClassName, String> classAndRemainingString = matchPackageAndClassNameWithinExpression(s, resolver, path);
if (classAndRemainingString != null) {
receiver = classAndRemainingString.first;
memberSelected = classAndRemainingString.second;
if (memberSelected == null) {
throw constructParserException(s, "a class cannot terminate a flow expression string");
}
} else {
String receiverString = select.first;
memberSelected = select.second;
receiver = parseHelper(receiverString, context, path);
}
if (memberSelected.equals("class")) {
if (receiver instanceof FlowExpressions.ClassName && !context.parsingMember) {
return receiver;
} else {
throw constructParserException(s, "class is not a legal identifier");
}
}
// Parse the rest, with a new receiver.
FlowExpressionContext newContext = context.copyChangeToParsingMemberOfReceiver(receiver);
return parseHelper(memberSelected, newContext, path);
}
use of org.checkerframework.javacutil.Resolver in project checker-framework by typetools.
the class FlowExpressionParseUtil method parseIdentifier.
private static Receiver parseIdentifier(String s, ProcessingEnvironment env, TreePath path, FlowExpressionContext context) throws FlowExpressionParseException {
Resolver resolver = new Resolver(env);
if (!context.parsingMember && context.useLocalScope) {
// Attempt to match a local variable within the scope of the
// given path before attempting to match a field.
VariableElement varElem = resolver.findLocalVariableOrParameterOrField(s, path);
if (varElem != null) {
if (varElem.getKind() == ElementKind.FIELD) {
boolean isOriginalReceiver = context.receiver instanceof ThisReference;
return getReceiverField(s, context, isOriginalReceiver, varElem);
} else {
return new LocalVariable(varElem);
}
}
}
// field access
TypeMirror receiverType = context.receiver.getType();
boolean originalReceiver = true;
VariableElement fieldElem = null;
if (receiverType.getKind() == TypeKind.ARRAY && s.equals("length")) {
fieldElem = resolver.findField(s, receiverType, path);
}
// Search for field in each enclosing class.
while (receiverType.getKind() == TypeKind.DECLARED) {
fieldElem = resolver.findField(s, receiverType, path);
if (fieldElem != null) {
break;
}
receiverType = getTypeOfEnclosingClass((DeclaredType) receiverType);
originalReceiver = false;
}
if (fieldElem != null && fieldElem.getKind() == ElementKind.FIELD) {
return getReceiverField(s, context, originalReceiver, fieldElem);
}
// Class name
Element classElem = resolver.findClass(s, path);
TypeMirror classType = ElementUtils.getType(classElem);
if (classType != null) {
return new ClassName(classType);
}
throw constructParserException(s, "identifier not found");
}
use of org.checkerframework.javacutil.Resolver in project checker-framework by typetools.
the class FlowExpressionParseUtil method parseMethod.
private static Receiver parseMethod(String s, FlowExpressionContext context, TreePath path, ProcessingEnvironment env) throws FlowExpressionParseException {
Pair<Pair<String, String>, String> method = parseMethod(s);
if (method == null) {
return null;
}
String methodName = method.first.first;
// parse parameter list
String parameterList = method.first.second;
List<Receiver> parameters = ParameterListParser.parseParameterList(parameterList, true, context.copyAndUseOuterReceiver(), path);
// get types for parameters
List<TypeMirror> parameterTypes = new ArrayList<>();
for (Receiver p : parameters) {
parameterTypes.add(p.getType());
}
ExecutableElement methodElement = null;
try {
Element element = null;
// try to find the correct method
Resolver resolver = new Resolver(env);
TypeMirror receiverType = context.receiver.getType();
if (receiverType.getKind() == TypeKind.ARRAY) {
element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
}
// Search for method in each enclosing class.
while (receiverType.getKind() == TypeKind.DECLARED) {
element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
if (element.getKind() == ElementKind.METHOD) {
break;
}
receiverType = getTypeOfEnclosingClass((DeclaredType) receiverType);
}
if (element == null) {
throw constructParserException(s, "element==null");
}
if (element.getKind() != ElementKind.METHOD) {
throw constructParserException(s, "element.getKind()==" + element.getKind());
}
methodElement = (ExecutableElement) element;
for (int i = 0; i < parameters.size(); i++) {
VariableElement formal = methodElement.getParameters().get(i);
TypeMirror formalType = formal.asType();
Receiver actual = parameters.get(i);
TypeMirror actualType = actual.getType();
// boxing necessary
if (TypesUtils.isBoxedPrimitive(formalType) && TypesUtils.isPrimitive(actualType)) {
MethodSymbol valueOfMethod = TreeBuilder.getValueOfMethod(env, formalType);
List<Receiver> p = new ArrayList<>();
p.add(actual);
Receiver boxedParam = new MethodCall(formalType, valueOfMethod, new ClassName(formalType), p);
parameters.set(i, boxedParam);
}
}
} catch (Throwable t) {
throw constructParserException(s, t);
}
assert methodElement != null;
/*if (!PurityUtils.isDeterministic(context.checkerContext.getAnnotationProvider(),
methodElement)) {
throw new FlowExpressionParseException(Result.failure(
"flowexpr.method.not.deterministic",
methodElement.getSimpleName()));
}*/
if (ElementUtils.isStatic(methodElement)) {
Element classElem = methodElement.getEnclosingElement();
Receiver staticClassReceiver = new ClassName(ElementUtils.getType(classElem));
return new MethodCall(ElementUtils.getType(methodElement), methodElement, staticClassReceiver, parameters);
} else {
if (context.receiver instanceof ClassName) {
throw constructParserException(s, "a non-static method call cannot have a class name as a receiver.");
}
TypeMirror methodType = TypesUtils.substituteMethodReturnType(methodElement, context.receiver.getType(), env);
return new MethodCall(methodType, methodElement, context.receiver, parameters);
}
}
Aggregations