use of javax.lang.model.type.TypeMirror in project immutables by immutables.
the class AccessorAttributesCollector method processGenerationCandidateMethod.
private void processGenerationCandidateMethod(ExecutableElement attributeMethodCandidate, TypeElement originalType) {
Name name = attributeMethodCandidate.getSimpleName();
if (CheckMirror.isPresent(attributeMethodCandidate)) {
if (!attributeMethodCandidate.getParameters().isEmpty() || attributeMethodCandidate.getModifiers().contains(Modifier.PRIVATE) || attributeMethodCandidate.getModifiers().contains(Modifier.ABSTRACT) || attributeMethodCandidate.getModifiers().contains(Modifier.STATIC) || attributeMethodCandidate.getModifiers().contains(Modifier.NATIVE)) {
report(attributeMethodCandidate).error("Method '%s' annotated with @%s must be non-private parameter-less method", name, CheckMirror.simpleName());
} else if (attributeMethodCandidate.getReturnType().getKind() == TypeKind.VOID) {
type.addNormalizeMethod(name.toString(), false);
} else if (returnsNormalizedAbstractValueType(attributeMethodCandidate)) {
type.addNormalizeMethod(name.toString(), true);
} else {
report(attributeMethodCandidate).error("Method '%s' annotated with @%s must return void or normalized instance of abstract value type", name, CheckMirror.simpleName());
}
return;
}
boolean useDefaultAsDefault = type.constitution.style().defaultAsDefault();
if (isDiscoveredAttribute(attributeMethodCandidate, useDefaultAsDefault)) {
TypeMirror returnType = resolveReturnType(attributeMethodCandidate);
ValueAttribute attribute = new ValueAttribute();
attribute.reporter = reporter;
attribute.returnType = returnType;
attribute.names = deriveNames(name.toString());
attribute.element = attributeMethodCandidate;
attribute.containingType = type;
boolean isFinal = isFinal(attributeMethodCandidate);
boolean isAbstract = isAbstract(attributeMethodCandidate);
boolean defaultAnnotationPresent = DefaultMirror.isPresent(attributeMethodCandidate);
boolean derivedAnnotationPresent = DerivedMirror.isPresent(attributeMethodCandidate);
if (isAbstract) {
attribute.isGenerateAbstract = true;
if (attributeMethodCandidate.getDefaultValue() != null) {
attribute.isGenerateDefault = true;
}
if (defaultAnnotationPresent || derivedAnnotationPresent) {
if (defaultAnnotationPresent) {
if (attribute.isGenerateDefault) {
report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).warning("@Value.Default annotation is superflous for default annotation attribute");
} else {
report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).error("@Value.Default attribute should have initializer body", name);
}
}
if (derivedAnnotationPresent) {
if (attribute.isGenerateDefault) {
report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("@Value.Derived cannot be used with default annotation attribute");
} else {
report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("@Value.Derived attribute should have initializer body", name);
}
}
}
} else if (defaultAnnotationPresent && derivedAnnotationPresent) {
report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("Attribute '%s' cannot be both @Value.Default and @Value.Derived", name);
attribute.isGenerateDefault = true;
} else if ((defaultAnnotationPresent || derivedAnnotationPresent) && isFinal) {
report(attributeMethodCandidate).error("Annotated attribute '%s' will be overriden and cannot be final", name);
} else if (defaultAnnotationPresent) {
attribute.isGenerateDefault = true;
if (useDefaultAsDefault && attribute.isInterfaceDefaultMethod()) {
report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).warning("@Value.Default annotation is superflous for default annotation attribute" + " when 'defaultAsDefault' style is enabled");
}
} else if (derivedAnnotationPresent) {
attribute.isGenerateDerived = true;
} else if (useDefaultAsDefault) {
attribute.isGenerateDefault = attribute.isInterfaceDefaultMethod();
}
if (LazyMirror.isPresent(attributeMethodCandidate)) {
if (isAbstract || isFinal) {
report(attributeMethodCandidate).error("@Value.Lazy attribute '%s' must be non abstract and non-final", name);
} else if (defaultAnnotationPresent || derivedAnnotationPresent) {
report(attributeMethodCandidate).error("@Value.Lazy attribute '%s' cannot be @Value.Derived or @Value.Default", name);
} else {
attribute.isGenerateLazy = true;
attribute.isGenerateDefault = false;
}
}
attributes.add(attribute);
// Compute this eagerly here, for no strong reason
if (attribute.isGenerateDefault) {
type.defaultAttributesCount++;
}
if (attribute.isGenerateDerived) {
type.derivedAttributesCount++;
}
if (attributeMethodCandidate.getEnclosingElement().equals(originalType)) {
hasNonInheritedAttributes = true;
}
}
}
use of javax.lang.model.type.TypeMirror in project immutables by immutables.
the class AccessorAttributesCollector method resolveReturnType.
static TypeMirror resolveReturnType(ProcessingEnvironment processing, ExecutableElement method, TypeElement typeElement) {
method = CachingElements.getDelegate(method);
TypeMirror returnType = method.getReturnType();
// our target class could not define formal type parameters also.
if (returnType.getKind() == TypeKind.TYPEVAR) {
return asInheritedMemberReturnType(processing, typeElement, method);
} else if (returnType.getKind() == TypeKind.DECLARED || returnType.getKind() == TypeKind.ERROR) {
if (!((DeclaredType) returnType).getTypeArguments().isEmpty()) {
return asInheritedMemberReturnType(processing, typeElement, method);
}
}
return returnType;
}
use of javax.lang.model.type.TypeMirror in project immutables by immutables.
the class TypeStringProvider method appendTypeArguments.
private void appendTypeArguments(TypeMirror type, DeclaredType declaredType) {
List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
if (!arguments.isEmpty()) {
buffer.append('<');
boolean notFirst = false;
for (TypeMirror argument : arguments) {
typeAnnotationHandle(argument);
if (notFirst) {
buffer.append(',').append(' ');
}
notFirst = true;
int mark = buffer.length();
caseType(argument);
cutTypeArgument(type, mark);
}
buffer.append('>');
}
}
use of javax.lang.model.type.TypeMirror in project immutables by immutables.
the class ValueType method getNonAttributeAbstractMethodSignatures.
public Set<String> getNonAttributeAbstractMethodSignatures() {
if (element.getKind().isClass() || element.getKind().isInterface()) {
Set<String> signatures = new LinkedHashSet<>();
List<? extends Element> members = constitution.protoclass().environment().processing().getElementUtils().getAllMembers(CachingElements.getDelegate((TypeElement) element));
for (ExecutableElement m : ElementFilter.methodsIn(members)) {
if (!m.getParameters().isEmpty() || m.getSimpleName().contentEquals(AccessorAttributesCollector.HASH_CODE_METHOD) || m.getSimpleName().contentEquals(AccessorAttributesCollector.TO_STRING_METHOD)) {
if (m.getModifiers().contains(Modifier.ABSTRACT)) {
TypeMirror returnType = m.getReturnType();
if (!AccessorAttributesCollector.isEclipseImplementation(m)) {
returnType = AccessorAttributesCollector.asInheritedMemberReturnType(constitution.protoclass().processing(), CachingElements.getDelegate((TypeElement) element), m);
}
signatures.add(toSignature(m, returnType));
}
}
}
return signatures;
}
return Collections.emptySet();
}
use of javax.lang.model.type.TypeMirror in project hibernate-orm by hibernate.
the class JPAMetaModelEntityProcessor method modelGenerationNeedsToBeDeferred.
private boolean modelGenerationNeedsToBeDeferred(Collection<MetaEntity> entities, MetaEntity containedEntity) {
ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(containedEntity.getTypeElement(), context);
for (MetaEntity entity : entities) {
if (entity.equals(containedEntity)) {
continue;
}
for (Element subElement : ElementFilter.fieldsIn(entity.getTypeElement().getEnclosedElements())) {
TypeMirror mirror = subElement.asType();
if (!TypeKind.DECLARED.equals(mirror.getKind())) {
continue;
}
boolean contains = mirror.accept(visitor, subElement);
if (contains) {
return true;
}
}
for (Element subElement : ElementFilter.methodsIn(entity.getTypeElement().getEnclosedElements())) {
TypeMirror mirror = subElement.asType();
if (!TypeKind.DECLARED.equals(mirror.getKind())) {
continue;
}
boolean contains = mirror.accept(visitor, subElement);
if (contains) {
return true;
}
}
}
return false;
}
Aggregations