Proguard源码分析(五) ConfigurationParser.keep参数
本章节我们绕回来讲Keep参数,也就是ConfigurationParser 这个类。
ConfigurationParser这个类是非常重要的类,如果你已经开始看源码,你会发现所有的类和功能都围着它来转,本章节我们来揭开它的地一层面纱。
else if (ConfigurationConstants.KEEP_OPTION.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, true, false, false);
else if (ConfigurationConstants.KEEP_CLASS_MEMBERS_OPTION
.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, false, false, false);
else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBERS_OPTION
.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, false, true, false);
else if (ConfigurationConstants.KEEP_NAMES_OPTION
.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, true, false, true);
else if (ConfigurationConstants.KEEP_CLASS_MEMBER_NAMES_OPTION
.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, false, false, true);
else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBER_NAMES_OPTION
.startsWith(nextWord))
configuration.keep = parseKeepClassSpecificationArguments(
configuration.keep, false, true, true);
可见,所有以keep打头的参数都是调用的parseKeepClassSpecificationArguments
跟一下这个函数的逻辑
while (true) {
readNextWord("keyword '" + ConfigurationConstants.CLASS_KEYWORD
+ "', '" + ClassConstants.EXTERNAL_ACC_INTERFACE
+ "', or '" + ClassConstants.EXTERNAL_ACC_ENUM + "'",
false, true);
if (!ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD
.equals(nextWord)) {
// Not a comma. Stop parsing the keep modifiers.
break;
}
readNextWord("keyword '"
+ ConfigurationConstants.ALLOW_SHRINKING_SUBOPTION + "', '"
+ ConfigurationConstants.ALLOW_OPTIMIZATION_SUBOPTION
+ "', or '"
+ ConfigurationConstants.ALLOW_OBFUSCATION_SUBOPTION + "'");
if (ConfigurationConstants.ALLOW_SHRINKING_SUBOPTION
.startsWith(nextWord)) {
allowShrinking = true;
} else if (ConfigurationConstants.ALLOW_OPTIMIZATION_SUBOPTION
.startsWith(nextWord)) {
allowOptimization = true;
} else if (ConfigurationConstants.ALLOW_OBFUSCATION_SUBOPTION
.startsWith(nextWord)) {
allowObfuscation = true;
} else {
throw new ParseException("Expecting keyword '"
+ ConfigurationConstants.ALLOW_SHRINKING_SUBOPTION
+ "', '"
+ ConfigurationConstants.ALLOW_OPTIMIZATION_SUBOPTION
+ "', or '"
+ ConfigurationConstants.ALLOW_OBFUSCATION_SUBOPTION
+ "' before " + reader.locationDescription());
}
}
ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD
.equals(nextWord)代表以非ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD 作为终结符号,也就是说在keep之后可以跟一些参数,这些参数我们来看一下~
public static final String ALLOW_SHRINKING_SUBOPTION = "allowshrinking";
public static final String ALLOW_OPTIMIZATION_SUBOPTION = "allowoptimization";
public static final String ALLOW_OBFUSCATION_SUBOPTION = "allowobfuscation";
也就是说你可以采用下面这种写法:
-keep,allowshrinking,allowoptimization public class *;
我们来看一下这三个参数的影响:
if ((shrinking && !keepClassSpecification.allowShrinking) ||
(optimizing && !keepClassSpecification.allowOptimization) ||
(obfuscating && !keepClassSpecification.allowObfuscation))
{
ClassPoolVisitor classPoolVisitor = createClassPoolVisitor(keepClassSpecification,
classVisitor,
memberVisitor);
multiClassPoolVisitor.addClassPoolVisitor(classPoolVisitor);
}
结果似乎并不是我想的那样,要对这个类不做任何处理,必须保证这三个参数都为true.
在这之后会调用parseClassSpecificationArguments() 来生成一个ClassSpecification 的原始数据
classSpecification.requiredSetAccessFlags,
classSpecification.requiredUnsetAccessFlags,
classSpecification.annotationType,
classSpecification.className,
classSpecification.extendsAnnotationType,
classSpecification.extendsClassName,
classSpecification.fieldSpecifications,
classSpecification.methodSpecifications
requiredSetAccessFlags 和requiredUnsetAccessFlags 两个是必须设置的
它是检测是否加载该类的入口之一。他们的值是:
public static final int INTERNAL_ACC_PUBLIC = 0x0001;
public static final int INTERNAL_ACC_PRIVATE = 0x0002;
public static final int INTERNAL_ACC_PROTECTED = 0x0004;
public static final int INTERNAL_ACC_STATIC = 0x0008;
public static final int INTERNAL_ACC_FINAL = 0x0010;
public static final int INTERNAL_ACC_SUPER = 0x0020;
public static final int INTERNAL_ACC_SYNCHRONIZED = 0x0020;
public static final int INTERNAL_ACC_VOLATILE = 0x0040;
public static final int INTERNAL_ACC_TRANSIENT = 0x0080;
public static final int INTERNAL_ACC_BRIDGE = 0x0040;
public static final int INTERNAL_ACC_VARARGS = 0x0080;
public static final int INTERNAL_ACC_NATIVE = 0x0100;
public static final int INTERNAL_ACC_INTERFACE = 0x0200;
public static final int INTERNAL_ACC_ABSTRACT = 0x0400;
public static final int INTERNAL_ACC_STRICT = 0x0800;
public static final int INTERNAL_ACC_SYNTHETIC = 0x1000;
public static final int INTERNAL_ACC_ANNOTATTION = 0x2000;
public static final int INTERNAL_ACC_ENUM = 0x4000;
parseClassSpecificationArguments() 方法中定义了class的写法
当你的:
if (accessFlag == ClassConstants.INTERNAL_ACC_ANNOTATTION) {
// Already read the next word.
readNextWord("annotation type or keyword '"
+ ClassConstants.EXTERNAL_ACC_INTERFACE + "'", false,
false);
// Is the next word actually an annotation type?
if (!nextWord.equals(ClassConstants.EXTERNAL_ACC_INTERFACE)
&& !nextWord.equals(ClassConstants.EXTERNAL_ACC_ENUM)
&& !nextWord.equals(ConfigurationConstants.CLASS_KEYWORD)) {
// Parse the annotation type.
annotationType = ListUtil.commaSeparatedString(
parseCommaSeparatedList("annotation type", false,
false, false, false, true, false, false,
true, null), false);
// Continue parsing the access modifier that we just read
// in the next cycle.
continue;
}
// Otherwise just handle the annotation modifier.
}
accessFlag 为注解符号的时候,大致写法是这样的:
-keep @com.test.TestAnno
public class * {
*;
}
-keepclassmembers class * {
@com.test.TestAnno <methods>;
}
也就是说完全按照java的语法标准来实现。
解析完注解之后直到解析class interface enum 这些关键字
if (strippedWord.equals(ClassConstants.EXTERNAL_ACC_INTERFACE)
|| strippedWord.equals(ClassConstants.EXTERNAL_ACC_ENUM)
|| strippedWord.equals(ConfigurationConstants.CLASS_KEYWORD)) {
// The interface or enum keyword. Stop parsing the class flags.
break;
}
得到externalClassName
之后调用
if (!configurationEnd()) {
// Parse 'implements ...' or 'extends ...' part, if any.
if (ConfigurationConstants.IMPLEMENTS_KEYWORD.equals(nextWord)
|| ConfigurationConstants.EXTENDS_KEYWORD.equals(nextWord)) {
readNextWord("class name or interface name", false, true);
// Parse the annotation type, if any.
LOG.log("start ");
if (ConfigurationConstants.ANNOTATION_KEYWORD.equals(nextWord)) {
extendsAnnotationType = ListUtil.commaSeparatedString(
parseCommaSeparatedList("annotation type", true,
false, false, false, true, false, false,
true, null), false);
}
String externalExtendsClassName = ListUtil
.commaSeparatedString(
parseCommaSeparatedList(
"class name or interface name", false,
false, false, false, true, false,
false, false, null), false);
extendsClassName = ConfigurationConstants.ANY_CLASS_KEYWORD
.equals(externalExtendsClassName) ? null : ClassUtil
.internalClassName(externalExtendsClassName);
}
}
configurationEnd() 的结束条件是-和@,那么括号里面的又是干什么用的呢?
这是一种语法结构大致结构是这个样子的:
-keep public class * extends @com.test.TestAnno * #here
{
*;
}
解析到here这个位置,代表保持这个这个标注注解类的子类
最后将定义个类的元数据:
ClassSpecification classSpecification = new ClassSpecification(
lastComments, requiredSetClassAccessFlags,
requiredUnsetClassAccessFlags, annotationType, className,
extendsAnnotationType, extendsClassName);
进行下一次的匹配,
if (!configurationEnd()) {
// Check the class member opening part.
if (!ConfigurationConstants.OPEN_KEYWORD.equals(nextWord)) {
throw new ParseException("Expecting opening '"
+ ConfigurationConstants.OPEN_KEYWORD + "' at "
+ reader.locationDescription());
}
// Parse all class members.
while (true) {
readNextWord("class member description" + " or closing '"
+ ConfigurationConstants.CLOSE_KEYWORD + "'", false,
true);
if (nextWord.equals(ConfigurationConstants.CLOSE_KEYWORD)) {
// The closing brace. Stop parsing the class members.
readNextWord();
break;
}
parseMemberSpecificationArguments(externalClassName,
classSpecification);
}
}
这个匹配必须是非结束符号也就是不是 - 或者@
这就说明proguard的语法支持
-keep public class ...或者
-keep public class ...{...}
我们来看下第二种它是怎么做的:
if (nextWord.equals(ConfigurationConstants.CLOSE_KEYWORD)) {
// The closing brace. Stop parsing the class members.
readNextWord();
break;
}
当读入的字符不为}的时候将继续读入
解析成员通过方法parseMemberSpecificationArguments 来生成
这个方法跟类分析程序非常相似多了一些参数的条件,比如static native transient volatile final 这类用来形容方法或者变量的属性当然在这之前有过注解验证,也就是说支持:
{
@anno
static test
}
这种写法
接下来会通过
if ( ConfigurationConstants.ANY_CLASS_MEMBER_KEYWORD.equals(nextWord)
|| ConfigurationConstants.ANY_FIELD_KEYWORD.equals(nextWord)
|| ConfigurationConstants.ANY_METHOD_KEYWORD.equals(nextWord))
三个方法来对三种通配符号做处理,这三种通配符号分别是:*,<fields>,<methods>
匹配完成之后会生成叫做MemberSpecification 的对象来倒入到class的配置中
*可以看作是后面两个东西的集合,所以在proguard处理的时候会同时调用
classSpecification.addField(new MemberSpecification(
requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags, annotationType, null,
null));
classSpecification.addMethod(new MemberSpecification(
requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags, annotationType, null,
null));
这两个方法。
如果你不采用通配符号的方式来写的话,也就是说你默认会给出一个精确表达式,也有可能是一个模式匹配的表达式。我们来看一下Proguard对它的处理流程:
ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD.equals(name)
proguard会先检测是否是携带参数:
这里它对构造器方法和一些错误的可能做的屏蔽处理:
if (!(type.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)
|| type.equals(externalClassName) || type
.equals(ClassUtil
.externalShortClassName(externalClassName)))) {
throw new ParseException("Expecting type and name "
+ "instead of just '" + type + "' before "
+ reader.locationDescription());
}
原理很简单,由于构造器是没有返回值的,所以你之前期望得到的返回类型应该就是构造器的方法名<init>
if (ConfigurationConstants.SEPARATOR_KEYWORD.equals(nextWord)) {
// It's a field.
checkFieldAccessFlags(requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags);
// We already have a field descriptor.
String descriptor = ClassUtil.internalType(type);
// Add the field.
classSpecification.addField(new MemberSpecification(
requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags, annotationType, name,
descriptor));
} else if (ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD
.equals(nextWord)) {
// It's a method.
checkMethodAccessFlags(requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags);
// Parse the method arguments.
String descriptor = ClassUtil.internalMethodDescriptor(
type,
parseCommaSeparatedList("argument", true, true, true,
false, true, false, false, false, null));
if (!ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD
.equals(nextWord)) {
throw new ParseException("Expecting separating '"
+ ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD
+ "' or closing '"
+ ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD
+ "' before " + reader.locationDescription());
}
// Read the separator after the closing parenthesis.
readNextWord("separator '"
+ ConfigurationConstants.SEPARATOR_KEYWORD + "'");
if (!ConfigurationConstants.SEPARATOR_KEYWORD.equals(nextWord)) {
throw new ParseException("Expecting separator '"
+ ConfigurationConstants.SEPARATOR_KEYWORD
+ "' before " + reader.locationDescription());
}
// Add the method.
classSpecification.addMethod(new MemberSpecification(
requiredSetMemberAccessFlags,
requiredUnsetMemberAccessFlags, annotationType, name,
descriptor));
} else {
// It doesn't look like a field or a method.
throw new ParseException("Expecting opening '"
+ ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD
+ "' or separator '"
+ ConfigurationConstants.SEPARATOR_KEYWORD
+ "' before " + reader.locationDescription());
}
这段代码也非常好理解,对于只有名字然后直接跟分号的话,它认为是成员变量参数,如果是(则是方法,对于方法来说最重要的就是方法的签名,我们来关注一下方法是如何获得签名的.
方法签名是通过String descriptor = ClassUtil.internalMethodDescriptor(
type,
parseCommaSeparatedList("argument", true, true, true,
false, true, false, false, false, null));
来获得,其中type就是你的返回值,我们不说详细过程,只注重一些细节的结果parseCommaSeparatedList的参数列表最后得到相应的方法签名例如:
(ILcom/test/Base;)V
第一个I代表的是INT ,如果你想关注这些,不妨看一下jvm汇编一类的知识,其实c++的方法签名方式也大同小异。所以如果你之前从事过这方面的话,应该是不会陌生的。