private JExpression maybeCast(JType expected, JExpression expression) { if (expected != expression.getType()) { // Must be a generic; insert a cast operation. JReferenceType toType = (JReferenceType) expected; return new JCastOperation(expression.getSourceInfo(), toType, expression); } else { return expression; } }
@Override public void endVisit(JCastOperation x, Context ctx) { x.resolve(translate(x.getCastType())); }
@Override public void endVisit(CastExpression x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JType type = typeMap.get(x.resolvedType); JExpression expression = pop(x.expression); if (x.type instanceof NameReference) { pop(x.type); } push(new JCastOperation(info, type, expression)); } catch (Throwable e) { throw translateException(x, e); } }
private JExpression box(JExpression original, int implicitConversion) { int typeId = (implicitConversion & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4; ClassScope scope = curClass.scope; BaseTypeBinding primitiveType = (BaseTypeBinding) TypeBinding.wellKnownType(scope, typeId); ReferenceBinding boxType = (ReferenceBinding) scope.boxing(primitiveType); MethodBinding valueOfMethod = boxType.getExactMethod(VALUE_OF, new TypeBinding[]{primitiveType}, scope .compilationUnitScope()); assert valueOfMethod != null; // Add a cast to the correct primitive type if needed. JType targetPrimitiveType = typeMap.get(primitiveType); if (original.getType() != targetPrimitiveType) { original = new JCastOperation(original.getSourceInfo(), targetPrimitiveType, original); } JMethod boxMethod = typeMap.get(valueOfMethod); JMethodCall call = new JMethodCall(original.getSourceInfo(), null, boxMethod); call.addArg(original); return call; }