Skip to content

Commit

Permalink
Added classNameFilter(ClassNameFilter) into DexOpener.Builder and d…
Browse files Browse the repository at this point in the history
…eprecated `classNameFilters(ClassNameFilter ...)`
  • Loading branch information
tmurakami committed May 16, 2017
1 parent bcba46a commit 2f137cf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.tmurakami.dexopener;

import android.support.annotation.NonNull;

final class AcceptAll implements ClassNameFilter {

static final ClassNameFilter INSTANCE = new AcceptAll();

private AcceptAll() {
}

@Override
public boolean accept(@NonNull String className) {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import android.support.annotation.NonNull;

final class BuiltinClassNameFilter implements ClassNameFilter {
final class ClassNameFilterWrapper implements ClassNameFilter {

static final BuiltinClassNameFilter INSTANCE = new BuiltinClassNameFilter();

private final String[] disallowedPackages = {
private static final String[] EXCLUDED_PACKAGES = {
"android.",
"com.android.",
"com.github.tmurakami.classinjector.",
Expand All @@ -26,23 +24,30 @@ final class BuiltinClassNameFilter implements ClassNameFilter {
"org.objenesis.",
};

private BuiltinClassNameFilter() {
private final ClassNameFilter delegate;

ClassNameFilterWrapper(ClassNameFilter delegate) {
this.delegate = delegate;
}

@Override
public boolean accept(@NonNull String className) {
return isAccepted(className) && delegate.accept(className);
}

private static boolean isAccepted(String name) {
// The Data Binding Library generates several classes packaged as 'android.databinding'.
// Since these classes are tightly coupled with user classes, 'android.databinding' must not
// be filtered out.
if (className.startsWith("android.databinding.")) {
if (name.startsWith("android.databinding.")) {
return true;
}
for (String pkg : disallowedPackages) {
if (className.startsWith(pkg)) {
for (String pkg : EXCLUDED_PACKAGES) {
if (name.startsWith(pkg)) {
return false;
}
}
return !className.endsWith(".R") && !className.contains(".R$") && !className.endsWith(".BuildConfig");
return !name.endsWith(".R") && !name.contains(".R$") && !name.endsWith(".BuildConfig");
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import android.content.pm.ApplicationInfo;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* This is an object that provides the ability to mock final classes and methods.
*/
Expand Down Expand Up @@ -64,27 +60,35 @@ public static Builder builder(@NonNull Context context) {
*/
public static final class Builder {

private final List<ClassNameFilter> classNameFilters = new ArrayList<>();
private final ApplicationInfo applicationInfo;
private ClassNameFilter classNameFilter = AcceptAll.INSTANCE;

Builder(ApplicationInfo applicationInfo) {
this.applicationInfo = applicationInfo;
}

/**
* Appends class name filters.
* Throws an {@link UnsupportedOperationException}.
*
* @throws UnsupportedOperationException this method is deprecated
* @deprecated use {@link #classNameFilter(ClassNameFilter)} instead.
*/
@Deprecated
@NonNull
public Builder classNameFilters(@SuppressWarnings("unused") @NonNull ClassNameFilter... filters)
throws UnsupportedOperationException {
throw new UnsupportedOperationException("Use classNameFilter(ClassNameFilter) instead");
}

/**
* Sets a {@link ClassNameFilter}.
*
* @param filters the class name filter
* @param filter the {@link ClassNameFilter}
* @return this builder
*/
@NonNull
public Builder classNameFilters(@NonNull ClassNameFilter... filters) {
for (ClassNameFilter f : filters) {
if (f == null) {
throw new IllegalArgumentException("'filters' contains null");
}
classNameFilters.add(f);
}
public Builder classNameFilter(@NonNull ClassNameFilter filter) {
classNameFilter = filter;
return this;
}

Expand All @@ -95,10 +99,10 @@ public Builder classNameFilters(@NonNull ClassNameFilter... filters) {
*/
@NonNull
public DexOpener build() {
List<ClassNameFilter> filters = new ArrayList<>(classNameFilters);
filters.add(BuiltinClassNameFilter.INSTANCE);
ClassNameFilter filter = new ClassNameFilters(Collections.unmodifiableList(filters));
return new DexOpenerImpl(applicationInfo, filter, DexFileLoader.INSTANCE, DexClassFileFactory.INSTANCE);
return new DexOpenerImpl(applicationInfo,
new ClassNameFilterWrapper(classNameFilter),
DexFileLoader.INSTANCE,
DexClassFileFactory.INSTANCE);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import static org.junit.Assert.assertSame;

@RunWith(Parameterized.class)
public class BuiltinClassNameFilterTest {
public class ClassNameFilterWrapperTest {

private final BuiltinClassNameFilter testTarget = BuiltinClassNameFilter.INSTANCE;
private final ClassNameFilterWrapper testTarget =
new ClassNameFilterWrapper(AcceptAll.INSTANCE);

private final String className;
private final boolean expected;

public BuiltinClassNameFilterTest(String className, boolean expected) {
public ClassNameFilterWrapperTest(String className, boolean expected) {
this.className = className;
this.expected = expected;
}
Expand Down

This file was deleted.

0 comments on commit 2f137cf

Please sign in to comment.