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

ColumnReader NullPointerException when finding a setSomething() method that is not a property mutator #917

Merged
merged 16 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

package io.spine.server.entity.storage;

import com.google.common.collect.ImmutableSet;
import io.spine.server.entity.Entity;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newLinkedList;
Expand Down Expand Up @@ -84,6 +88,7 @@ static ColumnReader forClass(Class<? extends Entity> entityClass) {
* @throws IllegalStateException
* if entity column definitions are incorrect
*/
@SuppressWarnings("ConstantConditions")
Collection<EntityColumn> readColumns() {
BeanInfo entityDescriptor;
try {
Expand All @@ -92,24 +97,26 @@ Collection<EntityColumn> readColumns() {
throw new IllegalStateException(e);
}

Collection<EntityColumn> entityColumns = newLinkedList();
PropertyDescriptor[] propertyDescriptors = entityDescriptor.getPropertyDescriptors();
Arrays.stream(propertyDescriptors)
.filter(ColumnReader::hasAccessor)
.filter(ColumnReader::accessorIsAnnotated)
.map(PropertyDescriptor::getReadMethod)
.forEach(readMethod -> entityColumns.add(EntityColumn.from(readMethod)));

checkRepeatedColumnNames(entityColumns);
return entityColumns;

Set<EntityColumn> annotatedColumns = Arrays.stream(propertyDescriptors)
.map(PropertyDescriptor::getReadMethod)
serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved
.filter(ColumnReader::hasAccessor)
.filter(ColumnReader::accessorIsAnnotated)
.map(EntityColumn::from)
.collect(Collectors.toSet());
serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved

checkRepeatedColumnNames(annotatedColumns);
return ImmutableSet.copyOf(annotatedColumns);
}

private static boolean accessorIsAnnotated(PropertyDescriptor descriptor) {
return getAnnotatedVersion(descriptor.getReadMethod()).isPresent();
private static boolean accessorIsAnnotated(Method method) {
serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved
return getAnnotatedVersion(method).isPresent();
}

private static boolean hasAccessor(PropertyDescriptor descriptor) {
return descriptor.getReadMethod() != null;
@SuppressWarnings("ConstantConditions")
serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved
private static boolean hasAccessor(Method method) {
return method != null;
}

/**
Expand All @@ -123,6 +130,7 @@ private static boolean hasAccessor(PropertyDescriptor descriptor) {
* if columns contain repeated names
*/
private void checkRepeatedColumnNames(Iterable<EntityColumn> columns) {

serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved
Collection<String> checkedNames = newLinkedList();
for (EntityColumn column : columns) {
String columnName = column.getStoredName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ void fromImplementedInterface() {
}

@Test
@DisplayName("not confuse a `setSomething()` method with a property mutator")
@DisplayName("not confuse a setter method with a property mutator")
void testSetterDeclaringEntity(){
serhii-lekariev marked this conversation as resolved.
Show resolved Hide resolved
ColumnReader columnReader = forClass(EntityWithASetterButNoGetter.class);
Collection<EntityColumn> entityColumns = columnReader.readColumns();
assertThat(entityColumns).hasSize(1);
assertThat(entityColumns).isEmpty();
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ protected EntityWithASetterButNoGetter(String id) {
super(id);
}

@Column
public Integer getFortyThree(){
return 43;
}

@SuppressWarnings("WeakerAccess") // Required for a test
public void setSecretNumber(Integer secretNumber) {
this.secretNumber = secretNumber;
Expand Down