private String getDomainType(DomainDeclaredType instantiableType, boolean isCastRequired) { return isCastRequired ? "((" + instantiableType.getSimpleName() + ")" + DOMAIN_NAME + ")" : DOMAIN_NAME; }
private DomainDeclaredType replaceTypeParamsByWildcard(DomainDeclaredType domainDeclared) { if (domainDeclared.getTypeVariables().size() > 0) { MutableTypeVariable[] typeVariables = new MutableTypeVariable[domainDeclared.getTypeVariables().size()]; for (int i = 0; i < domainDeclared.getTypeVariables().size(); i++) { typeVariables[i] = envContext.getProcessingEnv().getTypeUtils().getTypeVariable(MutableWildcardType.WILDCARD_NAME); } domainDeclared.setTypeVariables(typeVariables); } return domainDeclared; } }
@Override public void print(ConverterProviderPrinterContext context) { if (types.size() == 0) { initializeDomainConverterMethod(); } if (!types.contains(context.getRawDomain().getCanonicalName())) { types.add(context.getRawDomain().getCanonicalName()); } } }
private void printDomainTypeVariables(Set<? extends MutableTypeMirror> types, ExecutableElement localMethod, ConverterProviderElementPrinter nestedPrinter) { for (MutableTypeMirror type: types) { DomainType domainType = processingEnv.getTransferObjectUtils().getDomainType(type); if (domainType.getKind().isDeclared() && domainType.getConverter() != null) { ServiceConverterProviderPrinterContext context = new ServiceConverterProviderPrinterContext((DomainDeclaredType)domainType, localMethod); nestedPrinter.print(context); } } }
protected void printCopyByLocalConverter(String domainField, DomainType domainMethodReturnType, String dtoField, FormattedPrintWriter pw) { pw.println("} else {"); pw.print(TransferObjectElementPrinter.RESULT_NAME + "." + MethodHelper.toSetter(dtoField)); if (domainMethodReturnType.getKind().equals(MutableTypeKind.TYPEVAR)) { pw.print("((" + ConverterTypeElement.DTO_TYPE_ARGUMENT_PREFIX + "_" + ((MutableTypeVariable)domainMethodReturnType).getVariable() + ")"); } else { pw.print("((", domainMethodReturnType.getDto(), ")"); } pw.print(TransferObjectElementPrinter.DOMAIN_NAME + "." + domainField); pw.println(");"); pw.println("}"); } }
private Set<? extends MutableTypeMirror> processBounds(Set<? extends MutableTypeMirror> bounds) { if (bounds.size() > 0) { Set<MutableTypeMirror> newBounds = new HashSet<MutableTypeMirror>(); Iterator<? extends MutableTypeMirror> iterator = bounds.iterator(); while (iterator.hasNext()) { MutableTypeMirror next = iterator.next(); if (next.getKind().isDeclared()) { List<MutableDeclaredType> dataInterfaces = new ArrayList<MutableDeclaredType>(); findDomainData(((MutableDeclaredType)next), dataInterfaces); if (dataInterfaces.size() > 0) { MutableDeclaredType dtoType = (MutableDeclaredType)envContext.getProcessingEnv().getTransferObjectUtils().getDomainType(next).getDto(); DomainDeclaredType domainDeclared = new DataDomainDeclared(dataInterfaces.get(0), dtoType, envContext, null/*, configurationContext*/); domainDeclared.setTypeVariables(new MutableTypeVariable[]{}); if (!hasCustomProperties((MutableDeclaredType) next, dataInterfaces.get(0))) { newBounds.add(domainDeclared); } else { newBounds.add(next); } } else { newBounds.add(next); } } else { newBounds.add(next); } } return newBounds; } return bounds; }
protected boolean copy(TransferObjectContext context, FormattedPrintWriter pw, CopyMethodPrinter printer) { DomainType returnType = context.getDomainMethodReturnType(); switch (returnType.getKind()) { case VOID: processingEnv.getMessager().printMessage(Kind.ERROR, "[ERROR] Unable to process " + context.getDtoFieldName() + ". Unsupported result type: " + returnType.getKind()); return false; case ARRAY: //TODO return false; } printer.printCopyMethod(context, pw); return true; } }
/** * Prints the definition of the hashCode method with the initial prechecks */ @Override public void initialize(ConfigurationTypeElement configurationTypeElement, MutableDeclaredType outputType) { this.outputType = outputType; GenerateHashcodeAccessor generateHashcodeAccessor = new GenerateHashcodeAccessor(configurationTypeElement.asConfigurationElement(), processingEnv); active = generateHashcodeAccessor.generate(); equalsType = generateHashcodeAccessor.getType(); hasKey = configurationTypeElement.hasKey(); if (!active) { return; } if (equalsType.equals(TraversalType.CYCLIC_SAFE)) { pw.println("private boolean processingHashCode = false;"); } pw.println(""); pw.println("@Override"); pw.println("public int hashCode() {"); pw.println("final int prime = 31;"); //TODO we should call also super.hashcode - but only when there is any superclass with implemented hashcode method DomainDeclaredType superClass = configurationTypeElement.getInstantiableDomain().getSuperClass(); if (superClass != null && superClass.getDomainDefinitionConfiguration() != null && new GenerateEqualsAccessor(superClass.getDomainDefinitionConfiguration().asConfigurationElement(), processingEnv).generate()) { pw.println("int result = super.hashCode();"); } else { pw.println("int result = 1;"); } }
protected void printDomainInstancer(FormattedPrintWriter pw, DomainDeclaredType domainTypeElement) { if (!entityResolver.shouldHaveIdMethod(domainTypeElement)) { pw.println(" return new ", domainTypeElement, "();"); } else { pw.println(domainTypeElement, " " + RESULT_NAME, " = new ", domainTypeElement, "();"); String setterMethod = MethodHelper.toSetter(domainTypeElement.getIdMethod(entityResolver)); if (ProcessorUtils.hasMethod(setterMethod, domainTypeElement.asElement())) { pw.println(RESULT_NAME + "." + MethodHelper.toSetter(domainTypeElement.getIdMethod(entityResolver)) + "((", domainTypeElement.getId(entityResolver), ")" + "id);"); } else { pw.println("if (id != null) {"); pw.println("throw new ", RuntimeException.class, "(\"Unable to define ID for imutable entity. Please define " + setterMethod + " method for ", domainTypeElement, "!\");"); pw.println("}"); } pw.println("return " + RESULT_NAME + ";"); } } }
protected void printCopyNested(PathResolver domainPathResolver, String fullPath, DomainDeclaredType referenceDomainType, ExecutableElement method, FormattedPrintWriter pw, Field field, String dtoName) { DomainDeclaredType instantiableDomain = (DomainDeclaredType)referenceDomainType.getConverter().getInstantiableDomain(); pw.print(referenceDomainType, " " + domainPathResolver.getCurrent() + " = "); if (instantiableDomain.getId(entityResolver) != null) { if (referenceDomainType.getConverter() == null) { processingEnv.getMessager().printMessage(Kind.ERROR, "[ERROR] No converter/configuration for " + referenceDomainType + " was found. Please, define configuration for " + referenceDomainType); } converterProviderPrinter.printObtainConverterFromCache(pw, ConverterTargetType.DTO, referenceDomainType, field, method, true); pw.println(".createDomainInstance(" + dtoName + "." + MethodHelper.toGetter(fullPath + MethodHelper.toMethod(MethodHelper.toField(instantiableDomain.getIdMethod(entityResolver)))) + ");"); } else { pw.println(TransferObjectElementPrinter.RESULT_NAME + "." + MethodHelper.toGetter(domainPathResolver.getCurrent()) + ";"); pw.println("if (" + domainPathResolver.getCurrent() + " == null) {"); pw.print(domainPathResolver.getCurrent() + " = "); // TODO add NPE check converterProviderPrinter.printObtainConverterFromCache(pw, ConverterTargetType.DTO, referenceDomainType, field, method, true); pw.println(".createDomainInstance(null);"); pw.println("}"); } }
public MutableTypeMirror getDelegateCast(TypeMirror domainType) { DomainType domainTypeElement = processingEnv.getTransferObjectUtils().getDomainType(domainType); MutableTypeMirror domainNamedType = processingEnv.getTypeUtils().toMutableType(domainType); return castToDelegate(domainNamedType, domainTypeElement.getDomainDefinitionConfiguration() == null ? null : domainTypeElement.getDomainDefinitionConfiguration().getDelegateConfigurationTypeElement(), true); }
@Override protected void printType(MutableTypeMirror type, ConverterProviderPrinterContext context) { DomainType domainType = processingEnv.getTransferObjectUtils().getDomainType(type); if (domainType.getKind().isDeclared() && domainType.getConverter() != null) { context = new ServiceConverterProviderPrinterContext((DomainDeclaredType)domainType, ((ServiceConverterProviderPrinterContext)context).getLocalMethod()); print(context); } }
protected void printCopyByLocalConverter(String localConverterName, PathResolver domainPathResolver, DomainType domainMethodReturnType, String dtoField, FormattedPrintWriter pw) { pw.println("if (" + localConverterName + " != null) {"); pw.print(TransferObjectElementPrinter.RESULT_NAME + "." + MethodHelper.toSetter(domainPathResolver.getPath()) + "(" + localConverterName + ".fromDto(" + TransferObjectElementPrinter.DTO_NAME + "." + MethodHelper.toGetter(dtoField) + ")"); pw.println(");"); pw.println("} else {"); pw.print(TransferObjectElementPrinter.RESULT_NAME + "." + MethodHelper.toSetter(domainPathResolver.getPath())); if (domainMethodReturnType.getKind().equals(MutableTypeKind.TYPEVAR)) { pw.print("((" + ConverterTypeElement.DOMAIN_TYPE_ARGUMENT_PREFIX + "_" + ((MutableTypeVariable) domainMethodReturnType).getVariable() + ")"); } else { pw.print("((" + domainMethodReturnType + ")"); } pw.print(TransferObjectElementPrinter.DTO_NAME + "." + MethodHelper.toGetter(dtoField)); pw.println(");"); pw.println("}"); } }
pw.println(" return false;"); DomainDeclaredType superClass = configurationTypeElement.getInstantiableDomain().getSuperClass(); if (superClass != null && superClass.getDomainDefinitionConfiguration() != null && new GenerateEqualsAccessor(superClass.getDomainDefinitionConfiguration().asConfigurationElement(), processingEnv).generate()) { pw.println("if (!super.equals(obj))"); pw.println(" return false;");
protected ExecutableElement getSetterMethod(TransferObjectContext context, PathResolver domainPathResolver) { TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(context.getConfigurationTypeElement().getDomain().getCanonicalName()); return getSetterMethod(typeElement, domainPathResolver); //return ProcessorUtils.getMethod(MethodHelper.toSetter(domainPathResolver.getPath()), typeElement)); }
private void printDomainType(DomainDeclaredType instantiableType, boolean isCastRequired, FormattedPrintWriter pw) { if (isCastRequired) { pw.print("((", instantiableType.getSimpleName(), ")" + DOMAIN_NAME + ")"); } else { pw.print(DOMAIN_NAME); } }
protected MutableTypeMirror getDelegateCast(MutableTypeMirror domainNamedType, boolean stripWildcard) { TypeMirror domainType = processingEnv.getTypeUtils().fromMutableType(domainNamedType); if (domainType == null) { return domainNamedType; } DomainType domainTypeElement = processingEnv.getTransferObjectUtils().getDomainType(domainType); return castToDelegate(domainNamedType, domainTypeElement.getDomainDefinitionConfiguration() == null ? null : domainTypeElement.getDomainDefinitionConfiguration().getDelegateConfigurationTypeElement(), stripWildcard); }
protected boolean isId(TransferObjectContext context) { return (processingEnv.getTypeUtils().isAssignable(processingEnv.getElementUtils().getTypeElement(context.getConfigurationTypeElement().getInstantiableDomain().getCanonicalName()).asType(), context.getDomainMethod().getEnclosingElement().asType()) && isIdField(context.getConfigurationTypeElement().getInstantiableDomain(), context.getDomainMethod().getSimpleName().toString())); }
protected String processMethodBody(String body) { Set<? extends Element> configurations = getClassPathTypes().getElementsAnnotatedWith(TransferObjectMapping.class, roundEnv);//processingEnv.getEnvironmentContext().getRoundEnv().getElementsAnnotatedWith(TransferObjectMapping.class); for (Element configuration: configurations) { if (configuration.getAnnotation(Generated.class) == null && (configuration.getKind().isClass() || configuration.getKind().isInterface())) { ConfigurationContext configurationContext = new ConfigurationContext(processingEnv.getEnvironmentContext().getConfigurationEnv()); ConfigurationTypeElement configurationTypeElement = new ConfigurationTypeElement(configuration, processingEnv.getEnvironmentContext(), configurationContext); configurationContext.addConfiguration(configurationTypeElement); String domainElementName = configurationTypeElement.getInstantiableDomainSpecified() == null ? null : configurationTypeElement.getInstantiableDomainSpecified().getSimpleName().toString(); if (domainElementName != null) { body = replaceDomainType(domainElementName, configurationTypeElement.getDto(), body); } domainElementName = configurationTypeElement.getDomain().getSimpleName(); body = replaceDomainType(domainElementName, configurationTypeElement.getDto(), body); } } return body; }
private TransferObjectContext createTOContext(ExecutableElement method, DomainDeclaredType domainTypeElement, boolean superClassMethod, ConfigurationTypeElement configurationTypeElement, List<String> generated) { String fieldName = TransferObjectHelper.getFieldPath(method); if (contains(generated, fieldName)) { return null; } TypeElement domainElement = environmentContext.getProcessingEnv().getElementUtils().getTypeElement(domainTypeElement.getCanonicalName()); ExecutableElement overrider = ProcessorUtils.getOverrider(domainElement, method, processingEnv); if (overrider != null) { method = overrider; } TransferObjectContext context = transferObjectContextProvider.get(configurationTypeElement, Modifier.PUBLIC, method, method, superClassMethod); if (context == null) { return null; } generated.add(fieldName); return context; }