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; } }
public static String debug(JExpression inst) { return inst.getClass()+" ["+inst.toSource()+"] @"+inst.getSourceInfo(); }
@SuppressWarnings("unchecked") protected <T extends JStatement> List<T> pop(Statement[] statements) { if (statements == null) { return Collections.emptyList(); } List<T> result = (List<T>) popList(statements.length); int i = 0; for (ListIterator<T> it = result.listIterator(); it.hasNext(); ++i) { Object element = it.next(); if (element == null) { it.remove(); } else if (element instanceof JExpression) { it.set((T) simplify((JExpression) element, (Expression) statements[i]).makeStatement()); } } return result; }
@Override public void endVisit(JExpression x, Context ctx) { assert !x.getType().isExternal() || errorsFound; }
@Override public JExpression injectMagic(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { if (methodCall.getArgs().size() != 1) { logger.log(Type.ERROR, "X_Reflect.magicClass accepts one and only one argument: a class literal."); throw new UnableToCompleteException(); } if (!(methodCall.getArgs().get(0) instanceof JClassLiteral)) { logger.log(Type.ERROR, "X_Reflect.magicClass accepts one and only one argument: a class literal." + " You sent a " + methodCall.getArgs().get(0).getClass() + " : " + methodCall.getArgs().get(0).toSource()); throw new UnableToCompleteException(); } JClassLiteral clazz = (JClassLiteral)methodCall.getArgs().get(0); return mappedClasses.get(clazz.getRefType().getName(), new ReflectionGeneratorContext(logger, clazz, methodCall, currentMethod, context, ast)); }
private JExpression simplify(JExpression result, Expression x) { if (x.constant != null && x.constant != Constant.NotAConstant) { // Prefer JDT-computed constant value to the actual written expression. result = getConstant(result.getSourceInfo(), x.constant); } return maybeBoxOrUnbox(result, x.implicitConversion); }
@Override public boolean visit(JReboundEntryPoint x, Context ctx) { pastRebound = true; x.getEntryCalls().get(0).traverse(this, ctx);; return false; }
logger.log(Type.ERROR, "Could not generate X_Inject.instance for "+arg.getType().getName());
public static JClassLiteral extractClassLiteral(TreeLogger logger, JMethodCall methodCall, int paramPosition, UnifyAstView ast) throws UnableToCompleteException { List<JExpression> args = methodCall.getArgs(); JExpression arg = args.get(paramPosition); JClassLiteral classLit = extractClassLiteral(logger, arg, ast, false); if (classLit == null) { logger.log(Type.ERROR, "The method " + methodCall.getTarget().toSource() + " only accepts class literals." + " You sent a " + arg.getClass() + " : " + arg.toSource()+" from method " + methodCall.toSource()+ " with argumetsn " + methodCall.getArgs()+ ";"); throw new UnableToCompleteException(); } return classLit; }
private JExpression unbox(JExpression original, int implicitConversion) { int typeId = implicitConversion & TypeIds.COMPILE_TYPE_MASK; ClassScope scope = curClass.scope; BaseTypeBinding primitiveType = (BaseTypeBinding) TypeBinding.wellKnownType(scope, typeId); ReferenceBinding boxType = (ReferenceBinding) scope.boxing(primitiveType); char[] selector = CharOperation.concat(primitiveType.simpleName, VALUE); MethodBinding valueMethod = boxType.getExactMethod(selector, NO_TYPES, scope.compilationUnitScope()); assert valueMethod != null; JMethod unboxMethod = typeMap.get(valueMethod); JMethodCall call = new JMethodCall(original.getSourceInfo(), original, unboxMethod); return call; }
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; }
JProgram.createLocal(info, elementVarName + "$array", collection.getType(), true, curMethod.body); JLocal indexVar =
protected JStatement pop(Statement x) { JNode pop = (x == null) ? null : pop(); if (x instanceof Expression) { return simplify((JExpression) pop, (Expression) x).makeStatement(); } return (JStatement) pop; }
curRef = maybeCast(castType, curRef); SourceInfo info = curRef.getSourceInfo();
@Override public void endVisit(SynchronizedStatement x, BlockScope scope) { try { JBlock block = pop(x.block); JExpression expression = pop(x.expression); block.addStmt(0, expression.makeStatement()); push(block); } catch (Throwable e) { throw translateException(x, e); } }
info = expr.getSourceInfo().makeChild(SourceOrigin.UNKNOWN); final JMethodBody body = new JMethodBody(info); getSingleton = new JMethod(info, targetName, enclosing, type, false, true, true,
JExpression invoke = MagicClassInjector.injectMagicClass(logger, call, magicClass, context, ast); if (invoke != null) block.addStmt(invoke.makeStatement());
call, magicClass, context, ast); if (invoke != null) { block.addStmt(invoke.makeStatement());