下面列出了org.eclipse.lsp4j.ParameterInformation#org.codehaus.groovy.ast.expr.Expression 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void visitDeclarationExpression(final DeclarationExpression expression) {
Expression rightExpression = expression.getRightExpression();
rightExpression.visit(this);
ClassNode leftType = typeChooser.resolveType(expression.getLeftExpression(), node);
ClassNode rightType = optimizeDivWithIntOrLongTarget(rightExpression, leftType);
if (rightType == null) rightType = typeChooser.resolveType(rightExpression, node);
if (isPrimitiveType(leftType) && isPrimitiveType(rightType)) {
// if right is a constant, then we optimize only if it makes a block complete, so we set a maybe
if (rightExpression instanceof ConstantExpression) {
opt.chainCanOptimize(true);
} else {
opt.chainShouldOptimize(true);
}
addMeta(expression).type = Optional.ofNullable(typeChooser.resolveType(expression, node)).orElse(leftType);
opt.chainInvolvedType(leftType);
opt.chainInvolvedType(rightType);
}
}
/**
* This will return an initial guess as to the string representation of the parent parent object,
* based solely on the method callstack hierarchy. Any direct property or variable parents should
* be resolved by using the getValidStringRepresentation function.
*/
private String getParentParent() {
for (int i = methodCallStack.size() - 2; i >= 0; i--) {
MethodCallExpression expression = methodCallStack.get(i);
Expression arguments = expression.getArguments();
if (arguments instanceof ArgumentListExpression) {
ArgumentListExpression ale = (ArgumentListExpression) arguments;
List<Expression> expressions = ale.getExpressions();
if (expressions.size() == 1 && expressions.get(0) instanceof ClosureExpression) {
return expression.getMethodAsString();
}
}
}
return null;
}
private void createListenerSetter(SourceUnit source, boolean bindable, ClassNode declaringClass, PropertyNode propertyNode) {
if (bindable && needsPropertyChangeSupport(declaringClass, source)) {
addPropertyChangeSupport(declaringClass);
}
if (needsVetoableChangeSupport(declaringClass, source)) {
addVetoableChangeSupport(declaringClass);
}
String setterName = getSetterName(propertyNode.getName());
if (declaringClass.getMethods(setterName).isEmpty()) {
Expression fieldExpression = fieldX(propertyNode.getField());
BlockStatement setterBlock = new BlockStatement();
setterBlock.addStatement(createConstrainedStatement(propertyNode, fieldExpression));
if (bindable) {
setterBlock.addStatement(createBindableStatement(propertyNode, fieldExpression));
} else {
setterBlock.addStatement(createSetStatement(fieldExpression));
}
// create method void <setter>(<type> fieldName)
createSetterMethod(declaringClass, propertyNode, setterName, setterBlock);
} else {
wrapSetterMethod(declaringClass, bindable, propertyNode.getName());
}
}
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
proposals = completionComputer.computeCompletionProposals(context, monitor);
final BinaryExpression binaryExpression = (BinaryExpression) contentAssistContext.getPerceivedCompletionNode();
final Expression leftExpression = binaryExpression.getLeftExpression();
String multipleInputName = null;
if (leftExpression instanceof PropertyExpression) {
final PropertyExpression propertyExpr = (PropertyExpression) leftExpression;
final Expression objectExpression = propertyExpr.getProperty();
multipleInputName = objectExpression.getText();
} else if (leftExpression instanceof VariableExpression) {
multipleInputName = ((VariableExpression) leftExpression).getName();
}
if (multipleInputName != null) {
final ContractInput multipleInput = getInputWithName(multipleInputName, inputs);
final ContractInput copy = EcoreUtil.copy(multipleInput);
copy.setMultiple(false);
final String fullyQualifiedType = ExpressionHelper.getContractInputReturnType(copy);
if (fullyQualifiedType != null && prefix.isEmpty()) {
proposals = getMethodProposals(contentAssistContext, context, inputs, prefix, multipleInputName, fullyQualifiedType);
}
}
}
private ConstructorCallExpression getConstructorCallExpression(
ConstructorNode constructorNode) {
Statement code = constructorNode.getCode();
if (!(code instanceof BlockStatement)) {
return null;
}
BlockStatement block = (BlockStatement) code;
List<Statement> stats = block.getStatements();
if (stats == null || stats.isEmpty()) {
return null;
}
Statement stat = stats.get(0);
if (!(stat instanceof ExpressionStatement)) {
return null;
}
Expression expr = ((ExpressionStatement) stat).getExpression();
if (!(expr instanceof ConstructorCallExpression)) {
return null;
}
return (ConstructorCallExpression) expr;
}
@Override
protected void writePostOrPrefixMethod(final int op, final String method, final Expression expression, final Expression orig) {
MethodNode mn = orig.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
if (mn != null) {
controller.getOperandStack().pop();
MethodCallExpression call = callX(expression, method);
call.setMethodTarget(mn);
call.visit(controller.getAcg());
return;
}
ClassNode top = controller.getOperandStack().getTopOperand();
if (ClassHelper.isPrimitiveType(top) && (ClassHelper.isNumberType(top) || char_TYPE.equals(top))) {
MethodVisitor mv = controller.getMethodVisitor();
visitInsnByType(top, mv, ICONST_1, LCONST_1, FCONST_1, DCONST_1);
if ("next".equals(method)) {
visitInsnByType(top, mv, IADD, LADD, FADD, DADD);
} else {
visitInsnByType(top, mv, ISUB, LSUB, FSUB, DSUB);
}
return;
}
super.writePostOrPrefixMethod(op, method, expression, orig);
}
public void visitField(FieldNode node) {
if (sameVariableName(leaf, node)) {
if (node.hasInitialExpression()){
Expression expression = node.getInitialExpression();
if (expression instanceof ConstantExpression
&& !expression.getText().equals("null")) { // NOI18N
guessedType = ((ConstantExpression) expression).getType();
} else if (expression instanceof ConstructorCallExpression) {
guessedType = ((ConstructorCallExpression) expression).getType();
} else if (expression instanceof MethodCallExpression) {
int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
AstPath newPath = new AstPath(path.root(), newOffset, doc);
guessedType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
}
}
}
}
private Statement createConstructorStatementCollection(FieldNode fNode, Parameter namedArgsMap, boolean shouldNullCheck) {
final Expression fieldExpr = propX(varX("this"), fNode.getName());
ClassNode fieldType = fieldExpr.getType();
Expression param = getParam(fNode, namedArgsMap != null);
Statement assignStmt = ifElseS(
isInstanceOfX(param, CLONEABLE_TYPE),
assignS(fieldExpr, cloneCollectionExpr(cloneArrayOrCloneableExpr(param, fieldType), fieldType)),
assignS(fieldExpr, cloneCollectionExpr(param, fieldType)));
assignStmt = ifElseS(
equalsNullX(param),
shouldNullCheck ? NullCheckASTTransformation.makeThrowStmt(fNode.getName()) : assignNullS(fieldExpr),
assignStmt);
Expression initExpr = fNode.getInitialValueExpression();
final Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
assignInit = shouldNullCheck ? NullCheckASTTransformation.makeThrowStmt(fNode.getName()) : assignNullS(fieldExpr);
} else {
assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
}
return assignFieldWithDefault(namedArgsMap, fNode, assignStmt, assignInit);
}
@Override
public void visitMethodCallExpression(final MethodCallExpression call) {
boolean old = inBuilderMethod;
inBuilderMethod = false;
if (call.isImplicitThis() && call.getArguments() instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
if (!expressions.isEmpty()) {
Expression lastArg = expressions.get(expressions.size() - 1);
if (lastArg instanceof ClosureExpression) {
call.getObjectExpression().visit(this);
call.getMethod().visit(this);
for (Expression expression : expressions) {
inBuilderMethod = (expression == lastArg);
expression.visit(this);
}
}
}
} else {
super.visitMethodCallExpression(call);
}
inBuilderMethod = old;
}
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
boolean assignment = StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType());
boolean isDeclaration = expression instanceof DeclarationExpression;
Expression leftExpression = expression.getLeftExpression();
Expression rightExpression = expression.getRightExpression();
if (isDeclaration) {
recordFinalVars(leftExpression);
}
// visit RHS first for expressions like a = b = 0
inAssignmentRHS = assignment;
rightExpression.visit(this);
inAssignmentRHS = false;
leftExpression.visit(this);
if (assignment) {
recordAssignments(expression, isDeclaration, leftExpression, rightExpression);
}
}
private boolean doAssignmentToLocalVariable(final String method, final BinaryExpression binExp) {
Expression left = binExp.getLeftExpression();
if (left instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) left;
Variable v = ve.getAccessedVariable();
if (v instanceof DynamicVariable) return false;
if (v instanceof PropertyExpression) return false;
/* field and declaration we don't return false */
} else {
return false;
}
evaluateBinaryExpression(method, binExp);
controller.getOperandStack().dup();
controller.getCompileStack().pushLHS(true);
binExp.getLeftExpression().visit(controller.getAcg());
controller.getCompileStack().popLHS();
return true;
}
private void visitAnnotationArrayElement(final Expression expr, final int arrayElementType, final AnnotationVisitor av) {
switch (arrayElementType) {
case 1:
AnnotationNode atAttr = (AnnotationNode) ((AnnotationConstantExpression) expr).getValue();
AnnotationVisitor av2 = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(atAttr.getClassNode()));
visitAnnotationAttributes(atAttr, av2);
av2.visitEnd();
break;
case 2:
av.visit(null, ((ConstantExpression) expr).getValue());
break;
case 3:
av.visit(null, Type.getType(BytecodeHelper.getTypeDescription(expr.getType())));
break;
case 4:
PropertyExpression propExpr = (PropertyExpression) expr;
av.visitEnum(null,
BytecodeHelper.getTypeDescription(propExpr.getObjectExpression().getType()),
String.valueOf(((ConstantExpression) propExpr.getProperty()).getValue()));
break;
}
}
protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) {
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
gStringStrings.add(new ConstantExpression(""));
gStringStrings.add(new ConstantExpression(""));
List<Expression> gStringValues = new ArrayList<Expression>();
gStringValues.add(new VariableExpression(parameters[0]));
block.addStatement(
new ReturnStatement(
new MethodCallExpression(
thiz,
new GStringExpression("$name", gStringStrings, gStringValues),
new ArgumentListExpression(
new SpreadExpression(new VariableExpression(parameters[1]))
)
)
)
);
}
static void visitField(ErrorCollecting xform, AnnotationNode node, FieldNode fieldNode) {
final Expression soft = node.getMember("soft");
final Expression init = getInitExpr(xform, fieldNode);
String backingFieldName = "$" + fieldNode.getName();
fieldNode.rename(backingFieldName);
fieldNode.setModifiers(ACC_PRIVATE | ACC_SYNTHETIC | (fieldNode.getModifiers() & (~(ACC_PUBLIC | ACC_PROTECTED))));
PropertyNode pNode = fieldNode.getDeclaringClass().getProperty(backingFieldName);
if (pNode != null) {
fieldNode.getDeclaringClass().getProperties().remove(pNode);
}
if (soft instanceof ConstantExpression && ((ConstantExpression) soft).getValue().equals(true)) {
createSoft(fieldNode, init);
} else {
create(fieldNode, init);
// @Lazy not meaningful with primitive so convert to wrapper if needed
if (ClassHelper.isPrimitiveType(fieldNode.getType())) {
fieldNode.setType(ClassHelper.getWrapper(fieldNode.getType()));
}
}
}
public ClassNode getMemberClassValue(AnnotationNode node, String name, ClassNode defaultValue) {
final Expression member = node.getMember(name);
if (member != null) {
if (member instanceof ClassExpression) {
if (!isUndefined(member.getType())) return member.getType();
} else if (member instanceof VariableExpression) {
addError("Error expecting to find class value for '" + name + "' but found variable: " + member.getText() + ". Missing import?", node);
return null;
} else if (member instanceof ConstantExpression) {
addError("Error expecting to find class value for '" + name + "' but found constant: " + member.getText() + "!", node);
return null;
}
}
return defaultValue;
}
public static List<FieldNode> getFieldsForLeftSideOfPropertyExpression(Expression node, ASTNodeVisitor astVisitor) {
ClassNode classNode = getTypeOfNode(node, astVisitor);
if (classNode != null) {
boolean statics = node instanceof ClassExpression;
return classNode.getFields().stream().filter(fieldNode -> {
return statics ? fieldNode.isStatic() : !fieldNode.isStatic();
}).collect(Collectors.toList());
}
return Collections.emptyList();
}
private void invokeClosure(final Expression arguments, final String methodName) {
AsmClassGenerator acg = controller.getAcg();
acg.visitVariableExpression(new VariableExpression(methodName));
controller.getOperandStack().box();
if (arguments instanceof TupleExpression) {
arguments.visit(acg);
} else {
new TupleExpression(arguments).visit(acg);
}
invokeClosureMethod.call(controller.getMethodVisitor());
controller.getOperandStack().replace(ClassHelper.OBJECT_TYPE);
}
private Expression transformSuperMethodCall(final MethodCallExpression call) {
String method = call.getMethodAsString();
if (method == null) {
throwSuperError(call);
}
Expression arguments = transform(call.getArguments());
ArgumentListExpression superCallArgs = new ArgumentListExpression();
if (arguments instanceof ArgumentListExpression) {
ArgumentListExpression list = (ArgumentListExpression) arguments;
for (Expression expression : list) {
superCallArgs.addExpression(expression);
}
} else {
superCallArgs.addExpression(arguments);
}
MethodCallExpression transformed = new MethodCallExpression(
weaved,
Traits.getSuperTraitMethodName(traitClass, method),
superCallArgs
);
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(false);
return transformed;
}
private void checkPrePostfixOperation(final Expression variable, final Expression originalExpression) {
if (variable instanceof Variable) {
recordAssignment((Variable) variable, false, false, true, originalExpression);
if (variable instanceof VariableExpression) {
Variable accessed = ((VariableExpression) variable).getAccessedVariable();
if (accessed != variable) {
recordAssignment(accessed, false, false, true, originalExpression);
}
}
}
}
public Expression transformRangeExpression(RangeExpression range) {
final ClassNode inferred = range.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (INTRANGE_TYPE.equals(inferred)) {
ArgumentListExpression bounds = new ArgumentListExpression(new ConstantExpression(range.isInclusive(),true),range.getFrom(), range.getTo());
ConstructorCallExpression cce = new ConstructorCallExpression(INTRANGE_TYPE, bounds);
cce.setSourcePosition(range);
cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, INTRANGE_CTOR);
cce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, INTRANGE_TYPE);
return transformer.transform(cce);
}
return transformer.superTransform(range);
}
@Override
protected void evaluateBinaryExpressionWithAssignment(final String method, final BinaryExpression expression) {
Expression leftExpression = expression.getLeftExpression();
if (leftExpression instanceof PropertyExpression) {
PropertyExpression pexp = (PropertyExpression) leftExpression;
BinaryExpression expressionWithoutAssignment = binX(
leftExpression,
Token.newSymbol(
TokenUtil.removeAssignment(expression.getOperation().getType()),
expression.getOperation().getStartLine(),
expression.getOperation().getStartColumn()
),
expression.getRightExpression()
);
expressionWithoutAssignment.copyNodeMetaData(expression);
expressionWithoutAssignment.setSafe(expression.isSafe());
expressionWithoutAssignment.setSourcePosition(expression);
if (makeSetProperty(
pexp.getObjectExpression(),
pexp.getProperty(),
expressionWithoutAssignment,
pexp.isSafe(),
pexp.isSpreadSafe(),
pexp.isImplicitThis(),
pexp instanceof AttributeExpression)) {
return;
}
}
super.evaluateBinaryExpressionWithAssignment(method, expression);
}
private static boolean extractImplicitThis$0StmtIfInnerClassFromExpression(final List<Statement> stmts, final Statement bstmt) {
Expression expr = ((ExpressionStatement) bstmt).getExpression();
if (expr instanceof BinaryExpression) {
Expression lExpr = ((BinaryExpression) expr).getLeftExpression();
if (lExpr instanceof FieldExpression) {
if ("this$0".equals(((FieldExpression) lExpr).getFieldName())) {
stmts.remove(bstmt); // remove from here and let the caller reposition it
return true;
}
}
}
return false;
}
@Override
public boolean complete(List<CompletionProposal> proposals, CompletionContext context, int anchor) {
this.context = context;
ASTNode leaf = context.path.leaf();
if (leaf instanceof ConstructorCallExpression) {
ConstructorCallExpression constructorCall = (ConstructorCallExpression) leaf;
Expression constructorArgs = constructorCall.getArguments();
if (constructorArgs instanceof TupleExpression) {
List<Expression> arguments = ((TupleExpression) constructorArgs).getExpressions();
if (arguments.isEmpty()) {
completeNamedParams(proposals, anchor, constructorCall, null);
} else {
for (Expression argExpression : arguments) {
if (argExpression instanceof NamedArgumentListExpression) {
completeNamedParams(proposals, anchor, constructorCall, (NamedArgumentListExpression) argExpression);
}
}
}
}
}
ASTNode leafParent = context.path.leafParent();
ASTNode leafGrandparent = context.path.leafGrandParent();
if (leafParent instanceof NamedArgumentListExpression &&
leafGrandparent instanceof ConstructorCallExpression) {
completeNamedParams(proposals, anchor, (ConstructorCallExpression) leafGrandparent, (NamedArgumentListExpression) leafParent);
}
return false;
}
@Override
public void visitDeclarationExpression(final DeclarationExpression expression) {
visitAnnotations(expression);
// visit right side first to prevent the use of a variable before its declaration
expression.getRightExpression().visit(this);
if (expression.isMultipleAssignmentDeclaration()) {
TupleExpression list = expression.getTupleExpression();
for (Expression listExpression : list.getExpressions()) {
declare((VariableExpression) listExpression);
}
} else {
declare(expression.getVariableExpression());
}
}
/**
* Find and add method usage if the given type contains method corresponding
* with the given method call. In other words we are looking for the method
* declaration in the given type and the method we are looking for is based
* on given method call. Might return null if the type can't be interfered
* properly and thus we are not able to find correct method - this is typical
* for dynamic types.
*
* @param type where we are looking for the specific method
* @param methodCall method call for which we want to find method in given
* type or null if the type is dynamic
*/
private static MethodNode findMethod(ClassNode type, MethodCallExpression methodCall) {
String findingMethod = methodCall.getMethodAsString();
Expression arguments = methodCall.getArguments();
if (!type.isResolved()) {
type = type.redirect();
}
MethodNode method = type.tryFindPossibleMethod(findingMethod, arguments);
if (method != null) {
return method;
}
return findMostAccurateMethod(methodCall, type.getMethods(findingMethod));
}
private static void create(FieldNode fieldNode, final Expression initExpr) {
final BlockStatement body = new BlockStatement();
if (fieldNode.isStatic()) {
addHolderClassIdiomBody(body, fieldNode, initExpr);
} else if (fieldNode.isVolatile()) {
addDoubleCheckedLockingBody(body, fieldNode, initExpr);
} else {
addNonThreadSafeBody(body, fieldNode, initExpr);
}
addMethod(fieldNode, body, fieldNode.getType());
}
private void visitArrayAttributes(final AnnotationNode an, final Map<String, ListExpression> arrayAttr, final AnnotationVisitor av) {
if (arrayAttr.isEmpty()) return;
for (Map.Entry<String, ListExpression> entry : arrayAttr.entrySet()) {
AnnotationVisitor av2 = av.visitArray(entry.getKey());
List<Expression> values = entry.getValue().getExpressions();
if (!values.isEmpty()) {
int arrayElementType = determineCommonArrayType(values);
for (Expression exprChild : values) {
visitAnnotationArrayElement(exprChild, arrayElementType, av2);
}
}
av2.visitEnd();
}
}
private static ClassNode getTargetClass(SourceUnit source, AnnotationNode annotation) {
Expression value = annotation.getMember("value");
if (!(value instanceof ClassExpression)) {
//noinspection ThrowableInstanceNeverThrown
source.getErrorCollector().addErrorAndContinue("@groovy.lang.Category must define 'value' which is the class to apply this category to", annotation, source);
return null;
} else {
ClassExpression ce = (ClassExpression) value;
return ce.getType();
}
}
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
super.visitConstructorCallExpression(call);
if (call.isUsingAnonymousInnerClass() && call.getType().getNodeMetaData(StaticTypeCheckingVisitor.class) != null) {
ClassNode anonType = call.getType();
anonType.putNodeMetaData(STATIC_COMPILE_NODE, anonType.getEnclosingMethod().getNodeMetaData(STATIC_COMPILE_NODE));
anonType.putNodeMetaData(WriterControllerFactory.class, anonType.getOuterClass().getNodeMetaData(WriterControllerFactory.class));
}
MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
if (target == null && call.getLineNumber() > 0) {
addError("Target constructor for constructor call expression hasn't been set", call);
} else if (target == null) {
// try to find a target
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments());
List<Expression> expressions = argumentListExpression.getExpressions();
ClassNode[] args = new ClassNode[expressions.size()];
for (int i = 0, n = args.length; i < n; i += 1) {
args[i] = typeChooser.resolveType(expressions.get(i), classNode);
}
target = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args);
call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
}
if (target != null) {
memorizeInitialExpressions(target);
}
}
@Override
protected Expression transformConstructorCall(ConstructorCallExpression exp)
{
if (allowed(exp.getType()))
{
Expression originalArgs = exp.getArguments();
Expression transformedArgs = transformArguments(originalArgs);
Expression unwrappedArgs = unwrapTransformedArguments(transformedArgs, originalArgs);
if (unwrappedArgs.equals(originalArgs))
{
if (log.isDebugEnabled())
{
log.debug("allowed constructor call " + exp);
}
return exp;
}
ConstructorCallExpression transformedCall = new ConstructorCallExpression(
exp.getType(), unwrappedArgs);
if (log.isDebugEnabled())
{
log.debug("transformed constructor call " + transformedCall);
}
return transformedCall;
}
return super.transformConstructorCall(exp);
}