Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating field names as string constants #2084

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions querydsl-apt/src/main/java/com/querydsl/apt/APTOptions.java
Expand Up @@ -96,6 +96,11 @@ public final class APTOptions {
*/
public static final String QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS = "querydsl.variableNameFunctionClass";

/**
* generate field names as public static constants
*/
public static final String QUERYDSL_NAMES_AS_CONSTANTS = "querydsl.namesAsConstants";

private APTOptions() { }

}
Expand Up @@ -13,28 +13,63 @@
*/
package com.querydsl.apt;

import static com.querydsl.apt.APTOptions.*;

import java.lang.annotation.Annotation;
import java.util.*;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.TypeMirror;

import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.mysema.codegen.model.ClassType;
import com.querydsl.codegen.*;
import com.querydsl.codegen.CodegenModule;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why all these package refactorings?
still to the necessary changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge it as is? As it's been a while without update.

import com.querydsl.codegen.DefaultVariableNameFunction;
import com.querydsl.codegen.EmbeddableSerializer;
import com.querydsl.codegen.EntitySerializer;
import com.querydsl.codegen.EntityType;
import com.querydsl.codegen.ProjectionSerializer;
import com.querydsl.codegen.QueryTypeFactory;
import com.querydsl.codegen.Serializer;
import com.querydsl.codegen.SerializerConfig;
import com.querydsl.codegen.SimpleSerializerConfig;
import com.querydsl.codegen.SupertypeSerializer;
import com.querydsl.codegen.TypeMappings;
import com.querydsl.core.annotations.Config;
import com.querydsl.core.annotations.QueryProjection;
import com.querydsl.core.annotations.QueryType;
import com.querydsl.core.types.Expression;
import com.querydsl.core.util.Annotations;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.querydsl.apt.APTOptions.QUERYDSL_CREATE_DEFAULT_VARIABLE;
import static com.querydsl.apt.APTOptions.QUERYDSL_ENTITY_ACCESSORS;
import static com.querydsl.apt.APTOptions.QUERYDSL_EXCLUDED_CLASSES;
import static com.querydsl.apt.APTOptions.QUERYDSL_EXCLUDED_PACKAGES;
import static com.querydsl.apt.APTOptions.QUERYDSL_NAMES_AS_CONSTANTS;
import static com.querydsl.apt.APTOptions.QUERYDSL_INCLUDED_CLASSES;
import static com.querydsl.apt.APTOptions.QUERYDSL_INCLUDED_PACKAGES;
import static com.querydsl.apt.APTOptions.QUERYDSL_LIST_ACCESSORS;
import static com.querydsl.apt.APTOptions.QUERYDSL_MAP_ACCESSORS;
import static com.querydsl.apt.APTOptions.QUERYDSL_PACKAGE_SUFFIX;
import static com.querydsl.apt.APTOptions.QUERYDSL_PREFIX;
import static com.querydsl.apt.APTOptions.QUERYDSL_SUFFIX;
import static com.querydsl.apt.APTOptions.QUERYDSL_UNKNOWN_AS_EMBEDDABLE;
import static com.querydsl.apt.APTOptions.QUERYDSL_USE_FIELDS;
import static com.querydsl.apt.APTOptions.QUERYDSL_USE_GETTERS;
import static com.querydsl.apt.APTOptions.QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS;

