private void addSupertypeFields(EntityType model, Set<EntityType> handled) { if (handled.add(model)) { for (Supertype supertype : model.getSuperTypes()) { EntityType entityType = context.allTypes.get(supertype.getType().getFullName()); if (entityType != null) { addSupertypeFields(entityType, handled); supertype.setEntityType(entityType); model.include(supertype); } } } }
@Nullable public EntityType getEntityType(TypeMirror typeMirror, boolean deep) { List<String> key = keyBuilder.visit(typeMirror, true); // get from cache if (entityTypeCache.containsKey(key)) { EntityType entityType = entityTypeCache.get(key); if (deep && entityType.getSuperTypes().isEmpty()) { for (Type superType : getSupertypes(typeMirror, deep)) { entityType.addSupertype(new Supertype(superType)); } } return entityType; // create } else { return createEntityType(typeMirror, key, deep); } }
private static Type resolveVar(Type resolved, String varName, Type declaringType, EntityType context) { // get parameter index of var in declaring type int index = -1; for (int i = 0; i < declaringType.getParameters().size(); i++) { Type param = unwrap(declaringType.getParameters().get(i)); if (Objects.equal(getVarName(param), varName)) { index = i; } } if (index == -1) { throw new IllegalStateException("Did not find type " + varName + " in " + declaringType + " for " + context); } Supertype type = context.getSuperType(); while (!type.getEntityType().equals(declaringType)) { type = type.getEntityType().getSuperType(); } if (!type.getType().getParameters().isEmpty()) { return type.getType().getParameters().get(index); } else { // raw type return resolved; } }
private void addExternalParents(EntityType entityType) { Deque<Type> superTypes = new ArrayDeque<Type>(); if (entityType.getSuperType() != null) { superTypes.push(entityType.getSuperType().getType()); } while (!superTypes.isEmpty()) { Type superType = superTypes.pop(); if (!context.allTypes.containsKey(superType.getFullName())) { TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(superType.getFullName()); if (typeElement == null) { throw new IllegalStateException("Found no type for " + superType.getFullName()); } if (conf.isStrictMode() && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) { continue; } EntityType superEntityType = elementHandler.handleEntityType(typeElement); if (superEntityType.getSuperType() != null) { superTypes.push(superEntityType.getSuperType().getType()); } context.allTypes.put(superType.getFullName(), superEntityType); } } }
private boolean superTypeHasEntityFields(EntityType model) { Supertype superType = model.getSuperType(); return null != superType && null != superType.getEntityType() && superType.getEntityType().hasEntityFields(); }
writer.beginClass(model, model.getSuperType().getType()); } else { writer.beginClass(model);
protected void serialize(EntityType model, Property field, Type type, CodeWriter writer, String factoryMethod, String... args) throws IOException { Supertype superType = model.getSuperType(); // construct value StringBuilder value = new StringBuilder(); if (field.isInherited() && superType != null) { if (!superType.getEntityType().hasEntityFields()) { value.append("_super." + field.getEscapedName()); } } else { value.append(factoryMethod + "(\"" + field.getName() + QUOTE); for (String arg : args) { value.append(COMMA + arg); } value.append(")"); } // serialize it if (field.isInherited()) { writer.line("//inherited"); } if (value.length() > 0) { writer.publicFinal(type, field.getEscapedName(), value.toString()); } else { writer.publicFinal(type, field.getEscapedName()); } }
private void addSupertypeFields(EntityType model, Map<String, EntityType> superTypes, Set<EntityType> handled) { if (handled.add(model)) { for (Supertype supertype : model.getSuperTypes()) { EntityType entityType = superTypes.get(supertype.getType().getFullName()); if (entityType == null) { if (supertype.getType().getPackageName().startsWith("java.")) { // skip internal supertypes continue; } // FIXME this misses the generics Class<?> cl = supertype.getType().getJavaClass(); typeFactory.addEmbeddableType(cl); entityType = createEntityType(cl, new HashMap<Class<?>, EntityType>()); addProperties(cl, entityType); } addSupertypeFields(entityType, superTypes, handled); supertype.setEntityType(entityType); model.include(supertype); } } }
public void include(Supertype supertype) { EntityType entityType = supertype.getEntityType(); for (Delegate delegate : entityType.getDelegates()) { addDelegate(delegate); } for (Property property : entityType.getProperties()) { addProperty(property.createCopy(this)); } }
private void addSupertypeFields(EntityType model, Map<Class<?>, EntityType> superTypes, Set<EntityType> handled) { if (handled.add(model)) { for (Supertype supertype : model.getSuperTypes()) { EntityType entityType = superTypes.get(supertype.getType().getJavaClass()); if (entityType != null) { addSupertypeFields(entityType, superTypes, handled); supertype.setEntityType(entityType); model.include(supertype); } } } }
@Nullable private EntityType createEntityType(TypeMirror typeMirror, List<String> key, boolean deep) { entityTypeCache.put(key, null); Type value = visitor.visit(typeMirror, deep); if (value != null) { EntityType entityType = null; if (value instanceof EntityType) { entityType = (EntityType) value; } else { entityType = new EntityType(value, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); } entityTypeCache.put(key, entityType); if (deep) { for (Type superType : getSupertypes(typeMirror, deep)) { entityType.addSupertype(new Supertype(superType)); } } return entityType; } else { return null; } }
protected void introSuper(CodeWriter writer, EntityType model) throws IOException { EntityType superType = model.getSuperType().getEntityType(); Type superQueryType = typeMappings.getPathType(superType, model, false); if (!superType.hasEntityFields()) { writer.publicFinal(superQueryType, "_super", NEW + writer.getRawName(superQueryType) + "(this)"); } else { writer.publicFinal(superQueryType, "_super"); } }
for (Map.Entry<Class<?>, EntityType> entry : allTypes.entrySet()) { EntityType entityType = entry.getValue(); if (entityType.getSuperType() != null && !allTypes.containsKey(entityType.getSuperType().getType().getJavaClass())) { additions.add(entityType.getSuperType()); type.setEntityType(createEntityType(type.getType(), this.superTypes));
@Test public void superType() throws IOException { EntityType superType = new EntityType(new SimpleType(TypeCategory.ENTITY, "Entity2", "", "Entity2",false,false)); SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type, Collections.singleton(new Supertype(superType, superType))); typeMappings.register(superType, queryTypeFactory.create(superType)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final QEntity2 _super = new QEntity2(this);")); //CompileUtils.assertCompiles("QEntity", writer.toString()); }
protected void initEntityFields(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException { Supertype superType = model.getSuperType(); if (superType != null) { EntityType entityType = superType.getEntityType(); if (entityType != null && entityType.hasEntityFields()) { Type superQueryType = typeMappings.getPathType(entityType, model, false); writer.line("this._super = new " + writer.getRawName(superQueryType) + "(type, metadata, inits);"); } } for (Property field : model.getProperties()) { if (field.getType().getCategory() == TypeCategory.ENTITY) { initEntityField(writer, config, model, field); } else if (field.isInherited() && superType != null && superType.getEntityType().hasEntityFields()) { writer.line("this.", field.getEscapedName(), " = _super.", field.getEscapedName(), SEMICOLON); } } }