void changeTypeContext(ResolutionContext old, ResolutionContext new_, MethodDeclaration m) { m.setType(changeTypeContext(old, new_, m.getType())); m.setBody( new BlockStmt( Collections.singletonList( new ThrowStmt( new ObjectCreationExpr( null, new ClassOrInterfaceType("UnsupportedOperationException"), Collections.emptyList()))))); NodeUtil.forChildren( m, node -> { Expression scope = node.getScope(); // TODO: Currently guesses that it's a type name if first character is uppercase. // Should check for fields/variables which match instead if (scope instanceof NameExpr && Character.isUpperCase(((NameExpr) scope).getName().charAt(0))) { String name = ((NameExpr) scope).getName(); node.setScope(ASTHelper.createNameExpr(new_.typeToString(old.resolve(name)))); } }, MethodCallExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), VariableDeclarationExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), TypeExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), com.github.javaparser.ast.body.Parameter.class); }
private Expression generateCopyExpression( DefinedParamType type, String inputParmId, int inputIndex, String outputParamId, int outputIndex) { // GOAL: ((InputSocket<Mat>) inputs[0]).getValue().assignTo(((OutputSocket<Mat>) // outputs[0]).getValue()); final ClassOrInterfaceType outputType = new ClassOrInterfaceType("OutputSocket"); final ClassOrInterfaceType inputType = new ClassOrInterfaceType("InputSocket"); outputType.setTypeArgs(Collections.singletonList(type.getType())); inputType.setTypeArgs(Collections.singletonList(type.getType())); final MethodCallExpr copyExpression = new MethodCallExpr( getOrSetValueExpression( new EnclosedExpr(new CastExpr(inputType, arrayAccessExpr(inputParmId, inputIndex))), null), "assignTo", Collections.singletonList( getOrSetValueExpression( new EnclosedExpr( new CastExpr(outputType, arrayAccessExpr(outputParamId, outputIndex))), null))); copyExpression.setComment( new BlockComment( " Sets the value of the input Mat to the output because this operation does not have a destination Mat. ")); return copyExpression; }
private MethodCallExpr getOrSetValueExpression(Expression scope, Expression setExpression) { return new MethodCallExpr( scope, (setExpression == null ? "getValue" : "setValue"), (setExpression == null ? Collections.emptyList() : Collections.singletonList(setExpression))); }
@Override public void add(MethodInfo method) { MethodDeclaration methodDeclaration; if (method instanceof MethodDeclarationWrapper) { val wrapper = (MethodDeclarationWrapper) method; methodDeclaration = (MethodDeclaration) wrapper.declaration.clone(); methodDeclaration.setAnnotations(Collections.emptyList()); wrapper .getClassInfo() .changeTypeContext(wrapper.getContext(), getContext(), methodDeclaration); } else { methodDeclaration = new MethodDeclaration(); new MethodDeclarationWrapper(methodDeclaration).setAll(method); } addMember(methodDeclaration); }
@Override public void add(FieldInfo field) { FieldDeclaration fieldDeclaration; if (field instanceof FieldDeclarationWrapper) { val wrapper = (FieldDeclarationWrapper) field; fieldDeclaration = (FieldDeclaration) wrapper.declaration.clone(); fieldDeclaration.setAnnotations(Collections.emptyList()); changeTypeContext(wrapper.getContext(), getContext(), fieldDeclaration); } else { fieldDeclaration = new FieldDeclaration(); val vars = new ArrayList<VariableDeclarator>(); vars.add(new VariableDeclarator(new VariableDeclaratorId("unknown"))); fieldDeclaration.setVariables(vars); new FieldDeclarationWrapper(fieldDeclaration).setAll(field); } addMember(fieldDeclaration); val result = new FieldDeclarationWrapper(fieldDeclaration); if (!field.similar(result)) throw new TransformationException( "After adding to class, didn't match. added: " + field + " result: " + result); }
@Override public List<TypeVariable> getTypeVariables() { return Collections.emptyList(); }
public List<Expression> getSocketAssignments(String inputParamId, String outputParamId) { List<Expression> assignments = new ArrayList<>(); int inputIndex = 0; int outputIndex = 0; for (DefinedParamType paramType : paramTypes) { // We still need to increment the values if the param is ignored if (paramType.isIgnored()) continue; int index; String paramId; if (inputParamTypes.contains(paramType) && outputParamTypes.contains(paramType)) { if (paramType.getType().toStringWithoutComments().contains("Mat")) { assignments.add( generateCopyExpression( paramType, inputParamId, inputParamTypes.indexOf(paramType), outputParamId, outputParamTypes.indexOf(paramType))); } else { throw new IllegalStateException( "Can not generate Input/Output Socket for type: " + paramType.getType().toString()); } // We have used the input socket inputIndex++; } // Generate the output socket event if this is an input/output socket if (outputParamTypes.contains(paramType)) { paramId = outputParamId; index = outputIndex; outputIndex++; } else if (inputParamTypes.contains(paramType)) { paramId = inputParamId; index = inputIndex; inputIndex++; } else { assert false : "The paramType was not in either the input or output list"; return null; } final MethodCallExpr getValueExpression = getValueExpression(paramId, index); final Expression assignExpression; if (paramType.getType() instanceof PrimitiveType && (!((PrimitiveType) paramType.getType()) .getType() .equals(PrimitiveType.Primitive.Boolean))) { final String numberConversionFunction; switch (((PrimitiveType) paramType.getType()).getType()) { case Int: numberConversionFunction = "intValue"; break; case Short: case Char: numberConversionFunction = "shortValue"; break; case Float: numberConversionFunction = "floatValue"; break; case Double: numberConversionFunction = "doubleValue"; break; case Byte: numberConversionFunction = "byteValue"; break; case Long: numberConversionFunction = "longValue"; break; default: throw new IllegalStateException( "Conversion for type " + paramType.getType() + " is not defined"); } assignExpression = new MethodCallExpr( new EnclosedExpr( new CastExpr(ASTHelper.createReferenceType("Number", 0), getValueExpression)), numberConversionFunction); } else { assignExpression = new CastExpr(paramType.getTypeBoxedIfPossible(), getValueExpression); } assignments.add( new VariableDeclarationExpr( ModifierSet.FINAL, paramType.getType(), Collections.singletonList( new VariableDeclarator( new VariableDeclaratorId(paramType.getName()), assignExpression)))); } return assignments; }