Skip to content

Commit e48f9f9

Browse files
committed
@Dao に Singleton をフィールドで提供するタイプの Config を指定することをサポート
Config が Kotlin の Object declarations で書かれている場合をサポートするためです。
1 parent 0d92b92 commit e48f9f9

File tree

8 files changed

+194
-16
lines changed

8 files changed

+194
-16
lines changed

src/main/java/org/seasar/doma/internal/apt/DaoGenerator.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,18 @@ protected void printStaticFields() {
291291
protected void printConstructors() {
292292
if (daoMeta.hasUserDefinedConfig()) {
293293
String singletonMethodName = daoMeta.getSingletonMethodName();
294+
String singletonFieldName = daoMeta.getSingletonFieldName();
294295

295296
iprint("/** */%n");
296297
iprint("public %1$s() {%n", simpleName);
297298
indent();
298299
if (singletonMethodName == null) {
299-
iprint("super(new %1$s());%n", daoMeta.getConfigType());
300+
if (singletonFieldName == null) {
301+
iprint("super(new %1$s());%n", daoMeta.getConfigType());
302+
} else {
303+
iprint("super(%1$s.%2$s);%n", daoMeta.getConfigType(),
304+
singletonFieldName);
305+
}
300306
} else {
301307
iprint("super(%1$s.%2$s());%n", daoMeta.getConfigType(),
302308
singletonMethodName);
@@ -316,8 +322,13 @@ protected void printConstructors() {
316322
Connection.class.getName());
317323
indent();
318324
if (singletonMethodName == null) {
319-
iprint("super(new %1$s(), connection);%n",
320-
daoMeta.getConfigType());
325+
if (singletonFieldName == null) {
326+
iprint("super(new %1$s(), connection);%n",
327+
daoMeta.getConfigType());
328+
} else {
329+
iprint("super(%1$s.%2$s, connection);%n",
330+
daoMeta.getConfigType(), singletonFieldName);
331+
}
321332
} else {
322333
iprint("super(%1$s.%2$s(), connection);%n",
323334
daoMeta.getConfigType(), singletonMethodName);
@@ -332,8 +343,13 @@ protected void printConstructors() {
332343
DataSource.class.getName());
333344
indent();
334345
if (singletonMethodName == null) {
335-
iprint("super(new %1$s(), dataSource);%n",
336-
daoMeta.getConfigType());
346+
if (singletonFieldName == null) {
347+
iprint("super(new %1$s(), dataSource);%n",
348+
daoMeta.getConfigType());
349+
} else {
350+
iprint("super(%1$s.%2$s, dataSource);%n",
351+
daoMeta.getConfigType(), singletonFieldName);
352+
}
337353
} else {
338354
iprint("super(%1$s.%2$s(), dataSource);%n",
339355
daoMeta.getConfigType(), singletonMethodName);
@@ -544,8 +560,8 @@ public Void visitSqlFileSelectQueryMeta(final SqlFileSelectQueryMeta m,
544560
iprint("%1$s __result = __command.execute();%n",
545561
returnMeta.getTypeName());
546562
iprint("__query.complete();%n");
547-
iprint("exiting(\"%1$s\", \"%2$s\", __result);%n", canonicalName,
548-
m.getName());
563+
iprint("exiting(\"%1$s\", \"%2$s\", __result);%n",
564+
canonicalName, m.getName());
549565
iprint("return __result;%n");
550566
} else {
551567
if (m.getSelectStrategyType() == SelectType.STREAM) {
@@ -562,8 +578,8 @@ public Void visitSqlFileSelectQueryMeta(final SqlFileSelectQueryMeta m,
562578
if ("void".equals(returnMeta.getTypeName())) {
563579
iprint("__command.execute();%n");
564580
iprint("__query.complete();%n");
565-
iprint("exiting(\"%1$s\", \"%2$s\", null);%n", canonicalName,
566-
m.getName());
581+
iprint("exiting(\"%1$s\", \"%2$s\", null);%n",
582+
canonicalName, m.getName());
567583
} else {
568584
iprint("%1$s __result = __command.execute();%n",
569585
returnMeta.getTypeName());
@@ -1138,8 +1154,8 @@ protected void printEnteringStatements(QueryMeta m) {
11381154
}
11391155

11401156
protected void printArrayCreateEnteringStatements(ArrayCreateQueryMeta m) {
1141-
iprint("entering(\"%1$s\", \"%2$s\", (Object)%3$s);%n", canonicalName,
1142-
m.getName(), m.getParameterName());
1157+
iprint("entering(\"%1$s\", \"%2$s\", (Object)%3$s);%n",
1158+
canonicalName, m.getName(), m.getParameterName());
11431159
iprint("try {%n");
11441160
indent();
11451161
}

src/main/java/org/seasar/doma/internal/apt/meta/DaoMeta.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class DaoMeta implements TypeElementMeta {
5555

5656
protected String singletonMethodName;
5757

58+
protected String singletonFieldName;
59+
5860
public DaoMeta(DaoMirror daoMirror) {
5961
assertNotNull(daoMirror);
6062
this.daoMirror = daoMirror;
@@ -158,4 +160,12 @@ public void setSingletonMethodName(String singletonMethodName) {
158160
this.singletonMethodName = singletonMethodName;
159161
}
160162

163+
public String getSingletonFieldName() {
164+
return singletonFieldName;
165+
}
166+
167+
public void setSingletonFieldName(String singletonFieldName) {
168+
this.singletonFieldName = singletonFieldName;
169+
}
170+
161171
}

src/main/java/org/seasar/doma/internal/apt/meta/DaoMetaFactory.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.EnumSet;
2727
import java.util.HashSet;
2828
import java.util.List;
29+
import java.util.Optional;
2930
import java.util.Set;
3031

3132
import javax.annotation.processing.Filer;
@@ -34,6 +35,7 @@
3435
import javax.lang.model.element.ExecutableElement;
3536
import javax.lang.model.element.Modifier;
3637
import javax.lang.model.element.TypeElement;
38+
import javax.lang.model.element.VariableElement;
3739
import javax.lang.model.type.DeclaredType;
3840
import javax.lang.model.type.TypeMirror;
3941
import javax.lang.model.util.ElementFilter;
@@ -63,6 +65,8 @@
6365
*/
6466
public class DaoMetaFactory implements TypeElementMetaFactory<DaoMeta> {
6567

68+
protected static final String SINGLETON_CONFIG_FIELD_NAME = "INSTANCE";
69+
6670
protected final ProcessingEnvironment env;
6771

6872
protected final List<QueryMetaFactory> queryMetaFactories = new ArrayList<QueryMetaFactory>();
@@ -130,10 +134,25 @@ protected void validateUserDefinedConfig(TypeElement configElement,
130134
configElement, env);
131135
if (constructor == null
132136
|| !constructor.getModifiers().contains(Modifier.PUBLIC)) {
133-
throw new AptException(Message.DOMA4164, env,
134-
daoMeta.getDaoElement(),
135-
daoMirror.getAnnotationMirror(), daoMirror.getConfig(),
136-
configElement.getQualifiedName());
137+
Optional<VariableElement> field = ElementFilter
138+
.fieldsIn(configElement.getEnclosedElements())
139+
.stream()
140+
.filter(e -> e.getSimpleName().contentEquals(
141+
SINGLETON_CONFIG_FIELD_NAME))
142+
.filter(e -> e.getModifiers().containsAll(
143+
EnumSet.of(Modifier.STATIC, Modifier.PUBLIC,
144+
Modifier.FINAL)))
145+
.filter(e -> TypeMirrorUtil.isAssignable(e.asType(),
146+
Config.class, env)).findFirst();
147+
if (field.isPresent()) {
148+
daoMeta.setSingletonFieldName(SINGLETON_CONFIG_FIELD_NAME);
149+
} else {
150+
throw new AptException(Message.DOMA4164, env,
151+
daoMeta.getDaoElement(),
152+
daoMirror.getAnnotationMirror(),
153+
daoMirror.getConfig(),
154+
configElement.getQualifiedName());
155+
}
137156
}
138157
} else {
139158
String methodName = singletonConfig.method();

src/main/java/org/seasar/doma/message/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public enum Message implements MessageResource {
310310
DOMA4160("java.lang.Iterableのサブタイプをワイルカード型にしてはいけません。"),
311311
DOMA4161("SQLファイル[{0}]の妥当検査に失敗しました([{2}]行目[{3}]番目の文字付近)。括弧の前に位置するバインド変数[{4}]に対応するパラメータの型は基本型もしくはドメインクラスを要素としたjava.lang.Iterableのサブタイプでなければいけません。しかし、実際の型は[{5}]です。型を間違えていませんか?もしくはフィールドアクセスやメソッド呼び出しの記述を忘れていませんか?。SQL[{1}]"),
312312
DOMA4163("ユーザー定義の設定クラスは抽象型であってはいけません。クラス[{0}]は抽象型です。"),
313-
DOMA4164("ユーザー定義の設定クラスは引数なしのpublicなコンストラクタを持たなければいけません。クラス[{0}]には引数なしのpublicなコンストラクタが見つかりません。"),
313+
DOMA4164("ユーザー定義の設定クラス[{0}]には、引数なしのpublicなコンストラクタ、もしくはINSTANCEという名前のpublic static finalなフィールドが必要です。INSTANCEフィールドの型はorg.seasar.doma.Configのサブタイプでなければいけません。"),
314314
DOMA4166("エンティティリスナークラスは抽象型であってはいけません。クラス[{0}]は抽象型です。"),
315315
DOMA4167("エンティティリスナークラスは引数なしのpublicなコンストラクタを持たなければいけません。クラス[{0}]には引数なしのpublicなコンストラクタが見つかりません。"),
316316
DOMA4168("org.seasar.doma.jdbc.id.TableIdGeneratorの実装クラスは抽象型であってはいけません。クラス[{0}]は抽象型です。"),

src/test/java/org/seasar/doma/internal/apt/dao/DaoProcessorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,4 +988,14 @@ public void testResultStream() throws Exception {
988988
assertTrue(getCompiledResult());
989989
}
990990

991+
public void testPlainSingletonConfig() throws Exception {
992+
Class<?> target = PlainSingletonConfigDao.class;
993+
DaoProcessor processor = new DaoProcessor();
994+
addProcessor(processor);
995+
addCompilationUnit(target);
996+
compile();
997+
assertGeneratedSource(target);
998+
assertTrue(getCompiledResult());
999+
}
1000+
9911001
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.internal.apt.dao;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.seasar.doma.jdbc.Config;
21+
import org.seasar.doma.jdbc.dialect.Dialect;
22+
23+
/**
24+
* @author nakamura-to
25+
*
26+
*/
27+
public class PlainSingletonConfig implements Config {
28+
29+
public static final PlainSingletonConfig INSTANCE = new PlainSingletonConfig();
30+
31+
private PlainSingletonConfig() {
32+
}
33+
34+
@Override
35+
public DataSource getDataSource() {
36+
return null;
37+
}
38+
39+
@Override
40+
public Dialect getDialect() {
41+
return null;
42+
}
43+
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.internal.apt.dao;
17+
18+
import org.seasar.doma.Dao;
19+
20+
/**
21+
* @author nakamura-to
22+
*
23+
*/
24+
@Dao(config = PlainSingletonConfig.class)
25+
public interface PlainSingletonConfigDao {
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.seasar.doma.internal.apt.dao;
2+
3+
/** */
4+
@javax.annotation.Generated(value = { "Doma", "@VERSION@" }, date = "1970-01-01T09:00:00.000+0900")
5+
public class PlainSingletonConfigDaoImpl extends org.seasar.doma.internal.jdbc.dao.AbstractDao implements org.seasar.doma.internal.apt.dao.PlainSingletonConfigDao {
6+
7+
static {
8+
org.seasar.doma.internal.Artifact.validateVersion("@VERSION@");
9+
}
10+
11+
/** */
12+
public PlainSingletonConfigDaoImpl() {
13+
super(org.seasar.doma.internal.apt.dao.PlainSingletonConfig.INSTANCE);
14+
}
15+
16+
/**
17+
* @param connection the connection
18+
*/
19+
public PlainSingletonConfigDaoImpl(java.sql.Connection connection) {
20+
super(org.seasar.doma.internal.apt.dao.PlainSingletonConfig.INSTANCE, connection);
21+
}
22+
23+
/**
24+
* @param dataSource the dataSource
25+
*/
26+
public PlainSingletonConfigDaoImpl(javax.sql.DataSource dataSource) {
27+
super(org.seasar.doma.internal.apt.dao.PlainSingletonConfig.INSTANCE, dataSource);
28+
}
29+
30+
/**
31+
* @param config the configuration
32+
*/
33+
protected PlainSingletonConfigDaoImpl(org.seasar.doma.jdbc.Config config) {
34+
super(config);
35+
}
36+
37+
/**
38+
* @param config the configuration
39+
* @param connection the connection
40+
*/
41+
protected PlainSingletonConfigDaoImpl(org.seasar.doma.jdbc.Config config, java.sql.Connection connection) {
42+
super(config, connection);
43+
}
44+
45+
/**
46+
* @param config the configuration
47+
* @param dataSource the dataSource
48+
*/
49+
protected PlainSingletonConfigDaoImpl(org.seasar.doma.jdbc.Config config, javax.sql.DataSource dataSource) {
50+
super(config, dataSource);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)