| // Copyright (c) 2017, 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.shaking; |
| |
| import static com.android.tools.r8.graph.DexProgramClass.asProgramClassOrNull; |
| import static com.google.common.base.Predicates.alwaysTrue; |
| |
| import com.android.tools.r8.graph.AppInfoWithClassHierarchy; |
| import com.android.tools.r8.graph.AppView; |
| import com.android.tools.r8.graph.DexClass; |
| import com.android.tools.r8.graph.DexProgramClass; |
| import com.android.tools.r8.graph.DexType; |
| import com.android.tools.r8.graph.ImmediateAppSubtypingInfo; |
| import com.android.tools.r8.origin.Origin; |
| import com.android.tools.r8.position.Position; |
| import com.android.tools.r8.shaking.ProguardWildcard.BackReference; |
| import com.android.tools.r8.utils.BooleanBox; |
| import com.android.tools.r8.utils.IterableUtils; |
| import com.android.tools.r8.utils.StringUtils; |
| import com.google.common.collect.Iterables; |
| import java.util.List; |
| import java.util.Objects; |
| import java.util.function.Consumer; |
| import java.util.function.Predicate; |
| |
| public abstract class ProguardConfigurationRule extends ProguardClassSpecification { |
| |
| private boolean used = false; |
| // TODO(b/164019179): Since we are using the rule language for tracing main dex we can end up in |
| // a situation where the references to types are dead. |
| private boolean canReferenceDeadTypes = false; |
| |
| ProguardConfigurationRule( |
| Origin origin, |
| Position position, |
| String source, |
| List<ProguardTypeMatcher> classAnnotations, |
| ProguardAccessFlags classAccessFlags, |
| ProguardAccessFlags negatedClassAccessFlags, |
| boolean classTypeNegated, |
| ProguardClassType classType, |
| ProguardClassNameList classNames, |
| List<ProguardTypeMatcher> inheritanceAnnotations, |
| ProguardTypeMatcher inheritanceClassName, |
| boolean inheritanceIsExtends, |
| List<ProguardMemberRule> memberRules) { |
| super( |
| origin, |
| position, |
| source, |
| classAnnotations, |
| classAccessFlags, |
| negatedClassAccessFlags, |
| classTypeNegated, |
| classType, |
| classNames, |
| inheritanceAnnotations, |
| inheritanceClassName, |
| inheritanceIsExtends, |
| memberRules); |
| } |
| |
| public boolean isTrivalAllClassMatch() { |
| BooleanBox booleanBox = new BooleanBox(true); |
| getClassNames() |
| .forEachTypeMatcher( |
| unused -> booleanBox.set(false), |
| proguardTypeMatcher -> !proguardTypeMatcher.isMatchAnyClassPattern()); |
| return booleanBox.get() |
| && getClassAnnotations().isEmpty() |
| && getClassAccessFlags().isDefaultFlags() |
| && getNegatedClassAccessFlags().isDefaultFlags() |
| && !getClassTypeNegated() |
| && getClassType() == ProguardClassType.CLASS |
| && getInheritanceAnnotations().isEmpty() |
| && getInheritanceClassName() == null |
| && getMemberRules().isEmpty(); |
| } |
| |
| public boolean isUsed() { |
| return used; |
| } |
| |
| public void markAsUsed() { |
| used = true; |
| } |
| |
| public boolean isMaximumRemovedAndroidLogLevelRule() { |
| return false; |
| } |
| |
| public MaximumRemovedAndroidLogLevelRule asMaximumRemovedAndroidLogLevelRule() { |
| return null; |
| } |
| |
| public boolean isProguardCheckDiscardRule() { |
| return false; |
| } |
| |
| public ProguardCheckDiscardRule asProguardCheckDiscardRule() { |
| return null; |
| } |
| |
| public boolean isProguardKeepRule() { |
| return false; |
| } |
| |
| public ProguardKeepRule asProguardKeepRule() { |
| return null; |
| } |
| |
| public boolean isProguardIfRule() { |
| return false; |
| } |
| |
| public ProguardIfRule asProguardIfRule() { |
| return null; |
| } |
| |
| public boolean isClassInlineRule() { |
| return false; |
| } |
| |
| public ClassInlineRule asClassInlineRule() { |
| return null; |
| } |
| |
| public boolean isReprocessClassInitializerRule() { |
| return false; |
| } |
| |
| public ReprocessClassInitializerRule asReprocessClassInitializerRule() { |
| return null; |
| } |
| |
| public boolean isReprocessMethodRule() { |
| return false; |
| } |
| |
| public ReprocessMethodRule asReprocessMethodRule() { |
| return null; |
| } |
| |
| public void canReferenceDeadTypes() { |
| this.canReferenceDeadTypes = true; |
| } |
| |
| void forEachRelevantCandidate( |
| AppView<? extends AppInfoWithClassHierarchy> appView, |
| ImmediateAppSubtypingInfo subtypingInfo, |
| Iterable<DexProgramClass> defaultValue, |
| Predicate<DexProgramClass> isRelevant, |
| Consumer<DexClass> consumer) { |
| if (getClassNames().hasSpecificTypes()) { |
| for (DexType type : getClassNames().getSpecificTypes()) { |
| DexProgramClass clazz = |
| asProgramClassOrNull( |
| canReferenceDeadTypes |
| ? appView.appInfo().definitionForWithoutExistenceAssert(type) |
| : appView.definitionFor(type)); |
| if (clazz != null && isRelevant.test(clazz)) { |
| consumer.accept(clazz); |
| } |
| } |
| } else if (hasInheritanceClassName() && getInheritanceClassName().hasSpecificType()) { |
| DexType type = getInheritanceClassName().getSpecificType(); |
| DexClass clazz = appView.definitionFor(type); |
| if (clazz != null) { |
| subtypingInfo.forEachTransitiveProgramSubclassMatching(clazz, isRelevant, consumer); |
| if (appView.getVerticallyMergedClasses() != null |
| && appView.getVerticallyMergedClasses().hasBeenMergedIntoSubtype(type)) { |
| DexType targetType = appView.getVerticallyMergedClasses().getTargetFor(type); |
| DexProgramClass targetClass = asProgramClassOrNull(appView.definitionFor(targetType)); |
| assert targetClass != null; |
| if (isRelevant.test(targetClass)) { |
| consumer.accept(targetClass); |
| } |
| } |
| } |
| } else { |
| defaultValue.forEach(consumer); |
| } |
| } |
| |
| abstract String typeString(); |
| |
| String typeSuffix() { |
| return null; |
| } |
| |
| String modifierString() { |
| return null; |
| } |
| |
| public boolean isApplicableToClasspathClasses() { |
| return false; |
| } |
| |
| public boolean isApplicableToLibraryClasses() { |
| return false; |
| } |
| |
| protected boolean hasBackReferences() { |
| return !Iterables.isEmpty(getBackReferences()); |
| } |
| |
| public Iterable<BackReference> getBackReferences() { |
| return getWildcardsThatMatches(ProguardWildcard::isBackReference); |
| } |
| |
| protected final Iterable<ProguardWildcard> getWildcards() { |
| return getWildcardsThatMatches(alwaysTrue()); |
| } |
| |
| protected <T extends ProguardWildcard> Iterable<T> getWildcardsThatMatches( |
| Predicate<? super ProguardWildcard> predicate) { |
| return Iterables.concat( |
| ProguardTypeMatcher.getWildcardsThatMatchesOrEmpty(getClassAnnotations(), predicate), |
| ProguardClassNameList.getWildcardsThatMatchesOrEmpty(getClassNames(), predicate), |
| ProguardTypeMatcher.getWildcardsThatMatchesOrEmpty(getInheritanceAnnotations(), predicate), |
| ProguardTypeMatcher.getWildcardsThatMatchesOrEmpty(getInheritanceClassName(), predicate), |
| hasMemberRules() |
| ? IterableUtils.flatMap( |
| getMemberRules(), memberRule -> memberRule.getWildcardsThatMatches(predicate)) |
| : IterableUtils.empty()); |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (!(o instanceof ProguardConfigurationRule)) { |
| return false; |
| } |
| ProguardConfigurationRule that = (ProguardConfigurationRule) o; |
| if (used != that.used) { |
| return false; |
| } |
| if (!Objects.equals(typeString(), that.typeString())) { |
| return false; |
| } |
| if (!Objects.equals(modifierString(), that.modifierString())) { |
| return false; |
| } |
| return super.equals(that); |
| } |
| |
| @Override |
| public int hashCode() { |
| int result = 3 * typeString().hashCode(); |
| result = 3 * result + (used ? 1 : 0); |
| String modifier = modifierString(); |
| result = 3 * result + (modifier != null ? modifier.hashCode() : 0); |
| return result + super.hashCode(); |
| } |
| |
| @Override |
| protected StringBuilder append(StringBuilder builder) { |
| builder.append("-"); |
| builder.append(typeString()); |
| StringUtils.appendNonEmpty(builder, ",", modifierString(), null); |
| StringUtils.appendNonEmpty(builder, " ", typeSuffix(), null); |
| builder.append(' '); |
| super.append(builder); |
| return builder; |
| } |
| } |