Skip to content

Commit

Permalink
Merge branch 'LikeTheSalad-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 10, 2022
2 parents 919a474 + c85b731 commit cf63517
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,41 @@ enum TypeScope {
*/
EXTERNAL
}

/**
* A trivial implementation of an {@link AndroidDescriptor} that returns a fixed value.
*/
enum Trivial implements AndroidDescriptor {

/**
* A descriptor that marks all types as {@link TypeScope#LOCAL}.
*/
LOCAL(TypeScope.LOCAL),

/**
* A descriptor that marks all types as {@link TypeScope#EXTERNAL}.
*/
EXTERNAL(TypeScope.EXTERNAL);

/**
* The type scope to return.
*/
private final TypeScope typeScope;

/**
* Creates a new trivial android descriptor.
*
* @param typeScope The type scope to return.
*/
Trivial(TypeScope typeScope) {
this.typeScope = typeScope;
}

/**
* {@inheritDoc}
*/
public TypeScope getTypeScope(TypeDescription typeDescription) {
return typeScope;
}
}
}
168 changes: 168 additions & 0 deletions byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,174 @@ public void close() {
}
}

/**
* A compound source that combines multiple sources into a single representation.
*/
@HashCodeAndEqualsPlugin.Enhance
class Compound implements Source {

/**
* The represented sources.
*/
private final Collection<? extends Source> sources;

/**
* Creates a new compound source.
*
* @param sources The represented sources.
*/
public Compound(Collection<? extends Source> sources) {
this.sources = sources;
}

/**
* {@inheritDoc}
*/
public Source.Origin read() throws IOException {
if (sources.isEmpty()) {
return Empty.INSTANCE;
}
List<Source.Origin> origins = new ArrayList<Source.Origin>(sources.size());
try {
for (Source source : sources) {
origins.add(source.read());
}
} catch (IOException exception) {
for (Source.Origin origin : origins) {
origin.close();
}
throw exception;
}
return new Origin(origins);
}

/**
* Implements a compound {@link Source.Origin}.
*/
@HashCodeAndEqualsPlugin.Enhance
protected static class Origin implements Source.Origin {

/**
* A list of represented origins.
*/
private final List<Source.Origin> origins;

/**
* Creates a new compound origin.
*
* @param origins A list of represented origins.
*/
protected Origin(List<Source.Origin> origins) {
this.origins = origins;
}

/**
* {@inheritDoc}
*/
public Manifest getManifest() throws IOException {
for (Source.Origin origin : origins) {
Manifest manifest = origin.getManifest();
if (manifest != null) {
return manifest;
}
}
return NO_MANIFEST;
}

/**
* {@inheritDoc}
*/
public ClassFileLocator getClassFileLocator() {
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(origins.size());
for (Source.Origin origin : origins) {
classFileLocators.add(origin.getClassFileLocator());
}
return new ClassFileLocator.Compound(classFileLocators);
}

/**
* {@inheritDoc}
*/
public Iterator<Element> iterator() {
return new CompoundIterator(origins);
}

/**
* {@inheritDoc}
*/
public void close() throws IOException {
for (Source.Origin origin : origins) {
origin.close();
}
}

/**
* A compound iterator that combines several iterables.
*/
protected static class CompoundIterator implements Iterator<Element> {

/**
* The current iterator or {@code null} if no such iterator is defined.
*/
@MaybeNull
private Iterator<? extends Element> current;

/**
* A backlog of iterables to still consider.
*/
private final List<? extends Iterable<? extends Element>> backlog;

/**
* Creates a compound iterator.
*
* @param iterables The iterables to consider.
*/
protected CompoundIterator(List<? extends Iterable<? extends Element>> iterables) {
backlog = iterables;
forward();
}

/**
* {@inheritDoc}
*/
public boolean hasNext() {
return current != null && current.hasNext();
}

/**
* {@inheritDoc}
*/
public Element next() {
try {
if (current != null) {
return current.next();
} else {
throw new NoSuchElementException();
}
} finally {
forward();
}
}

/**
* Forwards the iterator to the next relevant iterable.
*/
private void forward() {
while ((current == null || !current.hasNext()) && !backlog.isEmpty()) {
current = backlog.remove(0).iterator();
}
}

/**
* {@inheritDoc}
*/
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
}
}

/**
* A source that represents a collection of in-memory resources that are represented as byte arrays.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package net.bytebuddy.build;

import net.bytebuddy.dynamic.ClassFileLocator;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.stubbing.Answer;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.jar.Manifest;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class PluginEngineSourceCompoundTest {

private static final String FOO = "foo";

@Rule
public MethodRule mockitoRule = MockitoJUnit.rule().silent();

@Mock
private Plugin.Engine.Source left;

@Mock
private Plugin.Engine.Source right;

@Mock
private Plugin.Engine.Source.Origin leftOrigin;

@Mock
private Plugin.Engine.Source.Origin rightOrigin;

@Mock
private ClassFileLocator leftLocator;

@Mock
private ClassFileLocator rightLocator;

@Mock
private Plugin.Engine.Source.Element leftElement;

@Mock
private Plugin.Engine.Source.Element rightElement;

@Before
public void setUp() throws Exception {
when(left.read()).thenReturn(leftOrigin);
when(right.read()).thenReturn(rightOrigin);
when(leftOrigin.getClassFileLocator()).thenReturn(leftLocator);
when(rightOrigin.getClassFileLocator()).thenReturn(rightLocator);
when(leftLocator.locate(Mockito.any(String.class))).thenReturn(new ClassFileLocator.Resolution.Illegal(FOO));
when(rightLocator.locate(Mockito.any(String.class))).thenReturn(new ClassFileLocator.Resolution.Illegal(FOO));
when(leftOrigin.iterator()).then(new Answer<Iterator<Plugin.Engine.Source.Element>>() {
@Override
public Iterator<Plugin.Engine.Source.Element> answer(InvocationOnMock invocation) {
return Collections.singleton(leftElement).iterator();
}
});
when(rightOrigin.iterator()).then(new Answer<Iterator<Plugin.Engine.Source.Element>>() {
@Override
public Iterator<Plugin.Engine.Source.Element> answer(InvocationOnMock invocation) {
return Collections.singleton(rightElement).iterator();
}
});
}

@Test
public void testEmptyCompound() throws Exception {
assertThat(new Plugin.Engine.Source.Compound(Collections.emptyList()).read(), sameInstance((Plugin.Engine.Source.Origin) Plugin.Engine.Source.Empty.INSTANCE));
}

@Test
public void testClassFileLocator() throws Exception {
assertThat(new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().getClassFileLocator().locate(FOO).isResolved(), is(false));
verify(leftLocator).locate(FOO);
verify(rightLocator).locate(FOO);
}

@Test
public void testManifest() throws Exception {
assertThat(new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().getManifest(), nullValue(Manifest.class));
verify(leftOrigin).getManifest();
verify(rightOrigin).getManifest();
}

@Test
public void testIteration() throws Exception {
Iterator<Plugin.Engine.Source.Element> iterator = new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(leftElement));
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(rightElement));
assertThat(iterator.hasNext(), is(false));

}

@Test
public void testClose() throws Exception {
new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().close();
verify(leftOrigin).close();
verify(rightOrigin).close();
}
}
Loading

0 comments on commit cf63517

Please sign in to comment.