| // Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| package com.android.tools.r8.utils; |
| |
| import com.android.tools.r8.utils.DexInspector.ClassSubject; |
| import com.android.tools.r8.utils.DexInspector.FieldSubject; |
| import com.android.tools.r8.utils.DexInspector.MethodSubject; |
| import com.android.tools.r8.utils.DexInspector.Subject; |
| import com.google.common.collect.ImmutableList; |
| import org.hamcrest.Description; |
| import org.hamcrest.Matcher; |
| import org.hamcrest.TypeSafeMatcher; |
| |
| public class DexInspectorMatchers { |
| |
| private static String type(Subject subject) { |
| String type = "<unknown subject type>"; |
| if (subject instanceof ClassSubject) { |
| type = "class"; |
| } else if (subject instanceof MethodSubject) { |
| type = "method"; |
| } else if (subject instanceof FieldSubject) { |
| type = "field"; |
| } |
| return type; |
| } |
| |
| private static String name(Subject subject) { |
| String name = "<unknown>"; |
| if (subject instanceof ClassSubject) { |
| name = ((ClassSubject) subject).getOriginalName(); |
| } else if (subject instanceof MethodSubject) { |
| name = ((MethodSubject) subject).getOriginalName(); |
| } else if (subject instanceof FieldSubject) { |
| name = ((FieldSubject) subject).getOriginalName(); |
| } |
| return name; |
| } |
| |
| public static Matcher<Subject> isPresent() { |
| return new TypeSafeMatcher<Subject>() { |
| @Override |
| public boolean matchesSafely(final Subject subject) { |
| return subject.isPresent(); |
| } |
| |
| @Override |
| public void describeTo(final Description description) { |
| description.appendText(" present"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final Subject subject, Description description) { |
| description |
| .appendText(type(subject) + " ").appendValue(name(subject)).appendText(" was not"); |
| } |
| }; |
| } |
| |
| public static Matcher<Subject> isRenamed() { |
| return new TypeSafeMatcher<Subject>() { |
| @Override |
| protected boolean matchesSafely(Subject subject) { |
| return subject.isPresent() && subject.isRenamed(); |
| } |
| |
| @Override |
| public void describeTo(Description description) { |
| description.appendText(" renamed"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final Subject subject, Description description) { |
| description |
| .appendText(type(subject) + " ").appendValue(name(subject)).appendText(" was not"); |
| } |
| }; |
| } |
| |
| public static Matcher<Subject> isNotRenamed() { |
| return new TypeSafeMatcher<Subject>() { |
| @Override |
| protected boolean matchesSafely(Subject subject) { |
| return subject.isPresent() && !subject.isRenamed(); |
| } |
| |
| @Override |
| public void describeTo(Description description) { |
| description.appendText(" not renamed"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final Subject subject, Description description) { |
| description |
| .appendText(type(subject) + " ").appendValue(name(subject)).appendText(" was"); |
| } |
| }; |
| } |
| |
| public static Matcher<ClassSubject> hasDefaultConstructor() { |
| return new TypeSafeMatcher<ClassSubject>() { |
| @Override |
| public boolean matchesSafely(final ClassSubject clazz) { |
| return clazz.init(ImmutableList.of()).isPresent(); |
| } |
| |
| @Override |
| public void describeTo(final Description description) { |
| description.appendText("class having default constructor"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final ClassSubject clazz, Description description) { |
| description |
| .appendText("class ").appendValue(clazz.getOriginalName()).appendText(" did not"); |
| } |
| }; |
| } |
| |
| public static Matcher<MethodSubject> isAbstract() { |
| return new TypeSafeMatcher<MethodSubject>() { |
| @Override |
| public boolean matchesSafely(final MethodSubject method) { |
| return method.isPresent() && method.isAbstract(); |
| } |
| |
| @Override |
| public void describeTo(final Description description) { |
| description.appendText("method abstract"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final MethodSubject method, Description description) { |
| description |
| .appendText("method ").appendValue(method.getOriginalName()).appendText(" was not"); |
| } |
| }; |
| } |
| |
| public static Matcher<MethodSubject> isPublic() { |
| return new TypeSafeMatcher<MethodSubject>() { |
| @Override |
| public boolean matchesSafely(final MethodSubject method) { |
| return method.isPresent() && method.isPublic(); |
| } |
| |
| @Override |
| public void describeTo(final Description description) { |
| description.appendText("method public"); |
| } |
| |
| @Override |
| public void describeMismatchSafely(final MethodSubject method, Description description) { |
| description |
| .appendText("method ").appendValue(method.getOriginalName()).appendText(" was not"); |
| } |
| }; |
| } |
| } |