private static Map<Variable, CodeTree> castBoundTypes(Map<Variable, LocalVariable> bindings) { Map<Variable, CodeTree> resolvedBindings = new HashMap<>(); for (Variable variable : bindings.keySet()) { LocalVariable localVariable = bindings.get(variable); CodeTree resolved = localVariable.createReference(); TypeMirror sourceType = localVariable.getTypeMirror(); TypeMirror targetType = variable.getResolvedTargetType(); if (targetType == null) { targetType = variable.getResolvedType(); } if (!ElementUtils.isAssignable(sourceType, targetType)) { resolved = CodeTreeBuilder.createBuilder().cast(targetType, resolved).build(); } resolvedBindings.put(variable, resolved); } return resolvedBindings; }
private static Map<Variable, CodeTree> castBoundTypes(Map<Variable, LocalVariable> bindings) { Map<Variable, CodeTree> resolvedBindings = new HashMap<>(); for (Variable variable : bindings.keySet()) { LocalVariable localVariable = bindings.get(variable); CodeTree resolved = localVariable.createReference(); TypeMirror sourceType = localVariable.getTypeMirror(); TypeMirror targetType = variable.getResolvedTargetType(); if (targetType == null) { targetType = variable.getResolvedType(); } if (!ElementUtils.isAssignable(sourceType, targetType)) { resolved = CodeTreeBuilder.createBuilder().startParantheses().cast(targetType, resolved).end().build(); } resolvedBindings.put(variable, resolved); } return resolvedBindings; }
public void visitVariable(Variable variable) { VariableElement resolvedVariable = variable.getResolvedVariable(); CodeTree tree; if (variable.getResolvedType().getKind() == TypeKind.NULL) { tree = CodeTreeBuilder.singleString("null"); } else if (variable.getReceiver() == null) { if (isStatic(resolvedVariable)) { tree = staticReference(resolvedVariable); } else { tree = bindings.get(variable); boolean bound = true; if (tree == null) { tree = string(resolvedVariable.getSimpleName().toString()); bound = false; } if (root != null && !bound) { tree = combine(root, string("."), tree); } } } else { if (isStatic(resolvedVariable)) { throw new AssertionError("Static variables cannot have receivers."); } tree = combine(pop(), string("."), string(resolvedVariable.getSimpleName().toString())); } push(tree); }
public void visitVariable(Variable variable) { VariableElement resolvedVariable = variable.getResolvedVariable(); CodeTree tree; if (variable.getResolvedType().getKind() == TypeKind.NULL) { tree = CodeTreeBuilder.singleString("null"); } else if (variable.getReceiver() == null) { if (isStatic(resolvedVariable)) { tree = staticReference(resolvedVariable); } else { tree = bindings.get(variable); boolean bound = true; if (tree == null) { tree = string(resolvedVariable.getSimpleName().toString()); bound = false; } if (root != null && !bound) { tree = combine(root, string("."), tree); } } } else { if (isStatic(resolvedVariable)) { throw new AssertionError("Static variables cannot have receivers."); } tree = combine(pop(), string("."), string(resolvedVariable.getSimpleName().toString())); } push(tree); }
@Override public DSLExpression visitBinary(Binary binary) { // signature: value == value can be folded to true if (IDENTITY_FOLD_OPERATORS.contains(binary.getOperator())) { if (binary.getLeft() instanceof Variable && binary.getRight() instanceof Variable) { Variable leftVar = ((Variable) binary.getLeft()); Variable rightVar = ((Variable) binary.getRight()); if (leftVar.equals(rightVar)) { // double and float cannot be folded as NaN is never identity equal if (!ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(float.class)) && !ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(double.class))) { return new BooleanLiteral(true); } } } } return super.visitBinary(binary); } });