1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2;
33
34import org.stringtemplate.v4.*;
35
36import java.io.*;
37import java.net.URL;
38
39public class AccessorTestGenerator {
40    private static class UnaryOperation {
41        public final String name;
42        public UnaryOperation(String name) {
43            this.name = name;
44        }
45    }
46
47    private static class BinaryOperation {
48        public final String name;
49        public final String[] inputTypes;
50        public BinaryOperation(String name, String[] inputTypes) {
51            this.name = name;
52            this.inputTypes = inputTypes;
53        }
54    }
55
56    private static class TypeDef {
57        public final String name;
58        public final UnaryOperation[] unaryOperations;
59        public final BinaryOperation[] binaryOperations;
60        public TypeDef(String name, UnaryOperation[] unaryOperations, BinaryOperation[] binaryOperations) {
61            this.name = name;
62            this.unaryOperations = unaryOperations;
63            this.binaryOperations = binaryOperations;
64        }
65    }
66
67    private static final UnaryOperation[] unaryOperations = new UnaryOperation[] {
68            new UnaryOperation("preinc"),
69            new UnaryOperation("postinc"),
70            new UnaryOperation("predec"),
71            new UnaryOperation("postdec")
72    };
73
74    private static final String[] booleanInputs = new String[] {"boolean"};
75    private static final String[] integralInputs = new String[] {"int", "long"};
76    private static final String[] allInputs = new String[] {"int", "float", "long", "double"};
77
78    private static final BinaryOperation[] booleanOperations = new BinaryOperation[] {
79            new BinaryOperation("and", booleanInputs),
80            new BinaryOperation("or", booleanInputs),
81            new BinaryOperation("xor", booleanInputs),
82    };
83
84    private static final BinaryOperation[] floatOperations = new BinaryOperation[] {
85            new BinaryOperation("add", allInputs),
86            new BinaryOperation("sub", allInputs),
87            new BinaryOperation("mul", allInputs),
88            new BinaryOperation("div", allInputs),
89            new BinaryOperation("rem", allInputs),
90    };
91
92    private static final BinaryOperation[] integralOperations = new BinaryOperation[] {
93            new BinaryOperation("add", allInputs),
94            new BinaryOperation("sub", allInputs),
95            new BinaryOperation("mul", allInputs),
96            new BinaryOperation("div", allInputs),
97            new BinaryOperation("rem", allInputs),
98            new BinaryOperation("and", integralInputs),
99            new BinaryOperation("or", integralInputs),
100            new BinaryOperation("xor", integralInputs),
101            new BinaryOperation("shl", integralInputs),
102            new BinaryOperation("shr", integralInputs),
103            new BinaryOperation("ushr", integralInputs),
104    };
105
106    private static final TypeDef[] types = new TypeDef[] {
107            new TypeDef("boolean", new UnaryOperation[0], booleanOperations),
108            new TypeDef("byte", unaryOperations, integralOperations),
109            new TypeDef("char", unaryOperations, integralOperations),
110            new TypeDef("short", unaryOperations, integralOperations),
111            new TypeDef("int", unaryOperations, integralOperations),
112            new TypeDef("long", unaryOperations, integralOperations),
113            new TypeDef("float", unaryOperations, floatOperations),
114            new TypeDef("double", unaryOperations, floatOperations),
115    };
116
117
118    public static void main(String[] args) throws IOException {
119        if (args.length != 1) {
120            System.err.println("Usage: java org.jf.dexlib2.AccessorTestGenerator <output_file>");
121        }
122
123        URL stgUrl = AccessorTestGenerator.class.getClassLoader().getResource("AccessorTest.stg");
124        STGroupFile stg = new STGroupFile(stgUrl, "utf-8", '<', '>');
125        ST fileSt = stg.getInstanceOf("file");
126        fileSt.add("types", types);
127
128        PrintWriter w = null;
129        try {
130            w = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
131            w.print(fileSt.render());
132        } finally {
133            if (w != null) {
134                w.close();
135            }
136        }
137    }
138}
139
140
141
142