Skip to content

Commit 8de2c5a

Browse files
authored
Revert "Remove invalid association handling and related tests (#1285)" (#1286)
2 parents 9e058d3 + a933020 commit 8de2c5a

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/entity/EntityMetaFactory.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.seasar.doma.internal.apt.annot.MetamodelAnnot;
5757
import org.seasar.doma.internal.apt.annot.TableAnnot;
5858
import org.seasar.doma.internal.apt.annot.ValueAnnot;
59+
import org.seasar.doma.internal.apt.cttype.CtType;
60+
import org.seasar.doma.internal.apt.cttype.EntityCtType;
5961
import org.seasar.doma.internal.apt.meta.TypeElementMetaFactory;
6062
import org.seasar.doma.internal.apt.util.AnnotationValueUtil;
6163
import org.seasar.doma.jdbc.entity.EntityListener;
@@ -596,10 +598,16 @@ void doOriginalStatesField(
596598
}
597599

598600
void doAssociationPropertyMeta(
599-
@SuppressWarnings("unused") TypeElement classElement,
600-
VariableElement fieldElement,
601-
EntityMeta entityMeta) {
601+
TypeElement classElement, VariableElement fieldElement, EntityMeta entityMeta) {
602602
validateFieldAnnotation(fieldElement, entityMeta);
603+
CtType ctType = ctx.getCtTypes().newCtType(fieldElement.asType());
604+
EntityCtType entityCtType = EntityCtType.resolveEntityCtType(ctType);
605+
if (entityCtType == null) {
606+
throw new AptException(
607+
Message.DOMA4485,
608+
fieldElement,
609+
new Object[] {fieldElement.getSimpleName(), classElement, fieldElement.asType()});
610+
}
603611
AssociationPropertyMeta associationPropertyMeta =
604612
new AssociationPropertyMeta(fieldElement.getSimpleName().toString());
605613
entityMeta.addAssociationPropertyMeta(associationPropertyMeta);

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/entity/association/EntityProcessorTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.seasar.doma.internal.apt.processor.entity.association;
1717

18+
import static org.junit.jupiter.api.Assertions.assertFalse;
1819
import static org.junit.jupiter.api.Assertions.assertTrue;
1920

2021
import java.net.URL;
@@ -33,6 +34,7 @@
3334
import org.seasar.doma.internal.apt.ResourceParameterResolver;
3435
import org.seasar.doma.internal.apt.SimpleParameterResolver;
3536
import org.seasar.doma.internal.apt.processor.EntityProcessor;
37+
import org.seasar.doma.message.Message;
3638

3739
class EntityProcessorTest extends CompilerSupport {
3840

@@ -116,4 +118,45 @@ public List<Extension> getAdditionalExtensions() {
116118
};
117119
}
118120
}
121+
122+
@TestTemplate
123+
@ExtendWith(ErrorInvocationContextProvider.class)
124+
void error(Class<?> clazz, Message message, String... options) throws Exception {
125+
addOption(options);
126+
addCompilationUnit(clazz);
127+
compile();
128+
assertFalse(getCompiledResult());
129+
assertMessage(message);
130+
}
131+
132+
static class ErrorInvocationContextProvider implements TestTemplateInvocationContextProvider {
133+
@Override
134+
public boolean supportsTestTemplate(ExtensionContext context) {
135+
return true;
136+
}
137+
138+
@Override
139+
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
140+
ExtensionContext context) {
141+
return Stream.of(invocationContext(IllegalAssociation.class, Message.DOMA4485));
142+
}
143+
144+
private TestTemplateInvocationContext invocationContext(
145+
Class<?> clazz, Message message, String... options) {
146+
return new TestTemplateInvocationContext() {
147+
@Override
148+
public String getDisplayName(int invocationIndex) {
149+
return clazz.getSimpleName();
150+
}
151+
152+
@Override
153+
public List<Extension> getAdditionalExtensions() {
154+
return Arrays.asList(
155+
new SimpleParameterResolver(clazz),
156+
new SimpleParameterResolver(message),
157+
new SimpleParameterResolver(options));
158+
}
159+
};
160+
}
161+
}
119162
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Doma Authors
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+
* https://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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.internal.apt.processor.entity.association;
17+
18+
import org.seasar.doma.Association;
19+
import org.seasar.doma.Entity;
20+
21+
@Entity
22+
class IllegalAssociation {
23+
24+
@Association String value;
25+
}

0 commit comments

Comments
 (0)