Skip to content

Commit

Permalink
Fix #139 - allow *EXPLICIT* disabling of anonymous/method scoped clas…
Browse files Browse the repository at this point in the history
…ses.
  • Loading branch information
leibnitz27 committed May 16, 2020
1 parent 329eadc commit 9a6f6d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.benf.cfr.reader.entities.innerclass.InnerClassAttributeInfo;
import org.benf.cfr.reader.util.collections.ListFactory;
import org.benf.cfr.reader.util.bytestream.ByteData;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.benf.cfr.reader.util.output.Dumper;

import java.util.List;
Expand Down Expand Up @@ -50,6 +51,8 @@ private static Pair<JavaTypeInstance, JavaTypeInstance> getInnerOuter(int idxinn
}

public AttributeInnerClasses(ByteData raw, ConstantPool cp) {
Boolean forbidMethodScopedClasses = cp.getDCCommonState().getOptions().getOption(OptionsImpl.FORBID_METHOD_SCOPED_CLASSES);
Boolean forbidAnonymousClasses = cp.getDCCommonState().getOptions().getOption(OptionsImpl.FORBID_ANONYMOUS_CLASSES);
this.length = raw.getS4At(OFFSET_OF_ATTRIBUTE_LENGTH);
int numberInnerClasses = raw.getU2At(OFFSET_OF_NUMBER_OF_CLASSES);
long offset = OFFSET_OF_CLASS_ARRAY;
Expand All @@ -68,10 +71,15 @@ public AttributeInnerClasses(ByteData raw, ConstantPool cp) {
// MarkAnonymous feels like a bit of a hack, but otherwise we need to propagate this information
// the whole way down the type creation path, and this is the only place we care about it.
// May add that in later.
// if (outerClassType == null) {
if (outerClassType == null) {
boolean methodScoped = innerNameIdx == 0;
innerClassType.getInnerClassHereInfo().markMethodScoped(methodScoped);
// This option has to be set manually, because it affects state generated
// which is shared between fallback passes.
if (forbidMethodScopedClasses) {
outerClassType = innerClassType.getInnerClassHereInfo().getOuterClass();
} else {
boolean isAnonymous = !forbidAnonymousClasses && innerNameIdx == 0;
innerClassType.getInnerClassHereInfo().markMethodScoped(isAnonymous);
}
}
innerClassAttributeInfoList.add(new InnerClassAttributeInfo(
innerClassType,
Expand Down
8 changes: 7 additions & 1 deletion src/org/benf/cfr/reader/util/getopt/OptionsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public String getDefaultValue() {
// Look, I don't like reflection. ;)
private static List<PermittedOptionProvider.ArgumentParam<?,?>> all = ListFactory.newList();

private static <A, B, C extends PermittedOptionProvider.ArgumentParam<A, B>> C register(C in) {
private static <T extends PermittedOptionProvider.ArgumentParam<?, ?>> T register(T in) {
all.add(in);
return in;
}
Expand Down Expand Up @@ -318,6 +318,12 @@ private static <A, B, C extends PermittedOptionProvider.ArgumentParam<A, B>> C r
public static final PermittedOptionProvider.Argument<Boolean> DECOMPILE_INNER_CLASSES = register(new PermittedOptionProvider.Argument<Boolean>(
"innerclasses", defaultTrueBooleanDecoder,
"Decompile inner classes"));
public static final PermittedOptionProvider.Argument<Boolean> FORBID_METHOD_SCOPED_CLASSES = register(new PermittedOptionProvider.Argument<Boolean>(
"forbidmethodscopedclasses", defaultFalseBooleanDecoder,
"Don't allow method scoped classes. Note - this will NOT be used as a fallback, it must be specified.\nIt will produce odd code."));
public static final PermittedOptionProvider.Argument<Boolean> FORBID_ANONYMOUS_CLASSES = register(new PermittedOptionProvider.Argument<Boolean>(
"forbidanonymousclasses", defaultFalseBooleanDecoder,
"Don't allow anonymous classes. Note - this will NOT be used as a fallback, it must be specified.\nIt will produce odd code."));
public static final PermittedOptionProvider.Argument<Boolean> SKIP_BATCH_INNER_CLASSES = register(new PermittedOptionProvider.Argument<Boolean>(
"skipbatchinnerclasses", defaultTrueBooleanDecoder,
"When processing many files, skip inner classes, as they will be processed as part of outer classes anyway. If false, you will see inner classes as separate entities also."));
Expand Down

0 comments on commit 9a6f6d6

Please sign in to comment.