1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | /* |
11 | * $Id: JniHeaderClassGeneratorVisitor.java,v 1.1.1.5 2004/05/25 20:23:29 hastings Exp $ |
12 | */ |
13 | package com.moesol.generator; |
14 | |
15 | import java.io.*; |
16 | import java.lang.reflect.*; |
17 | import java.util.*; |
18 | import com.moesol.generator.core.*; |
19 | |
20 | public class JniHeaderClassGeneratorVisitor extends ClassGeneratorVisitor { |
21 | JniHeaderClassGeneratorVisitor(Writer out, TranslationContext ctx) { |
22 | super(out, ctx); |
23 | } |
24 | protected boolean shouldGenerateField(Field field) { |
25 | return Modifier.isStatic(field.getModifiers()) |
26 | && Modifier.isFinal(field.getModifiers()) |
27 | && field.getType().isPrimitive(); |
28 | } |
29 | protected void extendFieldProperties(Field field, Properties p) throws ApplyException { |
30 | p.setProperty("comment", "// "); |
31 | p.setProperty("access", getFieldModifiers(field)); |
32 | // TODO refactor javaForValue |
33 | p.setProperty("value", getValue(field)); |
34 | } |
35 | protected String getFieldTemplateName(Field field) { |
36 | return "jni.h.field.tmpl"; |
37 | } |
38 | protected boolean shouldGenerateMethod(Method method) { |
39 | return false; |
40 | } |
41 | protected void extendMethodProperties(Method method, Properties p) throws ApplyException { |
42 | } |
43 | protected String getTemplateName(Method method) { |
44 | return null; |
45 | } |
46 | protected boolean shouldGenerateConstructor(Constructor constructor) { |
47 | return false; |
48 | } |
49 | protected String getTemplateName(Constructor constructor) { |
50 | return null; |
51 | } |
52 | |
53 | private String getValue(Field field) throws ApplyException { |
54 | try { |
55 | field.setAccessible(true); |
56 | return JavaClassGeneratorVisitor.javaForValue(field.get(null)); |
57 | } catch (IllegalAccessException e) { |
58 | throw new ApplyException(e); |
59 | } |
60 | } |
61 | |
62 | private static class NameBitPair { |
63 | public NameBitPair(String name, int bit) { |
64 | this(name, bit, false); |
65 | } |
66 | public NameBitPair(String name, int bit, boolean all_missing) { |
67 | this.name = name; |
68 | this.bit = bit; |
69 | this.all_missing = all_missing; |
70 | } |
71 | public String getName() { |
72 | return name; |
73 | } |
74 | public boolean isMatch(int bit) { |
75 | if (all_missing) { |
76 | return (bit & this.bit) == 0; |
77 | } else { |
78 | return (bit & this.bit) != 0; |
79 | } |
80 | } |
81 | public static String toString(int mod) { |
82 | return toString(pairs, mod); |
83 | } |
84 | public static String toString(NameBitPair[] pairs, int mod) { |
85 | StringBuffer result = new StringBuffer(); |
86 | for (int i = 0; i < pairs.length; i++) { |
87 | if (pairs[i].isMatch(mod)) { |
88 | result.append(pairs[i].getName()); |
89 | } |
90 | } |
91 | return result.toString(); |
92 | } |
93 | private String name; |
94 | private int bit; |
95 | private boolean all_missing; |
96 | |
97 | private static NameBitPair[] pairs = { |
98 | new NameBitPair("public: ", Modifier.PUBLIC), |
99 | new NameBitPair("protected: ", Modifier.PROTECTED), |
100 | new NameBitPair("private: ", Modifier.PRIVATE), |
101 | new NameBitPair("public: ", Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE, true), |
102 | }; |
103 | } |
104 | private String getFieldModifiers(Field field) { |
105 | return NameBitPair.toString(field.getModifiers()); |
106 | } |
107 | } |