1515 */
1616package org .seasar .doma .internal .apt ;
1717
18+ import java .io .IOException ;
19+ import java .io .InputStream ;
20+ import java .io .InputStreamReader ;
21+ import java .util .Collections ;
1822import java .util .Date ;
23+ import java .util .HashMap ;
24+ import java .util .Map ;
25+ import java .util .Properties ;
1926
27+ import javax .annotation .processing .Filer ;
2028import javax .annotation .processing .ProcessingEnvironment ;
29+ import javax .tools .FileObject ;
30+ import javax .tools .StandardLocation ;
2131
2232import org .seasar .doma .internal .Artifact ;
2333
@@ -47,12 +57,14 @@ public final class Options {
4757
4858 public static final String VERSION_VALIDATION = "doma.version.validation" ;
4959
60+ public static final String CONFIG_PATH = "doma.config.path" ;
61+
5062 public static final String LOMBOK_ALL_ARGS_CONSTRUCTOR = "doma.lombok.AllArgsConstructor" ;
5163
5264 public static final String LOMBOK_VALUE = "doma.lombok.Value" ;
5365
5466 public static boolean isTestEnabled (ProcessingEnvironment env ) {
55- String test = env . getOptions (). get ( Options .TEST );
67+ String test = getOption ( env , Options .TEST );
5668 return Boolean .valueOf (test ).booleanValue ();
5769 }
5870
@@ -71,69 +83,120 @@ public static Date getDate(ProcessingEnvironment env) {
7183 }
7284
7385 public static boolean isDebugEnabled (ProcessingEnvironment env ) {
74- String debug = env . getOptions (). get ( Options .DEBUG );
86+ String debug = getOption ( env , Options .DEBUG );
7587 return Boolean .valueOf (debug ).booleanValue ();
7688 }
7789
7890 public static String getDaoPackage (ProcessingEnvironment env ) {
79- String pkg = env . getOptions (). get ( Options .DAO_PACKAGE );
91+ String pkg = getOption ( env , Options .DAO_PACKAGE );
8092 return pkg != null ? pkg : null ;
8193 }
8294
8395 public static String getDaoSubpackage (ProcessingEnvironment env ) {
84- String subpackage = env . getOptions (). get ( Options .DAO_SUBPACKAGE );
96+ String subpackage = getOption ( env , Options .DAO_SUBPACKAGE );
8597 return subpackage != null ? subpackage : null ;
8698 }
8799
88100 public static String getDaoSuffix (ProcessingEnvironment env ) {
89- String suffix = env . getOptions (). get ( Options .DAO_SUFFIX );
101+ String suffix = getOption ( env , Options .DAO_SUFFIX );
90102 return suffix != null ? suffix : Constants .DEFAULT_DAO_SUFFIX ;
91103 }
92104
93105 public static String getEntityFieldPrefix (ProcessingEnvironment env ) {
94- String prefix = env . getOptions (). get ( Options .ENTITY_FIELD_PREFIX );
106+ String prefix = getOption ( env , Options .ENTITY_FIELD_PREFIX );
95107 if ("none" .equalsIgnoreCase (prefix )) {
96108 return "" ;
97109 }
98110 return prefix != null ? prefix : Constants .DEFAULT_ENTITY_FIELD_PREFIX ;
99111 }
100112
101113 public static String getExprFunctions (ProcessingEnvironment env ) {
102- String name = env . getOptions (). get ( Options .EXPR_FUNCTIONS );
114+ String name = getOption ( env , Options .EXPR_FUNCTIONS );
103115 return name != null ? name : null ;
104116 }
105117
106118 public static String getDomainConverters (ProcessingEnvironment env ) {
107- String converters = env . getOptions (). get ( Options .DOMAIN_CONVERTERS );
119+ String converters = getOption ( env , Options .DOMAIN_CONVERTERS );
108120 return converters != null ? converters : null ;
109121 }
110122
111123 public static boolean getSqlValidation (ProcessingEnvironment env ) {
112- String v = env . getOptions (). get ( Options .SQL_VALIDATION );
124+ String v = getOption ( env , Options .SQL_VALIDATION );
113125 return v != null ? Boolean .valueOf (v ).booleanValue () : true ;
114126 }
115127
116128 public static boolean getVersionValidation (ProcessingEnvironment env ) {
117- String v = env . getOptions (). get ( Options .VERSION_VALIDATION );
129+ String v = getOption ( env , Options .VERSION_VALIDATION );
118130 return v != null ? Boolean .valueOf (v ).booleanValue () : true ;
119131 }
120132
133+ public static String getConfigPath (ProcessingEnvironment env ) {
134+ String configPath = env .getOptions ().get (Options .CONFIG_PATH );
135+ return configPath != null ? configPath : Constants .DEFAULT_CONFIG_PATH ;
136+ }
137+
121138 public static String getLombokAllArgsConstructor (ProcessingEnvironment env ) {
122- String name = env . getOptions (). get ( Options .LOMBOK_ALL_ARGS_CONSTRUCTOR );
139+ String name = getOption ( env , Options .LOMBOK_ALL_ARGS_CONSTRUCTOR );
123140 return name != null ? name : Constants .DEFAULT_LOMBOK_ALL_ARGS_CONSTRUCTOR ;
124141 }
125142
126143 public static String getLombokValue (ProcessingEnvironment env ) {
127- String name = env . getOptions (). get ( Options .LOMBOK_VALUE );
144+ String name = getOption ( env , Options .LOMBOK_VALUE );
128145 return name != null ? name : Constants .DEFAULT_LOMBOK_VALUE ;
129146 }
130147
148+ private static String getOption (ProcessingEnvironment env , String key ) {
149+ String v = env .getOptions ().get (key );
150+ if (v != null ) {
151+ return v ;
152+ }
153+
154+ return getConfig (env ).get (key );
155+ }
156+
157+ private static Map <String , Map <String , String >> configCache = new HashMap <>();
158+ private static Map <String , String > getConfig (ProcessingEnvironment env ) {
159+ FileObject config = getFileObject (env , "" , getConfigPath (env ));
160+ if (config == null ) {
161+ return Collections .emptyMap ();
162+ }
163+ return configCache .computeIfAbsent (config .toUri ().getPath (), configPath -> {
164+ try {
165+ return loadProperties (config );
166+ } catch (IOException e ) {
167+ return Collections .emptyMap ();
168+ }
169+ });
170+ }
171+
172+ public static FileObject getFileObject (ProcessingEnvironment env , String pkg , String relativeName ) {
173+ Filer filer = env .getFiler ();
174+
175+ try {
176+ return filer .getResource (StandardLocation .CLASS_OUTPUT , pkg , relativeName );
177+ } catch (IOException e ) {
178+ return null ;
179+ }
180+ }
181+
182+ @ SuppressWarnings ("unchecked" )
183+ private static Map <String , String > loadProperties (FileObject config ) throws IOException {
184+ try (InputStream is = config .openInputStream ();
185+ InputStreamReader isr = new InputStreamReader (is , "UTF-8" )){
186+ Properties props = new Properties ();
187+ props .load (isr );
188+ return (Map <String , String >) new HashMap (props );
189+ }
190+ }
191+
131192 protected static class Constants {
132193
133194 public static final String DEFAULT_DAO_SUFFIX = "Impl" ;
134195
135196 public static final String DEFAULT_ENTITY_FIELD_PREFIX = "$" ;
136197
198+ public static final String DEFAULT_CONFIG_PATH = "doma.config" ;
199+
137200 public static final String DEFAULT_LOMBOK_ALL_ARGS_CONSTRUCTOR = "lombok.AllArgsConstructor" ;
138201
139202 public static final String DEFAULT_LOMBOK_VALUE = "lombok.Value" ;
0 commit comments