/**
* {@code DefaultConfiguration} is a simple implementation of the {@link Configuration} interface.
*
Expand Down Expand Up @@ -205,6 +240,12 @@ public DefaultConfiguration(
}
module.bind(CodegenModule.VARIABLE_NAME_FUNCTION_CLASS, variableNameFunction);

boolean namesAsConstants = options.containsKey(QUERYDSL_NAMES_AS_CONSTANTS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferably use the same pattern as elsewhere

if (options.containsKey(QUERYDSL_NAMES_AS_CONSTANTS)) {
    module.bind(CodegenModule.NAMES_AS_CONSTANTS, Boolean.valueOf(options.get(QUERYDSL_NAMES_AS_CONSTANTS)))
}

? Boolean.valueOf(options.get(QUERYDSL_NAMES_AS_CONSTANTS))
: false;

module.bind(CodegenModule.NAMES_AS_CONSTANTS, namesAsConstants);

try {
// register additional mappings if querydsl-spatial is on the classpath
Class.forName("com.querydsl.spatial.GeometryExpression");
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* 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.querydsl.apt;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class NamesAsConstantsTest extends AbstractProcessorTest {

private static final String packagePath = "src/test/java/com/querydsl/apt/domain/";

@Test
public void process_all() throws IOException {

List<String> classes = getFiles(packagePath);

// default Processor
process(QuerydslAnnotationProcessor.class, classes,"prefix");

File file = new File("target/prefix/com/querydsl/apt/domain/query3/QTAddress.java");
assertTrue(file.exists());

String content = Files.toString(file, Charsets.UTF_8);

assertTrue(content.contains("public static final String ENTITY_NAME = \"address\";"));
assertTrue(content.contains("public static final QTAddress address = new QTAddress(ENTITY_NAME);"));

assertTrue(content.contains("public static final String POST_CODE = \"postCode\";"));
assertTrue(content.contains("public final StringPath postCode = createString(POST_CODE);"));
}

@Override
protected Collection<String> getAPTOptions() {
return Arrays.asList("-Aquerydsl.namesAsConstants=true", "-Aquerydsl.packageSuffix=.query3", "-Aquerydsl.prefix=QT");
}

}
@@ -0,0 +1,29 @@
package com.querydsl.apt.domain;

import static org.junit.Assert.assertNotNull;

import java.io.Serializable;

import org.junit.Test;

import com.querydsl.core.annotations.QueryEntity;
import com.querydsl.core.annotations.QuerySupertype;

public class GenericStackOverflowTest extends AbstractTest {

public interface Identifiable<ID extends Comparable<ID> & Serializable> {
}

@QuerySupertype
public abstract static class AbstractEntity<ID extends Comparable<ID> & Serializable> implements Identifiable<ID> {
}

@QueryEntity
public static class TestEntity extends AbstractEntity<Long> {
}

@Test
public void test() {
assertNotNull(QGenericStackOverflowTest_AbstractEntity.abstractEntity);
}
}
Expand Up @@ -53,6 +53,11 @@ public class CodegenModule extends AbstractModule {
*/
public static final String VARIABLE_NAME_FUNCTION_CLASS = "variableNameFunction";

/**
* key for generating field names as public static constants
*/
public static final String NAMES_AS_CONSTANTS = "namesAsConstants";

@Override
protected void configure() {
bind(TypeMappings.class, JavaTypeMappings.class);
Expand All @@ -69,6 +74,7 @@ protected void configure() {
bind(KEYWORDS, Collections.<String>emptySet());
bind(IMPORTS, Collections.<String>emptySet());
bind(VARIABLE_NAME_FUNCTION_CLASS, DefaultVariableNameFunction.INSTANCE);
bind(NAMES_AS_CONSTANTS, false);
}

}
Expand Up @@ -13,22 +13,29 @@
*/
package com.querydsl.codegen;

import static com.mysema.codegen.Symbols.UNCHECKED;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Collection;

import javax.inject.Inject;
import javax.inject.Named;

import com.mysema.codegen.CodeWriter;
import com.mysema.codegen.model.ClassType;
import com.mysema.codegen.model.Type;
import com.mysema.codegen.model.TypeCategory;
import com.mysema.codegen.model.Types;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.dsl.*;
import com.querydsl.core.types.dsl.BeanPath;
import com.querydsl.core.types.dsl.BooleanPath;
import com.querydsl.core.types.dsl.ComparablePath;
import com.querydsl.core.types.dsl.DatePath;
import com.querydsl.core.types.dsl.DateTimePath;
import com.querydsl.core.types.dsl.EnumPath;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.core.types.dsl.TimePath;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Collection;

import static com.mysema.codegen.Symbols.UNCHECKED;

/**
* {@code EmbeddableSerializer} is a {@link Serializer} implementation for embeddable types
Expand All @@ -45,8 +52,12 @@ public final class EmbeddableSerializer extends EntitySerializer {
* @param keywords keywords to be used
*/
@Inject
public EmbeddableSerializer(TypeMappings typeMappings, @Named("keywords") Collection<String> keywords) {
super(typeMappings, keywords);
public EmbeddableSerializer(
TypeMappings typeMappings,
@Named("keywords") Collection<String> keywords,
@Named("namesAsConstants") boolean namesAsConstants
) {
super(typeMappings, keywords, namesAsConstants);
}

@Override
Expand Down