Compare commits

...

1 Commits

Author SHA1 Message Date
Eric Lafortune
e5cf8ddde5 Added support for Java 14 and 15, including record attributes. 2020-09-06 23:54:08 +02:00
10 changed files with 229 additions and 13 deletions

View File

@@ -22,7 +22,7 @@ package proguard.obfuscate;
import proguard.classfile.*;
import proguard.classfile.attribute.*;
import proguard.classfile.attribute.visitor.AttributeVisitor;
import proguard.classfile.attribute.visitor.*;
import proguard.classfile.visitor.*;
import proguard.util.Processable;
@@ -39,7 +39,8 @@ import java.util.Arrays;
public class AttributeShrinker
implements ClassVisitor,
MemberVisitor,
AttributeVisitor
AttributeVisitor,
RecordComponentInfoVisitor
{
// Implementations for ClassVisitor.
@@ -83,6 +84,13 @@ implements ClassVisitor,
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
public void visitRecordAttribute(Clazz clazz, RecordAttribute recordAttribute)
{
// Compact any attributes of the components.
recordAttribute.componentsAccept(clazz, this);
}
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
// Compact the attributes array.
@@ -92,6 +100,17 @@ implements ClassVisitor,
}
// Implementations for RecordComponentInfoVisitor.
public void visitRecordComponentInfo(Clazz clazz, RecordComponentInfo recordComponentInfo)
{
// Compact the attributes array.
recordComponentInfo.u2attributesCount =
shrinkArray(recordComponentInfo.attributes,
recordComponentInfo.u2attributesCount);
}
// Small utility methods.
/**

View File

@@ -44,9 +44,12 @@ import proguard.util.ArrayUtil;
*/
public class TargetClassChanger
implements ClassVisitor,
// Implementation interfaces.
ConstantVisitor,
MemberVisitor,
AttributeVisitor,
RecordComponentInfoVisitor,
LocalVariableInfoVisitor,
LocalVariableTypeInfoVisitor,
AnnotationVisitor,
@@ -305,6 +308,13 @@ implements ClassVisitor,
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
public void visitRecordAttribute(Clazz clazz, RecordAttribute recordAttribute)
{
// Fix the record component attributes.
recordAttribute.componentsAccept(clazz, this);
}
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
// Change the references of the attributes.
@@ -354,6 +364,18 @@ implements ClassVisitor,
}
// Implementations for RecordComponentInfoVisitor.
public void visitRecordComponentInfo(Clazz clazz, RecordComponentInfo recordComponentInfo)
{
// Don't change the referenced field; it's still the original one
// in this class.
// Change the references of the attributes.
recordComponentInfo.attributesAccept(clazz, this);
}
// Implementations for LocalVariableInfoVisitor.
public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)

View File

@@ -46,6 +46,7 @@ public class ClassShrinker
implements ClassVisitor,
MemberVisitor,
AttributeVisitor,
RecordComponentInfoVisitor,
AnnotationVisitor,
ElementValueVisitor
{
@@ -233,6 +234,18 @@ implements ClassVisitor,
}
public void visitRecordAttribute(Clazz clazz, RecordAttribute recordAttribute)
{
// Shrink the array of RecordComponentInfo objects.
recordAttribute.u2componentsCount =
shrinkArray(recordAttribute.components,
recordAttribute.u2componentsCount);
// Shrink the attributes of the remaining components.
recordAttribute.componentsAccept(clazz, this);
}
public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute)
{
// Shrink the array of InnerClassesInfo objects.
@@ -318,6 +331,20 @@ implements ClassVisitor,
}
// Implementations for RecordComponentInfoVisitor.
public void visitRecordComponentInfo(Clazz clazz, RecordComponentInfo recordComponentInfo)
{
// Shrink the attributes array.
recordComponentInfo.u2attributesCount =
shrinkArray(recordComponentInfo.attributes,
recordComponentInfo.u2attributesCount);
// Shrink the remaining attributes.
recordComponentInfo.attributesAccept(clazz, this);
}
// Implementations for AnnotationVisitor.
public void visitAnnotation(Clazz clazz, Annotation annotation)

View File

