Merge pull request #1024 from querydsl/i1023

JDO uses field names in queries
This commit is contained in:
Timo Westkämper 2014-11-03 19:03:02 +02:00
commit 8cb13d54ec
5 changed files with 116 additions and 10 deletions

View File

@ -42,13 +42,15 @@ public enum VisitorConfig {
private final boolean visitFieldProperties, visitMethodProperties, visitConstructors;
public static VisitorConfig get(boolean fields, boolean methods) {
if (fields && !methods) {
public static VisitorConfig get(boolean fields, boolean methods, VisitorConfig defaultConfig) {
if (fields && methods) {
return VisitorConfig.ALL;
} else if (fields && !methods) {
return VisitorConfig.FIELDS_ONLY;
} else if (methods && !fields) {
return VisitorConfig.METHODS_ONLY;
} else {
return VisitorConfig.ALL;
return defaultConfig;
}
}

View File

@ -25,8 +25,6 @@ import com.mysema.query.annotations.QueryEntities;
import com.mysema.query.annotations.QuerySupertype;
import com.mysema.query.apt.AbstractQuerydslProcessor;
import com.mysema.query.apt.Configuration;
import com.mysema.query.apt.DefaultConfiguration;
import com.mysema.query.codegen.Keywords;
/**
* AnnotationProcessor for JDO which takes {@link PersistenceCapable}, {@link EmbeddedOnly} and
@ -46,7 +44,7 @@ public class JDOAnnotationProcessor extends AbstractQuerydslProcessor {
Class<? extends Annotation> embeddable = EmbeddedOnly.class;
Class<? extends Annotation> embedded = QueryEmbedded.class;
Class<? extends Annotation> skip = NotPersistent.class;
return new DefaultConfiguration(roundEnv, processingEnv.getOptions(), Keywords.JDO,
return new JDOConfiguration(roundEnv, processingEnv.getOptions(),
entities, entity, superType, embeddable, embedded, skip);
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2014 Timo Westkämper
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.apt.jdo;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import javax.annotation.processing.RoundEnvironment;
import javax.jdo.annotations.*;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import com.google.common.collect.ImmutableSet;
import com.mysema.query.apt.DefaultConfiguration;
import com.mysema.query.apt.VisitorConfig;
import com.mysema.query.codegen.Keywords;
public class JDOConfiguration extends DefaultConfiguration {
@SuppressWarnings("unchecked")
private static final Iterable<Class<? extends Annotation>> relevantAnnotations
= ImmutableSet.of(
Cacheable.class, Column.class, Columns.class,
javax.jdo.annotations.Element.class, Embedded.class,
Extension.class, Extensions.class, ForeignKey.class,
Index.class, Join.class, Key.class, NotPersistent.class,
Order.class, Persistent.class, PrimaryKey.class,
Serialized.class, Transactional.class, Unique.class, Value.class);
public JDOConfiguration(RoundEnvironment roundEnv,
Map<String, String> options,
Class<? extends Annotation> entitiesAnn,
Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn,
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> embeddedAnn, Class<? extends Annotation> skipAnn) {
super(roundEnv, options, Keywords.JDO, entitiesAnn, entityAnn, superTypeAnn,
embeddableAnn, embeddedAnn, skipAnn);
}
@Override
public VisitorConfig getConfig(TypeElement e, List<? extends Element> elements) {
boolean fields = false, methods = false;
for (Element element : elements) {
if (hasRelevantAnnotation(element)) {
fields |= element.getKind().equals(ElementKind.FIELD);
methods |= element.getKind().equals(ElementKind.METHOD);
}
}
return VisitorConfig.get(fields, methods, VisitorConfig.FIELDS_ONLY);
}
private boolean hasRelevantAnnotation(Element element) {
for (Class<? extends Annotation> relevantAnnotation : relevantAnnotations) {
if (element.getAnnotation(relevantAnnotation) != null) {
return true;
}
}
return false;
}
}

View File

@ -86,7 +86,7 @@ public class JPAConfiguration extends DefaultConfiguration {
methods |= element.getKind().equals(ElementKind.METHOD);
}
}
return VisitorConfig.get(fields, methods);
return VisitorConfig.get(fields, methods, VisitorConfig.ALL);
}
@Override

View File

@ -15,11 +15,14 @@ package com.mysema.query.domain;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.mysema.query.types.path.NumberPath;
import com.mysema.query.types.path.StringPath;
import org.junit.Test;
import com.mysema.query.types.path.StringPath;
public class JDOTest extends AbstractTest {
@PersistenceCapable
@ -37,16 +40,37 @@ public class JDOTest extends AbstractTest {
@PersistenceCapable
public static class JDOEntity2 {
@SuppressWarnings("unused")
private String stringField1;
private String stringField2;
public String getStringfield1() {
return stringField1;
}
public String getStringField2() {
return stringField2;
}
}
@PersistenceCapable
public static class JDOEntity3 {
private Integer integerField;
private String stringField;
@PrimaryKey
public Integer getId() {
return integerField;
}
@Persistent
public String getName() {
return stringField;
}
}
@Test
public void test() throws SecurityException, NoSuchFieldException {
cl = QJDOTest_JDOEntity.class;
@ -56,7 +80,12 @@ public class JDOTest extends AbstractTest {
cl = QJDOTest_JDOEntity2.class;
match(StringPath.class, "stringField1");
assertMissing("stringfield1");
match(StringPath.class, "stringField2");
cl = QJDOTest_JDOEntity3.class;
match(NumberPath.class, "id");
match(StringPath.class, "name");
}
}