1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use super::*;

#[derive(Debug, Clone)]
pub struct ConstructorSpec {
    pub modifiers: Modifiers,
    pub annotations: Vec<AnnotationSpec>,
    pub arguments: Vec<ArgumentSpec>,
    pub throws: Vec<ClassType>,
    pub elements: Elements,
}

impl ConstructorSpec {
    pub fn new(modifiers: Modifiers) -> ConstructorSpec {
        ConstructorSpec {
            modifiers: modifiers,
            annotations: Vec::new(),
            arguments: Vec::new(),
            throws: Vec::new(),
            elements: Elements::new(),
        }
    }

    pub fn push_annotation<A>(&mut self, annotation: A)
        where A: Into<AnnotationSpec>
    {
        self.annotations.push(annotation.into());
    }

    pub fn push_argument<A>(&mut self, argument: A)
        where A: Into<ArgumentSpec>
    {
        self.arguments.push(argument.into());
    }

    pub fn throws<T>(&mut self, throws: T)
        where T: Into<ClassType>
    {
        self.throws.push(throws.into())
    }

    pub fn push<E>(&mut self, element: E)
        where E: Into<Element>
    {
        self.elements.push(element);
    }

    pub fn as_element(self, enclosing: &str) -> Element {
        let mut elements = Elements::new();

        let mut open = Statement::new();

        for a in self.annotations {
            elements.push(a);
        }

        if !self.modifiers.is_empty() {
            open.push(self.modifiers);
            open.push(" ");
        }

        open.push(enclosing);

        if self.arguments.is_empty() {
            open.push("()");
        } else {
            open.push("(");
            open.push(Nested(Box::new(Push(join_statements(self.arguments, ", ")))));
            open.push(Variable::Element(Push(Variable::Literal(String::from(")")).into())));
        }

        if !self.throws.is_empty() {
            open.push(" throws ");

            let mut arguments = Statement::new();

            for throw in self.throws {
                arguments.push(throw);
            }

            open.push(arguments.join(", "));
        }

        open.push(" {");

        elements.push(open);
        elements.push_nested(self.elements);
        elements.push("}");

        elements.into()
    }
}

impl<'a, T> From<&'a T> for ConstructorSpec
    where T: Into<ConstructorSpec> + Clone
{
    fn from(value: &'a T) -> ConstructorSpec {
        value.clone().into()
    }
}