Skip to content

Commit

Permalink
Made some API collection return types more specific, since it integra…
Browse files Browse the repository at this point in the history
…tes nicer with JDK >= 8 streams. Also introduced ArchUnit's own ForwardingCollection/-Set, since we don't want to have Guava in the public API (sneakily leaked through Slice).

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Mar 23, 2019
1 parent 69909eb commit 4a6e738
Show file tree
Hide file tree
Showing 31 changed files with 447 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.CompositeArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
Expand Down Expand Up @@ -101,6 +102,16 @@ public class PublicAPIRules {
.and().doNotHaveName("adhereToPlantUmlDiagram")
.should().haveRawParameterTypes(thatArePublic());

@ArchTest
public static final ArchRule Guava_should_not_leak_into_public_API =
CompositeArchRule.of(
classes().that(publicAPI()).should().notBeAssignableTo(guavaClass()))
.and(codeUnits()
.that().arePublic()
.and().areDeclaredInClassesThat(are(publicAPI()))
.should().haveRawParameterTypes(withoutGuava())
.andShould().haveRawReturnType(not(guavaClass()).as("that are no Guava types")));

private static DescribedPredicate<JavaClass> publicAPI() {
return annotatedWith(PublicAPI.class).<JavaClass>forSubType()
.or(haveMemberThatBelongsToPublicApi())
Expand Down Expand Up @@ -316,4 +327,28 @@ public boolean apply(List<JavaClass> input) {
}
};
}

private static DescribedPredicate<JavaClass> guavaClass() {
return JavaClass.Functions.GET_PACKAGE_NAME.is(
new DescribedPredicate<String>("") {
@Override
public boolean apply(String input) {
return input.contains(".google.");
}
}).as("Guava Class");
}

private static DescribedPredicate<List<JavaClass>> withoutGuava() {
return new DescribedPredicate<List<JavaClass>>("without Guava") {
@Override
public boolean apply(List<JavaClass> input) {
for (JavaClass parameterType : input) {
if (guavaClass().apply(parameterType)) {
return false;
}
}
return true;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2019 TNG Technology Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tngtech.archunit.base;

import java.util.Collection;
import java.util.Iterator;

import com.tngtech.archunit.PublicAPI;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

@PublicAPI(usage = ACCESS)
public abstract class ForwardingCollection<T> implements Collection<T> {
protected ForwardingCollection() {
}

protected abstract Collection<T> delegate();

@Override
@PublicAPI(usage = ACCESS)
public int size() {
return delegate().size();
}

@Override
@PublicAPI(usage = ACCESS)
public boolean isEmpty() {
return delegate().isEmpty();
}

@Override
@PublicAPI(usage = ACCESS)
public boolean contains(Object o) {
return delegate().contains(o);
}

@Override
@PublicAPI(usage = ACCESS)
public Iterator<T> iterator() {
return delegate().iterator();
}

@Override
@PublicAPI(usage = ACCESS)
public Object[] toArray() {
return delegate().toArray();
}

@Override
@PublicAPI(usage = ACCESS)
@SuppressWarnings("SuspiciousToArrayCall")
public <T1> T1[] toArray(T1[] a) {
return delegate().toArray(a);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean add(T t) {
return delegate().add(t);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean remove(Object o) {
return delegate().remove(o);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean containsAll(Collection<?> c) {
return delegate().containsAll(c);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean addAll(Collection<? extends T> c) {
return delegate().addAll(c);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean removeAll(Collection<?> c) {
return delegate().removeAll(c);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean retainAll(Collection<?> c) {
return delegate().retainAll(c);
}

@Override
@PublicAPI(usage = ACCESS)
public void clear() {
delegate().clear();
}

@Override
public int hashCode() {
return delegate().hashCode();
}

@Override
public boolean equals(Object obj) {
return obj == this || delegate().equals(obj);
}

@Override
public String toString() {
return delegate().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2019 TNG Technology Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tngtech.archunit.base;

import java.util.Collection;
import java.util.List;
import java.util.ListIterator;

import com.tngtech.archunit.PublicAPI;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

@PublicAPI(usage = ACCESS)
public abstract class ForwardingList<T> extends ForwardingCollection<T> implements List<T> {
protected ForwardingList() {
}

@Override
protected abstract List<T> delegate();

@Override
@PublicAPI(usage = ACCESS)
public boolean addAll(int index, Collection<? extends T> c) {
return delegate().addAll(index, c);
}

@Override
@PublicAPI(usage = ACCESS)
public T get(int index) {
return delegate().get(index);
}

@Override
@PublicAPI(usage = ACCESS)
public T set(int index, T element) {
return delegate().set(index, element);
}

@Override
@PublicAPI(usage = ACCESS)
public void add(int index, T element) {
delegate().add(index, element);
}

@Override
@PublicAPI(usage = ACCESS)
public T remove(int index) {
return delegate().remove(index);
}

@Override
@PublicAPI(usage = ACCESS)
public int indexOf(Object o) {
return delegate().indexOf(o);
}

@Override
@PublicAPI(usage = ACCESS)
public int lastIndexOf(Object o) {
return delegate().lastIndexOf(o);
}

@Override
@PublicAPI(usage = ACCESS)
public ListIterator<T> listIterator() {
return delegate().listIterator();
}

@Override
@PublicAPI(usage = ACCESS)
public ListIterator<T> listIterator(int index) {
return delegate().listIterator(index);
}

@Override
@PublicAPI(usage = ACCESS)
public List<T> subList(int fromIndex, int toIndex) {
return delegate().subList(fromIndex, toIndex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 TNG Technology Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tngtech.archunit.base;

import java.util.Set;

import com.tngtech.archunit.PublicAPI;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

@PublicAPI(usage = ACCESS)
public abstract class ForwardingSet<T> extends ForwardingCollection<T> implements Set<T> {
protected ForwardingSet() {
}

@Override
protected abstract Set<T> delegate();
}
Loading

0 comments on commit 4a6e738

Please sign in to comment.