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 6 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
@@ -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 @@ -13,24 +13,67 @@
*/
package com.querydsl.codegen;

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

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

import javax.annotation.Generated;
import javax.inject.Inject;
import javax.inject.Named;

import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mysema.codegen.CodeWriter;
import com.mysema.codegen.model.*;
import com.querydsl.core.types.*;
import com.querydsl.core.types.dsl.*;
import com.mysema.codegen.model.ClassType;
import com.mysema.codegen.model.Constructor;
import com.mysema.codegen.model.Parameter;
import com.mysema.codegen.model.SimpleType;
import com.mysema.codegen.model.Type;
import com.mysema.codegen.model.TypeCategory;
import com.mysema.codegen.model.TypeExtends;
import com.mysema.codegen.model.Types;
import com.querydsl.core.types.ConstructorExpression;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.PathMetadata;
import com.querydsl.core.types.PathMetadataFactory;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.ArrayPath;
import com.querydsl.core.types.dsl.BooleanPath;
import com.querydsl.core.types.dsl.CollectionPath;
import com.querydsl.core.types.dsl.ComparableExpression;
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.EntityPathBase;
import com.querydsl.core.types.dsl.EnumPath;
import com.querydsl.core.types.dsl.ListPath;
import com.querydsl.core.types.dsl.MapPath;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.core.types.dsl.PathInits;
import com.querydsl.core.types.dsl.SetPath;
import com.querydsl.core.types.dsl.SimpleExpression;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.core.types.dsl.TimePath;

import javax.annotation.Generated;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.mysema.codegen.Symbols.ASSIGN;
import static com.mysema.codegen.Symbols.COMMA;
import static com.mysema.codegen.Symbols.DOT;
import static com.mysema.codegen.Symbols.EMPTY;
import static com.mysema.codegen.Symbols.NEW;
import static com.mysema.codegen.Symbols.QUOTE;
import static com.mysema.codegen.Symbols.RETURN;
import static com.mysema.codegen.Symbols.SEMICOLON;
import static com.mysema.codegen.Symbols.STAR;
import static com.mysema.codegen.Symbols.SUPER;
import static com.mysema.codegen.Symbols.THIS;
import static com.mysema.codegen.Symbols.UNCHECKED;

/**
* {@code EntitySerializer} is a {@link Serializer} implementation for entity types
Expand Down Expand Up @@ -620,6 +663,10 @@ public void serialize(EntityType model, SerializerConfig config,

protected void serialize(EntityType model, Property field, Type type, CodeWriter writer,
String factoryMethod, String... args) throws IOException {

String constantFieldName = constantFieldName(field.getEscapedName());
writer.publicStaticFinal(new ClassType(String.class), constantFieldName, "\"" + field.getName() + "\"");
Copy link
Member

Choose a reason for hiding this comment

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

use Types.String instead of new ClassType(String.class)
also could these additions be toggable with an option?

Copy link
Author

Choose a reason for hiding this comment

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

@timowest sure, how would you prefer me passing the option? A code example can help.

Copy link
Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

@oleg-smith,
Probably option to toggle this feature could be added as @nAmed constructor param like keywords param in EntitySerializer constructor.
@Named("keywords") Collection<String> keywords

Then default value for it could be binded in CodegenModule.java:69
bind(KEYWORDS, Collections.<String>emptySet());

And in DefaultConfiguration.java you could rebind it to a value gotten from options map.

if (options.containsKey(QUERYDSL_PACKAGE_SUFFIX)) {
            module.bind(CodegenModule.PACKAGE_SUFFIX, Strings.nullToEmpty(options.get(QUERYDSL_PACKAGE_SUFFIX)));
        }

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the radio silence.
Yes, that sounds good.
You can kind of see what places you need to update in this PR.


Supertype superType = model.getSuperType();
// construct value
StringBuilder value = new StringBuilder();
Expand All @@ -628,7 +675,7 @@ protected void serialize(EntityType model, Property field, Type type, CodeWriter
value.append("_super." + field.getEscapedName());
}
} else {
value.append(factoryMethod + "(\"" + field.getName() + QUOTE);
value.append(factoryMethod + "(" + constantFieldName);
for (String arg : args) {
value.append(COMMA + arg);
}
Expand All @@ -646,6 +693,14 @@ protected void serialize(EntityType model, Property field, Type type, CodeWriter
}
}

private String constantFieldName(String fieldName) {
String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, fieldName);
if (name.equals(fieldName)) {
name = "_" + fieldName;
}
return name;
}

protected void customField(EntityType model, Property field, SerializerConfig config,
CodeWriter writer) throws IOException {
Type queryType = typeMappings.getPathType(field.getType(), model, false);
Expand Down
Expand Up @@ -13,25 +13,29 @@
*/
package com.querydsl.sql.codegen;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.querydsl.core.alias.Gender;
import com.querydsl.sql.*;
import com.querydsl.sql.AbstractJDBCTest;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.EncryptedString;
import com.querydsl.sql.HSQLDBTemplates;
import com.querydsl.sql.QPerson;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.dml.SQLInsertClause;
import com.querydsl.sql.dml.SQLUpdateClause;
import com.querydsl.sql.types.EnumByNameType;
import com.querydsl.sql.types.StringType;
import com.querydsl.sql.types.UtilDateType;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class CustomTypesTest extends AbstractJDBCTest {

Expand Down Expand Up @@ -77,7 +81,7 @@ public void export() throws SQLException, IOException {
exporter.export(connection.getMetaData());
String person = Files.toString(new File("target/customExport/test/QPerson.java"), Charsets.UTF_8);
//System.err.println(person);
assertTrue(person.contains("createEnum(\"gender\""));
assertTrue(person.contains("createEnum(GENDER"));
}

@Test
Expand Down