private MetaClass inferType(final Context context, final Object initialization) { final Statement initStatement = GenUtil.generate(context, initialization); final MetaClass inferredType = (initStatement != null) ? initStatement.getType() : null; if (inferredType == null) { throw new InvalidTypeException("No type specified and no initialization provided to infer the type."); } return inferredType; }
@Test public void testDeclareVariableWithInvalidInitialization() { try { ContextBuilder.create() .declareVariable("n", Integer.class) .initializeWith("abc") .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }
@Test public void testInvalidCast() { try { final Statement stmt = Cast.to(Integer.class, Stmt.declareVariable("str", String.class).loadVariable("str")); stmt.generate(Context.create()); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected assertEquals("Wrong exception message", "java.lang.String cannot be cast to java.lang.Integer", e.getMessage()); } }
@Test public void testAddVariableWithImplicitTypeConversion() { Context ctx = ContextBuilder.create().addVariable("n", Integer.class, "10").getContext(); VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); try { Stmt.create(ContextBuilder.create().addVariable("n", Integer.class, "abc").getContext()).toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }
@Test public void testInvokeWithInvalidVariableReturnType() { try { StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() { }) .declareVariable("n", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected assertEquals("Wrong exception message", "java.lang.Integer is not assignable from java.lang.String", e.getMessage()); } }
private MetaClass inferType(final Context context, final Object initialization) { final Statement initStatement = GenUtil.generate(context, initialization); final MetaClass inferredType = (initStatement != null) ? initStatement.getType() : null; if (inferredType == null) { throw new InvalidTypeException("No type specified and no initialization provided to infer the type."); } return inferredType; }
@Test public void testDeclareVariableWithImplicitTypeConversion() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", Integer.class, "10").generate(ctx); assertEquals("failed to generate variable declaration with implicit type conversion", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); try { StatementBuilder.create().declareVariable("n", Integer.class, "abc").toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }
private void checkSwitchExprType() { final boolean validType = supportedTypes.stream() .anyMatch(cls -> MetaClassFactory.get(cls).isAssignableFrom(switchExprStmt.getType().asBoxed())); if (!validType) throw new InvalidTypeException("Type not permitted in switch statements:" + switchExprStmt.getType().getFullyQualifiedName()); } }
private void checkSwitchExprType() { final boolean validType = supportedTypes.stream() .anyMatch(cls -> MetaClassFactory.get(cls).isAssignableFrom(switchExprStmt.getType().asBoxed())); if (!validType) throw new InvalidTypeException("Type not permitted in switch statements:" + switchExprStmt.getType().getFullyQualifiedName()); } }
@Override public String generate(Context context) { StringBuilder buf = new StringBuilder(); try { buf.append("throw "); if (throwableType != null) { buf.append(ObjectBuilder.newInstanceOf(throwableType).withParameters(parameters).generate(context)); } else { VariableReference exceptionVar = context.getVariable(exceptionVariableName); if (!exceptionVar.getType().isAssignableTo(Throwable.class)) { throw new InvalidTypeException("Variable " + exceptionVariableName + " is not a Throwable"); } buf.append(exceptionVar.generate(context)); } } catch (GenerationException e) { blameAndRethrow(e); } return buf.toString(); } };
@Override public String generate(Context context) { StringBuilder buf = new StringBuilder(); try { buf.append("throw "); if (throwableType != null) { buf.append(ObjectBuilder.newInstanceOf(throwableType).withParameters(parameters).generate(context)); } else { VariableReference exceptionVar = context.getVariable(exceptionVariableName); if (!exceptionVar.getType().isAssignableTo(Throwable.class)) { throw new InvalidTypeException("Variable " + exceptionVariableName + " is not a Throwable"); } buf.append(exceptionVar.generate(context)); } } catch (GenerationException e) { blameAndRethrow(e); } return buf.toString(); } };
for (final LiteralValue<?> value : caseBlocks.keySet()) { if (!switchExprMetaClass.isAssignableFrom(value.getType().getErased())) { throw new InvalidTypeException( value.generate(context) + " is not a valid value for " + switchExprStmt.getType().getFullyQualifiedName());
for (final LiteralValue<?> value : caseBlocks.keySet()) { if (!switchExprMetaClass.isAssignableFrom(value.getType().getErased())) { throw new InvalidTypeException( value.generate(context) + " is not a valid value for " + switchExprStmt.getType().getFullyQualifiedName());
throw new InvalidTypeException(nfe);
throw new InvalidTypeException(nfe);
appendCallElement(new DeferredCallElement((writer, context1, statement) -> { if (extendsBlock == null && (type.isAbstract() || type.isInterface() || type.isPrimitive())) throw new InvalidTypeException("Cannot instantiate type:" + type, blame);
appendCallElement(new DeferredCallElement((writer, context1, statement) -> { if (extendsBlock == null && (type.isAbstract() || type.isInterface() || type.isPrimitive())) throw new InvalidTypeException("Cannot instantiate type:" + type, blame);
public static void assertAssignableTypes(final Context context, final MetaClass from, final MetaClass to) { if (!to.asBoxed().isAssignableFrom(from.asBoxed())) { if (to.isArray() && from.isArray() && GenUtil.getArrayDimensions(to) == GenUtil.getArrayDimensions(from) && to.getOuterComponentType().isAssignableFrom(from.getOuterComponentType())) { return; } if (!context.isPermissiveMode()) { if (classAliases.contains(from.getFullyQualifiedName()) && classAliases.contains(to.getFullyQualifiedName())) { // handle convertibility between MetaClass API and java Class reference. return; } throw new InvalidTypeException(to.getFullyQualifiedName() + " is not assignable from " + from.getFullyQualifiedName()); } } }
public static void assertAssignableTypes(final Context context, final MetaClass from, final MetaClass to) { if (!to.asBoxed().isAssignableFrom(from.asBoxed())) { if (to.isArray() && from.isArray() && GenUtil.getArrayDimensions(to) == GenUtil.getArrayDimensions(from) && to.getOuterComponentType().isAssignableFrom(from.getOuterComponentType())) { return; } if (!context.isPermissiveMode()) { if (classAliases.contains(from.getFullyQualifiedName()) && classAliases.contains(to.getFullyQualifiedName())) { // handle convertibility between MetaClass API and java Class reference. return; } throw new InvalidTypeException(to.getFullyQualifiedName() + " is not assignable from " + from.getFullyQualifiedName()); } } }
@Override public String generate(final Context context) { final String stmt = statement.generate(context); if (!toType.isPrimitive() && !toType.isAssignableFrom(statement.getType()) && !toType.isAssignableTo(statement.getType()) && !toType.isInterface() && !statement.getType().asBoxed().equals(toType)) { if (context.isPermissiveMode()) { return "(" + LoadClassReference.getClassReference(toType, context) + ") " + stmt; } else { throw new InvalidTypeException(statement.getType() + " cannot be cast to " + toType); } } else if (toType.isAssignableFrom(statement.getType()) && !statement.getType().equals(MetaClassFactory.get(NullType.class))) { return stmt; } else { return "(" + LoadClassReference.getClassReference(toType, context) + ") " + stmt; } }