From 9ddf8ab9b98ce9ff8fc8792441dad74ceef1eac9 Mon Sep 17 00:00:00 2001 From: Dino Oliva Date: Thu, 18 Mar 2021 10:42:13 -0700 Subject: [PATCH] Minor cleanup of Type and Member datastructures. PiperOrigin-RevId: 363691930 --- .../google/devtools/treeshaker/Member.java | 34 +++++---- .../com/google/devtools/treeshaker/Type.java | 72 +++++++++---------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Member.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Member.java index 1117dad572..0d1c3d3a2d 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Member.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Member.java @@ -18,36 +18,44 @@ final class Member { static Member buildFrom(MemberInfo memberInfo, Type declaringType) { - Member member = new Member(); - member.memberInfo = memberInfo; - member.declaringType = declaringType; - member.isStatic = memberInfo.getStatic(); - member.isConstructor = memberInfo.getConstructor(); - return member; + return new Member( + memberInfo.getName(), + declaringType, + memberInfo.getStatic(), + memberInfo.getConstructor(), + memberInfo.getExported()); } - private MemberInfo memberInfo; - private Type declaringType; - private boolean isStatic; - private boolean isConstructor; + private final String name; + private final Type declaringType; + private final boolean isStatic; + private final boolean isConstructor; + private final boolean isExported; private boolean fullyTraversed; private boolean live; private final List referencedTypes = new ArrayList<>(); private final List referencedMembers = new ArrayList<>(); - private Member() {} + private Member(String name, Type declaringType, boolean isStatic, boolean isConstructor, + boolean isExported) { + this.name = name; + this.declaringType = declaringType; + this.isStatic = isStatic; + this.isConstructor = isConstructor; + this.isExported = isExported; + } Type getDeclaringType() { return declaringType; } boolean isExported() { - return memberInfo.getExported(); + return isExported; } String getName() { - return memberInfo.getName(); + return name; } public boolean isConstructor() { diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Type.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Type.java index 8c79467e35..ecc50b8548 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Type.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Type.java @@ -19,46 +19,48 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; final class Type { - private String name; + static Type buildFrom(TypeInfo typeInfo, String name) { + return new Type(name, typeInfo.getExported(), typeInfo.getMemberList()); + } + + private final String name; + private final boolean isExported; + private final Map membersByName = new LinkedHashMap<>(); + private Type superClass; private final List superInterfaces = new ArrayList<>(); private final List immediateSubtypes = new ArrayList<>(); - private final LinkedHashMap membersByName = new LinkedHashMap<>(); private boolean live; private boolean instantiated; - private boolean isExported; private final List potentiallyLiveMembers = new ArrayList<>(); - static Type buildFrom(TypeInfo typeInfo, String name) { - Type type = new Type(); - type.name = name; - type.isExported = typeInfo.getExported(); - typeInfo - .getMemberList() - .forEach(memberInfo -> type.addMember(Member.buildFrom(memberInfo, type))); - - return type; + private Type(String name, boolean isExported, Collection members) { + this.name = name; + this.isExported = isExported; + members.forEach(memberInfo -> { + Member member = Member.buildFrom(memberInfo, this); + Member previous = membersByName.put(member.getName(), member); + checkState(previous == null); + }); } - private Type() {} + String getName() { + return name; + } - Collection getMembers() { - return membersByName.values(); + boolean isExported() { + return isExported; } Member getMemberByName(String name) { return membersByName.get(name); } - void addMember(Member member) { - Member previous = membersByName.put(member.getName(), member); - checkState(previous == null); - } - - String getName() { - return name; + Collection getMembers() { + return membersByName.values(); } Type getSuperClass() { @@ -69,7 +71,7 @@ void setSuperClass(Type superClass) { this.superClass = superClass; } - List getSuperInterfaces() { + Collection getSuperInterfaces() { return superInterfaces; } @@ -77,6 +79,14 @@ void addSuperInterface(Type superInterface) { this.superInterfaces.add(superInterface); } + Collection getImmediateSubtypes() { + return immediateSubtypes; + } + + void addImmediateSubtype(Type type) { + immediateSubtypes.add(type); + } + void markLive() { this.live = true; } @@ -93,8 +103,8 @@ void instantiate() { this.instantiated = true; } - /** Returns the list of members that need to mark as live when the type becomes live. */ - List getPotentiallyLiveMembers() { + // Returns the list of members that need to mark as live when the type becomes live. + Collection getPotentiallyLiveMembers() { return potentiallyLiveMembers; } @@ -102,16 +112,4 @@ void addPotentiallyLiveMember(Member member) { checkState(!isInstantiated()); potentiallyLiveMembers.add(member); } - - public void addImmediateSubtype(Type type) { - immediateSubtypes.add(type); - } - - public List getImmediateSubtypes() { - return immediateSubtypes; - } - - boolean isExported() { - return isExported; - } }