@@ -857,15 +857,6 @@ implements ClassVisitor,
}
public void visitSourceDebugExtensionAttribute(Clazz clazz,
SourceDebugExtensionAttribute sourceDebugExtensionAttribute)
{
markAsUsed(sourceDebugExtensionAttribute);
markConstant(clazz, sourceDebugExtensionAttribute.u2attributeNameIndex);
}
public void visitBootstrapMethodsAttribute(Clazz clazz, BootstrapMethodsAttribute bootstrapMethodsAttribute)
{
// Don't mark the attribute and its name here. We may mark it in
@@ -891,6 +882,26 @@ implements ClassVisitor,
}
public void visitSourceDebugExtensionAttribute(Clazz clazz, SourceDebugExtensionAttribute sourceDebugExtensionAttribute)
{
markAsUsed(sourceDebugExtensionAttribute);
markConstant(clazz, sourceDebugExtensionAttribute.u2attributeNameIndex);
}
public void visitRecordAttribute(Clazz clazz, RecordAttribute recordAttribute)
{
markAsUsed(recordAttribute);
markConstant(clazz, recordAttribute.u2attributeNameIndex);
// Don't mark the components yet. We may mark them later, in
// RecordComponentUsageMarker.
//recordAttribute.componentsAccept(clazz, this);
}
public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute)
{
// Don't mark the attribute and its name yet. We may mark it later, in

View File

@@ -0,0 +1,108 @@
/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2020 Guardsquare NV
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package proguard.shrink;
import proguard.classfile.*;
import proguard.classfile.attribute.*;
import proguard.classfile.attribute.visitor.*;
import proguard.classfile.constant.*;
import proguard.classfile.constant.visitor.ConstantVisitor;
import proguard.classfile.visitor.*;
/**
* This RecordComponentInfoVisitor marks all record components that
* it visits and whose corresponding fields have been marked before.
*
* @see ClassUsageMarker
*
* @author Eric Lafortune
*/
public class RecordComponentUsageMarker
implements RecordComponentInfoVisitor,
MemberVisitor,
ConstantVisitor
{
private final ClassUsageMarker classUsageMarker;
// Field acting as a return parameter.
private boolean fieldUsed;
/**
* Creates a new InnerUsageMarker.
* @param classUsageMarker the marker to mark and check the classes and
* class members.
*/
public RecordComponentUsageMarker(ClassUsageMarker classUsageMarker)
{
this.classUsageMarker = classUsageMarker;
}
// Implementations for RecordComponentInfoVisitor.
public void visitRecordComponentInfo(Clazz clazz, RecordComponentInfo recordComponentInfo)
{
// Is the field that corresponds to the component used?
fieldUsed = false;
recordComponentInfo.referencedFieldAccept(clazz, this);
if (fieldUsed)
{
// Mark the component.
classUsageMarker.markAsUsed(recordComponentInfo);
// Mark its name and descriptor.
markConstant(clazz, recordComponentInfo.u2nameIndex);
markConstant(clazz, recordComponentInfo.u2descriptorIndex);
// Mark its attributes.
recordComponentInfo.attributesAccept(clazz, classUsageMarker);
}
}
// Implementations for MemberVisitor.
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
fieldUsed = classUsageMarker.isUsed(programField);
}
// Implementations for ConstantVisitor.
public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant)
{
classUsageMarker.markAsUsed(utf8Constant);
}
// Small utility methods.
/**
* Marks the given constant pool entry of the given class. This includes
* visiting any other referenced constant pool entries.
*/
private void markConstant(Clazz clazz, int index)
{
clazz.constantPoolEntryAccept(index, this);
}
}

View File

@@ -138,6 +138,12 @@ public class UsageMarker
new KotlinModuleUsageMarker(simpleUsageMarker))));
}
// Mark the record metadata.
programClassPool.classesAccept(
new AllAttributeVisitor(
new AllRecordComponentInfoVisitor(
new RecordComponentUsageMarker(classUsageMarker))));
// Check if the Gson optimization is enabled.
StringMatcher filter = configuration.optimizations != null ?
new ListParser(new NameParser()).parse(configuration.optimizations) :

View File

@@ -47,6 +47,10 @@ attributes. You can keep them with the
: Specifies the name of the source directory from which the class file was
compiled.
`Record`
: Specifies the components of a record class. Code may access this information
by reflection.
`InnerClasses`
: Specifies the relationship between a class and its inner classes and outer
classes. Other than this and the naming convention with a '\$' separator
@@ -135,7 +139,7 @@ ProGuard automatically keeps the following essential attributes, processing
them as necessary. We're listing them for the sake of completeness:
`ConstantValue`
: Specifies a constant integer, float, class, string, etc.
: Specifies a constant integer, float, class, string, etc.
`Code`
: Specifies the actual bytecode of a method.

View File

@@ -1,3 +1,12 @@
## Version 7.1
| Version| Issue | Module | Explanation
|--------|----------|----------|----------------------------------
| 7.1.x | PGD-0064 | CORE | Added support for Java 14 and 15.
| 7.1.x | PGD-0064 | CORE | Added support for record attributes.
| 7.1.x | DGD-2390 | CORE | Fixed storage and alignment of uncompressed zip entries.
| 7.1.x | DGD-2338 | CORE | Fixed processing of constant boolean arrays.
## Version 7.0 (Jun 2020)
| Version| Issue | Module | Explanation

View File

@@ -2,5 +2,10 @@ gsonVersion = 2.8.5
kotlinVersion = 1.3.31
kotlinxMetadataVersion = 0.1.0
target = 1.8
# Optionally compile the WTK plugin.
wtkDir = /usr/local/java/wtk2.1
wtkHome = /usr/local/java/wtk
wtkHome = /usr/local/java/wtk
# Optionally set up a composite build with ProGuardCORE.
#proguardCoreDir = ../proguard-core

View File

@@ -11,6 +11,11 @@ pluginManagement {
}
}
if (hasProperty('proguardCoreDir'))
{
includeBuild proguardCoreDir
}
rootProject.name = 'parent'
include 'base'