Skip to content

Commit

Permalink
feat: Basic support for replacing unused CP entries in illegal stripper
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Feb 13, 2022
1 parent b3bc45f commit 27a5a59
Show file tree
Hide file tree
Showing 37 changed files with 542 additions and 50 deletions.
20 changes: 19 additions & 1 deletion src/main/java/me/coley/cafedude/classfile/ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import me.coley.cafedude.classfile.attribute.Attribute;
import me.coley.cafedude.classfile.behavior.AttributeHolder;
import me.coley.cafedude.classfile.behavior.CpAccessor;
import me.coley.cafedude.classfile.constant.ConstPoolEntry;
import me.coley.cafedude.classfile.constant.CpClass;
import me.coley.cafedude.classfile.constant.CpUtf8;
import me.coley.cafedude.io.AttributeContext;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
* Class file format.
*
* @author Matt Coley
*/
public class ClassFile implements AttributeHolder {
public class ClassFile implements AttributeHolder, CpAccessor {
private final ConstPool pool;
private List<Integer> interfaceIndices;
private List<Field> fields;
Expand Down Expand Up @@ -251,4 +254,19 @@ public void setAttributes(List<Attribute> attributes) {
public AttributeContext getHolderType() {
return AttributeContext.CLASS;
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getClassIndex());
set.add(getSuperIndex());
set.addAll(getInterfaceIndices());
for (Attribute attribute : getAttributes())
set.addAll(attribute.cpAccesses());
for (ClassMember field : getFields())
set.addAll(field.cpAccesses());
for (ClassMember method : getMethods())
set.addAll(method.cpAccesses());
return set;
}
}
15 changes: 14 additions & 1 deletion src/main/java/me/coley/cafedude/classfile/ClassMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import me.coley.cafedude.classfile.attribute.Attribute;
import me.coley.cafedude.classfile.behavior.AttributeHolder;
import me.coley.cafedude.classfile.behavior.CpAccessor;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
* Base class member.
*
* @author Matt Coley
*/
public abstract class ClassMember implements AttributeHolder {
public abstract class ClassMember implements AttributeHolder, CpAccessor {
private List<Attribute> attributes;
private int access;
private int nameIndex;
Expand Down Expand Up @@ -87,4 +90,14 @@ public List<Attribute> getAttributes() {
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getNameIndex());
set.add(getTypeIndex());
for (Attribute attribute : getAttributes())
set.addAll(attribute.cpAccesses());
return set;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import me.coley.cafedude.classfile.attribute.AnnotationsAttribute;
import me.coley.cafedude.classfile.attribute.ParameterAnnotationsAttribute;
import me.coley.cafedude.classfile.behavior.CpAccessor;

import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
* Annotation outline. Represents an annotation item to be contained in an annotation collection attribute such as:
Expand All @@ -18,7 +21,7 @@
* @see AnnotationsAttribute
* @see ParameterAnnotationsAttribute
*/
public class Annotation {
public class Annotation implements CpAccessor {
private final Map<Integer, ElementValue> values;
private final int typeIndex;

Expand Down Expand Up @@ -51,6 +54,15 @@ public Map<Integer, ElementValue> getValues() {
return values;
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getTypeIndex());
for (ElementValue value : values.values())
set.addAll(value.cpAccesses());
return set;
}

/**
* @return Computed size for the annotation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.Set;
import java.util.TreeSet;

/**
* Nested annotation element value.
*
Expand Down Expand Up @@ -44,6 +47,13 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.addAll(annotation.cpAccesses());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
* Array element value.
Expand Down Expand Up @@ -46,6 +48,14 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
for (ElementValue value : getArray())
set.addAll(value.cpAccesses());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.Set;
import java.util.TreeSet;

/**
* Class element value.
*
Expand Down Expand Up @@ -44,6 +47,13 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getClassIndex());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.coley.cafedude.classfile.annotation;

import me.coley.cafedude.classfile.behavior.CpAccessor;

/**
* Base attribute element value.
*
* @author Matt Coley
*/
public abstract class ElementValue {
public abstract class ElementValue implements CpAccessor {
private final char tag;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.Set;
import java.util.TreeSet;

/**
* Enum element value.
*
Expand Down Expand Up @@ -63,6 +66,14 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getNameIndex());
set.add(getTypeIndex());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.Set;
import java.util.TreeSet;

/**
* Primitive value element value.
*
Expand Down Expand Up @@ -42,6 +45,13 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getValueIndex());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.coley.cafedude.classfile.annotation;

import java.util.Set;
import java.util.TreeSet;

/**
* UTF8 string element value.
*
Expand Down Expand Up @@ -44,6 +47,13 @@ public char getTag() {
return super.getTag();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getUtfIndex());
return set;
}

@Override
public int computeLength() {
// u1: tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import me.coley.cafedude.classfile.annotation.ElementValue;

import java.util.Set;

/**
* Represents the default value of a annotation field <i>(Which are technically methods, but I digress)</i>.
*
Expand All @@ -24,12 +26,19 @@ public AnnotationDefaultAttribute(int nameIndex, ElementValue elementValue) {

/**
* @return Value of the annotation type element represented by the {@code method_info} structure
* enclosing this attribute.
* enclosing this attribute.
*/
public ElementValue getElementValue() {
return elementValue;
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = super.cpAccesses();
set.addAll(elementValue.cpAccesses());
return set;
}

@Override
public int computeInternalLength() {
return getElementValue().computeLength();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.coley.cafedude.classfile.annotation.Annotation;

import java.util.List;
import java.util.Set;

/**
* Annotation collection attribute. Represents either:
Expand Down Expand Up @@ -42,6 +43,14 @@ public void setAnnotations(List<Annotation> annotations) {
this.annotations = annotations;
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = super.cpAccesses();
for (Annotation annotation : getAnnotations())
set.addAll(annotation.cpAccesses());
return set;
}

@Override
public int computeInternalLength() {
// u2 num_annotations + annotations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package me.coley.cafedude.classfile.attribute;

import me.coley.cafedude.classfile.behavior.CpAccessor;

import java.util.Set;
import java.util.TreeSet;

/**
* Base attribute.
*
* @author Matt Coley
*/
public abstract class Attribute {
public abstract class Attribute implements CpAccessor {
private final int nameIndex;

/**
Expand Down Expand Up @@ -41,4 +46,11 @@ public int computeCompleteLength() {
// ??: Internal length
return 6 + computeInternalLength();
}

@Override
public Set<Integer> cpAccesses() {
Set<Integer> set = new TreeSet<>();
set.add(getNameIndex());
return set;
}
}
Loading

0 comments on commit 27a5a59

Please sign in to comment.