Skip to content

Commit

Permalink
update types (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcekan authored and aklish committed Sep 30, 2019
1 parent b3f913c commit c948337
Show file tree
Hide file tree
Showing 37 changed files with 87 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public boolean ok(T record, RequestScope requestScope, Optional<ChangeSpec> chan
return false;
}

Collection originalCollection = (Collection) changeSpec.get().getOriginal();
Collection modifiedCollection = (Collection) changeSpec.get().getModified();
Collection<?> originalCollection = (Collection<?>) changeSpec.get().getOriginal();
Collection<?> modifiedCollection = (Collection<?>) changeSpec.get().getModified();

return collectionIsSuperset(originalCollection, modifiedCollection);
}
Expand All @@ -55,15 +55,15 @@ public boolean ok(T record, RequestScope requestScope, Optional<ChangeSpec> chan
return false;
}

Collection originalCollection = (Collection) changeSpec.get().getOriginal();
Collection modifiedCollection = (Collection) changeSpec.get().getModified();
Collection<?> originalCollection = (Collection<?>) changeSpec.get().getOriginal();
Collection<?> modifiedCollection = (Collection<?>) changeSpec.get().getModified();

return collectionIsSuperset(modifiedCollection, originalCollection);
}

}

private static boolean collectionIsSuperset(Collection baseCollection, Collection potentialSuperset) {
private static boolean collectionIsSuperset(Collection<?> baseCollection, Collection<?> potentialSuperset) {
return (potentialSuperset.size() >= baseCollection.size())
&& (potentialSuperset.containsAll(baseCollection));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Common {
public static class UpdateOnCreate<T> extends OperationCheck<T> {
@Override
public boolean ok(T record, RequestScope requestScope, Optional<ChangeSpec> changeSpec) {
for (PersistentResource resource : requestScope.getNewResources()) {
for (PersistentResource<?> resource : requestScope.getNewResources()) {
if (record == resource.getObject()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -390,16 +391,11 @@ public static Selection field(String name, String value, boolean quoted) {
* @see <a href="https://graphql.org/learn/schema/#object-types-and-fields">Object Types and Fields</a>
*/
public static Selection field(String name, SelectionSet... selectionSet) {
List<SelectionSet> ss = Arrays.stream(selectionSet)
.map(i -> (SelectionSet) i)
.collect(Collectors.toList());
return new Field(null, name, Arguments.emptyArgument(), relayWrap(ss));
return field(null, name, selectionSet);
}

public static Selection field(String alias, String name, SelectionSet... selectionSet) {
List<SelectionSet> ss = Arrays.stream(selectionSet)
.map(i -> (SelectionSet) i)
.collect(Collectors.toList());
List<SelectionSet> ss = ImmutableList.copyOf(selectionSet);
return new Field(alias, name, Arguments.emptyArgument(), relayWrap(ss));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* The type Attributes.
*/
public class Attributes extends LinkedHashMap {
public class Attributes extends LinkedHashMap<String, Object> {

/**
* Instantiates a new Attributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* The type Relationships.
*/
public class Relationships extends LinkedHashMap<String, Map> {
public class Relationships extends LinkedHashMap<String, Map<String, ?>> {

/**
* Instantiates a new Relationships.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
}
annotations.putIfAbsent(annotationClass, annotation);
}
return annotation == NO_ANNOTATION ? null : (A) annotation;
return annotation == NO_ANNOTATION ? null : annotationClass.cast(annotation);
}

private List<Class<?>> getInheritedTypes(Class<?> entityClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public boolean matchesId(String checkId) {
}
}

PersistentResource<T> resource = new PersistentResource(obj, null, requestScope.getUUIDFor(obj), requestScope);
PersistentResource<T> resource = new PersistentResource<T>(
loadClass.cast(obj), null, requestScope.getUUIDFor(obj), requestScope);
// No need to have read access for a newly created object
if (!requestScope.getNewResources().contains(resource)) {
resource.checkFieldAwarePermissions(ReadPermission.class);
Expand Down Expand Up @@ -266,9 +267,8 @@ public static Set<PersistentResource> loadRecords(
if (shouldSkipCollection(loadClass, ReadPermission.class, requestScope)) {
if (ids.isEmpty()) {
return Collections.emptySet();
} else {
throw new InvalidObjectIdentifierException(ids.toString(), dictionary.getJsonAliasFor(loadClass));
}
throw new InvalidObjectIdentifierException(ids.toString(), dictionary.getJsonAliasFor(loadClass));
}


Expand Down Expand Up @@ -805,23 +805,22 @@ public Set<PersistentResource> getRelation(String relation,
/* If this is a bulk edit request and the ID we are fetching for is newly created... */
if (entityType == null) {
throw new InvalidAttributeException(relation, type);
} else {
if (!ids.isEmpty()) {
// Fetch our set of new resources that we know about since we can't find them in the datastore
newResources = requestScope.getNewPersistentResources().stream()
.filter(resource -> entityType.isAssignableFrom(resource.getResourceClass())
&& ids.contains(resource.getUUID().orElse("")))
.collect(Collectors.toSet());
}
if (!ids.isEmpty()) {
// Fetch our set of new resources that we know about since we can't find them in the datastore
newResources = requestScope.getNewPersistentResources().stream()
.filter(resource -> entityType.isAssignableFrom(resource.getResourceClass())
&& ids.contains(resource.getUUID().orElse("")))
.collect(Collectors.toSet());

FilterExpression idExpression = buildIdFilterExpression(ids, entityType, dictionary, requestScope);
FilterExpression idExpression = buildIdFilterExpression(ids, entityType, dictionary, requestScope);

// Combine filters if necessary
filterExpression = filter
.map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe))
.orElse(idExpression);
} else {
filterExpression = filter.orElse(null);
}
// Combine filters if necessary
filterExpression = filter
.map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe))
.orElse(idExpression);
} else {
filterExpression = filter.orElse(null);
}

// TODO: Filter on new resources?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected RequestScope(String path, JsonApiDocument jsonApiDocument, RequestScop

@Override
public Set<com.yahoo.elide.security.PersistentResource> getNewResources() {
return (Set<com.yahoo.elide.security.PersistentResource>) (Set<?>) newPersistentResources;
return (Set) newPersistentResources;
}

public boolean isNewResource(Object entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ public Object loadObject(Class<?> entityClass,
if (! filterExpression.isPresent()
|| tx.supportsFiltering(entityClass, filterExpression.get()) == FeatureSupport.FULL) {
return tx.loadObject(entityClass, id, filterExpression, scope);
} else {
return DataStoreTransaction.super.loadObject(entityClass, id, filterExpression, scope);
}
return DataStoreTransaction.super.loadObject(entityClass, id, filterExpression, scope);
}

@Override
Expand Down Expand Up @@ -432,9 +431,8 @@ private Pair<Optional<Sorting>, Optional<Sorting>> splitSorting(
) {
if (sorting.isPresent() && (! tx.supportsSorting(entityClass, sorting.get()) || filteredInMemory)) {
return Pair.of(Optional.empty(), sorting);
} else {
return Pair.of(sorting, Optional.empty());
}
return Pair.of(sorting, Optional.empty());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ public PaginationStatus visitPAREN(ExpressionParser.PARENContext ctx) {

@Override
public PaginationStatus visitPermissionClass(ExpressionParser.PermissionClassContext ctx) {
Check check = getCheck(dictionary, ctx.getText());
Check<?> check = getCheck(dictionary, ctx.getText());

//Filter expression checks can always be pushed to the DataStore so pagination is possible
if (FilterExpressionCheck.class.isAssignableFrom(check.getClass())) {
if (check instanceof FilterExpressionCheck) {
return PaginationStatus.CAN_PAGINATE;
}
if (UserCheck.class.isAssignableFrom(check.getClass())) {
if (check instanceof UserCheck) {
if (check.ok(scope.getUser())) {
return PaginationStatus.USER_CHECK_TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public FilterExpression visitPermissionClass(ExpressionParser.PermissionClassCon
return filterExpression;
}

if (UserCheck.class.isAssignableFrom(check.getClass())) {
if (check instanceof UserCheck) {
boolean userCheckResult = check.ok(requestScope.getUser());
return userCheckResult ? TRUE_USER_CHECK_EXPRESSION : FALSE_USER_CHECK_EXPRESSION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) {
parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject));
return () -> {
JsonApiDocument returnDoc = new JsonApiDocument();
returnDoc.setData(new Data(newObject.toResource()));
returnDoc.setData(new Data<>(newObject.toResource()));
JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class);
return Pair.of(HttpStatus.SC_CREATED, responseBody);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class FilterExpressionCheck<T> extends InlineCheck<T> {
* @param requestScope Request scope object
* @return FilterExpression for FilterExpressionCheck.
*/
public abstract FilterExpression getFilterExpression(Class<?> entityClass, RequestScope requestScope);
public abstract FilterExpression getFilterExpression(Class entityClass, RequestScope requestScope);

/* NOTE: Filter Expression checks and user checks are intended to be _distinct_ */
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ public <A extends Annotation> ExpressionResult checkPermission(Class<A> annotati
if (SharePermission.class == annotationClass) {
if (requestScope.getDictionary().isShareable(resource.getResourceClass())) {
return expressionBuilder.buildAnyFieldExpressions(resource, ReadPermission.class, changeSpec);
} else {
return PermissionExpressionBuilder.FAIL_EXPRESSION;
}
return PermissionExpressionBuilder.FAIL_EXPRESSION;
}
return expressionBuilder.buildAnyFieldExpressions(resource, annotationClass, changeSpec);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class CoerceUtil {
public static <T> T coerce(Object value, Class<T> cls) {
initializeCurrentClassLoaderIfNecessary();

if (value == null || cls == null || cls.isAssignableFrom(value.getClass())) {
if (value == null || cls == null || cls.isInstance(value)) {
return (T) value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public Object serialize(T val) {

private static <T> T numberToDate(Class<T> cls, Number epoch) throws ReflectiveOperationException {
if (ClassUtils.isAssignable(cls, java.sql.Date.class)) {
return (T) new java.sql.Date(epoch.longValue());
return cls.cast(new java.sql.Date(epoch.longValue()));
} else if (ClassUtils.isAssignable(cls, Timestamp.class)) {
return (T) new Timestamp(epoch.longValue());
return cls.cast(new Timestamp(epoch.longValue()));
} else if (ClassUtils.isAssignable(cls, Time.class)) {
return (T) new Time(epoch.longValue());
return cls.cast(new Time(epoch.longValue()));
} else if (ClassUtils.isAssignable(cls, Date.class)) {
return (T) new Date(epoch.longValue());
return cls.cast(new Date(epoch.longValue()));
} else {
throw new UnsupportedOperationException("Cannot convert to " + cls.getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class InMemoryStoreTransactionTest {
private RequestScope scope = mock(RequestScope.class);
private InMemoryStoreTransaction inMemoryStoreTransaction = new InMemoryStoreTransaction(wrappedTransaction);
private EntityDictionary dictionary;
private Set<Object> books = new HashSet<>();
private Set<Book> books = new HashSet<>();
private Book book1;
private Book book2;
private Book book3;
Expand Down Expand Up @@ -113,7 +113,7 @@ public InMemoryStoreTransactionTest() {
books.add(book2);
books.add(book3);

author.setBooks(new ArrayList(books));
author.setBooks(new ArrayList<>(books));

when(scope.getDictionary()).thenReturn(dictionary);
}
Expand All @@ -131,7 +131,7 @@ public void testFullFilterPredicatePushDown() {
when(wrappedTransaction.supportsFiltering(eq(Book.class),
any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL);
when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression)),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -195,7 +195,7 @@ public void testTransactionRequiresInMemoryFilterDuringLoad() {
when(wrappedTransaction.supportsFiltering(eq(Book.class),
any())).thenReturn(DataStoreTransaction.FeatureSupport.FULL);
when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression)),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -225,7 +225,7 @@ public void testDataStoreRequiresTotalInMemoryFilter() {
when(wrappedTransaction.supportsFiltering(eq(Book.class),
any())).thenReturn(DataStoreTransaction.FeatureSupport.NONE);
when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -257,7 +257,7 @@ public void testDataStoreRequiresPartialInMemoryFilter() {
when(wrappedTransaction.supportsFiltering(eq(Book.class),
any())).thenReturn(DataStoreTransaction.FeatureSupport.PARTIAL);
when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.of(expression1)),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -290,7 +290,7 @@ public void testSortingPushDown() {
any())).thenReturn(true);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.of(sorting)), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.of(sorting)), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -322,7 +322,7 @@ public void testDataStoreRequiresInMemorySorting() {
any())).thenReturn(false);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -360,7 +360,7 @@ public void testFilteringRequiresInMemorySorting() {
any())).thenReturn(true);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -391,7 +391,7 @@ public void testPaginationPushDown() {
when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.of(pagination)), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.of(pagination)), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -419,7 +419,7 @@ public void testDataStoreRequiresInMemoryPagination() {
when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(false);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -453,7 +453,7 @@ public void testFilteringRequiresInMemoryPagination() {
when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down Expand Up @@ -490,7 +490,7 @@ public void testSortingRequiresInMemoryPagination() {
when(wrappedTransaction.supportsPagination(eq(Book.class))).thenReturn(true);

when(wrappedTransaction.loadObjects(eq(Book.class), eq(Optional.empty()),
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn(books);
eq(Optional.empty()), eq(Optional.empty()), eq(scope))).thenReturn((Set) books);

Collection<Object> loaded = (Collection<Object>) inMemoryStoreTransaction.loadObjects(
Book.class,
Expand Down

0 comments on commit c948337

Please sign in to comment.