private ClassDeclaration createPreparedStatementSupportClass() { SimpleName preparedStatement = f.newSimpleName("statement"); //$NON-NLS-1$ SimpleName properties = f.newSimpleName("properties"); //$NON-NLS-1$ List<TypeBodyDeclaration> members = new ArrayList<>(); members.add(createPrivateField(PreparedStatement.class, preparedStatement, false)); members.add(f.newConstructorDeclaration( null, new AttributeBuilder(f).toAttributes(), f.newSimpleName(NAME_PREPARED_STATEMENT_SUPPORT), Arrays.asList( f.newFormalParameterDeclaration( context.resolve(PreparedStatement.class), preparedStatement), f.newFormalParameterDeclaration( context.resolve(int[].class), properties)), Arrays.asList(mapField(preparedStatement), mapField(properties)))); SimpleName object = f.newSimpleName("object"); //$NON-NLS-1$ List<Statement> statements = new ArrayList<>(); List<PropertyDeclaration> declared = getProperties(); object, preparedStatement, f.newArrayAccessExpression(properties, Models.toLiteral(f, i)), declared.get(i))); members.add(f.newMethodDeclaration( null, new AttributeBuilder(f)
private MethodDeclaration createCreateResultSetSupport() { SimpleName resultSet = f.newSimpleName("resultSet"); //$NON-NLS-1$ SimpleName columnNames = f.newSimpleName("columnNames"); //$NON-NLS-1$ List<Statement> statements = new ArrayList<>(); statements.add(createNullCheck(resultSet)); statements.add(createNullCheck(columnNames)); SimpleName vector = f.newSimpleName("vector"); //$NON-NLS-1$ statements.add(new ExpressionBuilder(f, f.newThis()) .method(NAME_CREATE_VECTOR, columnNames) .toLocalVariableDeclaration(context.resolve(int[].class), vector)); statements.add(new TypeBuilder(f, f.newNamedType(f.newSimpleName(NAME_RESULT_SET_SUPPORT))) .newObject(resultSet, vector) .toReturnStatement()); MethodDeclaration decl = f.newMethodDeclaration( null, new AttributeBuilder(f) .Public() .toAttributes(), context.resolve(f.newParameterizedType( context.resolve(DataModelResultSet.class), context.resolve(model.getSymbol()))), f.newSimpleName("createResultSetSupport"), //$NON-NLS-1$ Arrays.asList( f.newFormalParameterDeclaration( context.resolve(ResultSet.class), resultSet), f.newFormalParameterDeclaration( f.newParameterizedType( context.resolve(List.class),
private FieldDeclaration createSingletonField() { return f.newFieldDeclaration( null, new AttributeBuilder(f) .Private() .Static() .Final() .toAttributes(), context.resolve(DataModelDescriptor.class), f.newSimpleName(NAME_SINGLETON_FIELD), null); }
private MethodDeclaration createGetModelTypeMethod(Name value) { return createGetMethod( "getModelType", //$NON-NLS-1$ f.newParameterizedType(context.resolve(Class.class), f.newWildcard()), f.newClassLiteral(context.resolve(value))); }
private MethodDeclaration createGetMethod(String name, Type type, Expression value) { return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), type, f.newSimpleName(name), Collections.emptyList(), Arrays.asList(f.newReturnStatement(value))); }
/** * Creates a parameterized about operator element. * @param environment current environment * @param typeParameters target type parameter elements * @param rawNodeType parameterization target * @param imports import builder * @return generated syntax model * @throws IllegalArgumentException if some parameters were {@code null} */ public static Type toParameterizedType( CompileEnvironment environment, List<? extends TypeParameterElement> typeParameters, Type rawNodeType, ImportBuilder imports) { Objects.requireNonNull(environment, "environment must not be null"); //$NON-NLS-1$ Objects.requireNonNull(typeParameters, "typeParameters must not be null"); //$NON-NLS-1$ Objects.requireNonNull(rawNodeType, "rawNodeType must not be null"); //$NON-NLS-1$ Objects.requireNonNull(imports, "imports must not be null"); //$NON-NLS-1$ if (typeParameters.isEmpty()) { return rawNodeType; } ModelFactory factory = Models.getModelFactory(); List<Type> typeArgs = new ArrayList<>(); for (TypeParameterElement typeParameter : typeParameters) { typeArgs.add(factory.newNamedType(factory.newSimpleName(typeParameter.getSimpleName().toString()))); } return factory.newParameterizedType(rawNodeType, typeArgs); }
private MethodDeclaration createReferenceMapGetMethod(PropertyReferenceDeclaration reference) { SimpleName key = f.newSimpleName("_k"); //$NON-NLS-1$ List<Statement> cases = new ArrayList<>(); reference.getReference().asMap().forEach((k, v) -> { PropertyDeclaration decl = v.findDeclaration(); assert decl != null; cases.add(f.newSwitchCaseLabel(Models.toLiteral(f, k))); cases.add(new TypeBuilder(f, f.newNamedType(context.getTypeName())) .dotThis() .method(context.getOptionGetterName(decl)) .toReturnStatement()); }); cases.add(f.newSwitchDefaultLabel()); cases.add(f.newReturnStatement(Models.toNullLiteral(f))); List<Statement> statements = new ArrayList<>(); statements.add(f.newIfStatement( f.newUnaryExpression(UnaryOperator.NOT, f.newInstanceofExpression(key, context.resolve(String.class))), f.newBlock(f.newReturnStatement(Models.toNullLiteral(f))))); statements.add(f.newSwitchStatement( f.newCastExpression(context.resolve(String.class), key), cases)); return f.newMethodDeclaration( null, new AttributeBuilder(f) .toAttributes(), context.getElementType(reference), f.newSimpleName("get"), //$NON-NLS-1$ Arrays.asList(f.newFormalParameterDeclaration(context.resolve(Object.class), key)), statements);
private MethodDeclaration createIsSupported() { SimpleName columnNames = f.newSimpleName("columnNames"); //$NON-NLS-1$ List<Statement> statements = new ArrayList<>(); statements.add(createNullCheck(columnNames)); statements.add(f.newIfStatement( new ExpressionBuilder(f, columnNames) .method("isEmpty") //$NON-NLS-1$ .toExpression(), f.newBlock(f.newReturnStatement(Models.toLiteral(f, false))))); statements.add(f.newTryStatement( f.newBlock( new ExpressionBuilder(f, f.newThis()) .method(NAME_CREATE_VECTOR, columnNames) .toStatement(), new ExpressionBuilder(f, Models.toLiteral(f, true)) .toReturnStatement()), Arrays.asList(f.newCatchClause( f.newFormalParameterDeclaration( context.resolve(IllegalArgumentException.class), f.newSimpleName("e")), //$NON-NLS-1$ f.newBlock(new ExpressionBuilder(f, Models.toLiteral(f, false)) .toReturnStatement()))), null)); MethodDeclaration decl = f.newMethodDeclaration( null, new AttributeBuilder(f) .toAttributes(), context.resolve(boolean.class), f.newSimpleName("isSupported"), //$NON-NLS-1$
List<Statement> statements = new ArrayList<>(); SimpleName obj = context.createVariableName("obj"); //$NON-NLS-1$ statements.add(f.newIfStatement( new ExpressionBuilder(f, f.newThis()) .apply(InfixOperator.EQUALS, obj) .toExpression(), f.newBlock(f.newReturnStatement(Models.toLiteral(f, true))))); statements.add(f.newIfStatement( new ExpressionBuilder(f, obj) .apply(InfixOperator.EQUALS, Models.toNullLiteral(f)) .toExpression(), f.newBlock(f.newReturnStatement(Models.toLiteral(f, false))))); statements.add(f.newIfStatement( new ExpressionBuilder(f, f.newThis()) .method("getClass") //$NON-NLS-1$ .apply(InfixOperator.NOT_EQUALS, new ExpressionBuilder(f, obj) .toExpression()) .toExpression(), f.newBlock(f.newReturnStatement(Models.toLiteral(f, false))))); SimpleName other = context.createVariableName("other"); //$NON-NLS-1$ Type self = context.resolve(context.getQualifiedTypeName()); for (PropertyDeclaration property : model.getDeclaredProperties()) { SimpleName field = context.getFieldName(property); statements.add(f.newIfStatement( new ExpressionBuilder(f, f.newThis()) .field(field) .method("equals", new ExpressionBuilder(f, other) //$NON-NLS-1$
private MethodDeclaration createGetColumnMap() { MethodDeclaration decl = f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), f.newParameterizedType( context.resolve(Map.class), context.resolve(String.class), context.resolve(String.class)), f.newSimpleName("getColumnMap"), //$NON-NLS-1$ Collections.emptyList(), Arrays.asList(new ExpressionBuilder(f, f.newSimpleName(NAME_COLUMN_MAP)) .toReturnStatement())); return decl; }
private MethodDeclaration createMethod(String methodName, Type returnType, Expression value) { return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), returnType, f.newSimpleName(methodName), Collections.emptyList(), Arrays.asList(new ExpressionBuilder(f, value).toReturnStatement())); } }
private MethodDeclaration createReferenceListGetMethod(PropertyReferenceDeclaration reference) { SimpleName index = f.newSimpleName("_i"); //$NON-NLS-1$ List<Statement> cases = new ArrayList<>(); int caseIndex = 0; for (PropertySymbol ref : reference.getReference().getAllReferences()) { PropertyDeclaration decl = ref.findDeclaration(); assert decl != null; cases.add(f.newSwitchCaseLabel(Models.toLiteral(f, caseIndex++))); cases.add(new TypeBuilder(f, f.newNamedType(context.getTypeName())) .dotThis() .method(context.getOptionGetterName(decl)) .toReturnStatement()); } cases.add(f.newSwitchDefaultLabel()); cases.add(new TypeBuilder(f, context.resolve(IndexOutOfBoundsException.class)) .newObject() .toThrowStatement()); return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), context.getElementType(reference), f.newSimpleName("get"), //$NON-NLS-1$ Arrays.asList(f.newFormalParameterDeclaration(context.resolve(int.class), index)), Arrays.asList(f.newSwitchStatement(index, cases))); }
private MethodDeclaration generateImplementor(ExecutableElement method) { return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(imports.toType(Override.class)) .Public() .toAttributes(), toTypeParameters(method), imports.resolve(converter.convert(method.getReturnType())), f.newSimpleName(method.getSimpleName().toString()), toParameters(method), 0, Collections.emptyList(), f.newBlock(new TypeBuilder(f, imports.toType(UnsupportedOperationException.class)) .newObject() .toThrowStatement())); }
private MethodDeclaration createCopyToModel() { SimpleName key = f.newSimpleName("key"); //$NON-NLS-1$ SimpleName value = f.newSimpleName("value"); //$NON-NLS-1$ SimpleName internal = f.newSimpleName("model"); //$NON-NLS-1$ return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), context.resolve(void.class), f.newSimpleName("copyToModel"), //$NON-NLS-1$ Arrays.asList(new FormalParameterDeclaration[] { f.newFormalParameterDeclaration(context.resolve(NullWritable.class), key), f.newFormalParameterDeclaration(context.resolve(model.getSymbol()), value), f.newFormalParameterDeclaration(context.resolve(model.getSymbol()), internal), }), Arrays.asList(new ExpressionBuilder(f, internal) .method("copyFrom", value) //$NON-NLS-1$ .toStatement())); }
@Override protected void test() throws IOException { ImportBuilder imports = new ImportBuilder( f, f.newPackageDeclaration(Models.toName(f, "com.example.testing")), ImportBuilder.Strategy.TOP_LEVEL); Expression target = resolver.apply(imports); MethodDeclaration method = f.newMethodDeclaration( null, new AttributeBuilder(f).Public().toAttributes(), imports.toType(Object.class), f.newSimpleName("call"), Collections.emptyList(), Collections.singletonList(f.newReturnStatement(target))); TypeDeclaration type = f.newClassDeclaration( null, new AttributeBuilder(f).Public().toAttributes(), f.newSimpleName("Work"), null, Collections.singletonList(new TypeBuilder(f, imports.toType(Callable.class)) .parameterize(imports.toType(Object.class)) .toType()), Collections.singletonList(method)); env.emit(f.newCompilationUnit( imports.getPackageDeclaration(), imports.toImportDeclarations(), Collections.singletonList(type))); } });
private MethodDeclaration createReferenceMapKeySetMethod(PropertyReferenceDeclaration reference) { return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), f.newParameterizedType(context.resolve(Set.class), context.resolve(String.class)), f.newSimpleName("keySet"), //$NON-NLS-1$ Arrays.asList(), Arrays.asList(new TypeBuilder(f, f.newNamedType(context.getTypeName())) .field(getKeysFieldName(reference)) .toReturnStatement())); }
private MethodDeclaration createWrite(EmitContext context, ModelDeclaration model) { assert context != null; assert model != null; ModelFactory f = context.getModelFactory(); SimpleName parameter = context.createVariableName("out"); //$NON-NLS-1$ List<Statement> statements = new ArrayList<>(); for (PropertyDeclaration property : model.getDeclaredProperties()) { SimpleName fieldName = context.getFieldName(property); statements.add(new ExpressionBuilder(f, fieldName) .method("write", parameter) //$NON-NLS-1$ .toStatement()); } return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), Collections.emptyList(), Models.toType(f, void.class), f.newSimpleName("write"), //$NON-NLS-1$ Collections.singletonList(f.newFormalParameterDeclaration( context.resolve(DataOutput.class), parameter)), 0, Collections.singletonList(context.resolve(IOException.class)), f.newBlock(statements)); }
private Expression createNewPropertyDescriptor(PropertyDeclaration property) { Expression columnName = Models.toLiteral(f, HiveFieldTrait.getColumnName(property)); Expression typeDesc = computeValueType(property); Expression comment = null; AstDescription description = property.getDescription(); if (description == null) { comment = Models.toNullLiteral(f); } else { comment = Models.toLiteral(f, description.getText()); } SimpleName dataModel = f.newSimpleName("dataModel"); //$NON-NLS-1$ ClassBody block = f.newClassBody(Arrays.asList(f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), new TypeBuilder(f, context.resolve(ValueOption.class)) .parameterize(f.newWildcard()) .toType(), f.newSimpleName("extract"), //$NON-NLS-1$ Arrays.asList(f.newFormalParameterDeclaration(context.resolve(Object.class), dataModel)), Arrays.asList(new ExpressionBuilder(f, dataModel) .castTo(context.resolve(model.getSymbol())) .method(context.getOptionGetterName(property)) .toReturnStatement())))); return new TypeBuilder(f, context.resolve(PropertyDescriptor.class)) .newObject(Arrays.asList(columnName, typeDesc, comment), block) .toExpression(); }
private MethodDeclaration createCopyMethod() { SimpleName other = context.createVariableName("other"); //$NON-NLS-1$ List<Statement> statements = new ArrayList<>(); for (PropertyDeclaration property : model.getDeclaredProperties()) { statements.add(new ExpressionBuilder(f, f.newThis()) .field(context.getFieldName(property)) .method("copyFrom", new ExpressionBuilder(f, other) //$NON-NLS-1$ .field(context.getFieldName(property)) .toExpression()) .toStatement()); } return f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .annotation(context.resolve(SuppressWarnings.class), Models.toLiteral(f, "deprecation")) //$NON-NLS-1$ .Public() .toAttributes(), context.resolve(void.class), f.newSimpleName("copyFrom"), //$NON-NLS-1$ Collections.singletonList(f.newFormalParameterDeclaration( context.resolve(context.getQualifiedTypeName()), other)), statements); }
private Statement createResultSetNullMapping( Expression object, Expression resultSet, PropertyDeclaration property) { return f.newIfStatement( new ExpressionBuilder(f, resultSet) .method("wasNull") //$NON-NLS-1$ .toExpression(), f.newBlock(setNullToProperty(object, property))); }