diff --git a/src/main/java/com/probejs/formatter/formatter/FormatterClass.java b/src/main/java/com/probejs/formatter/formatter/FormatterClass.java index 44e9d91e..893b8c26 100644 --- a/src/main/java/com/probejs/formatter/formatter/FormatterClass.java +++ b/src/main/java/com/probejs/formatter/formatter/FormatterClass.java @@ -87,14 +87,14 @@ public List format(int indent, int stepIndent) { } List assignableTypes = DocManager.typesAssignable - .getOrDefault(classInfo.getClazzRaw().getName(), new ArrayList<>()) + .getOrDefault(classInfo.getRaw().getName(), new ArrayList<>()) .stream() .map(t -> t.transform(IType.defaultTransformer)) .collect(Collectors.toList()); if (classInfo.isEnum()) { //TODO: add special processing for KubeJS - Class clazz = classInfo.getClazzRaw(); + Class clazz = classInfo.getRaw(); try { Method values = clazz.getMethod("values"); values.setAccessible(true); @@ -124,12 +124,12 @@ public List format(int indent, int stepIndent) { firstLine.add("class"); } firstLine.add(NameResolver.getResolvedName(classInfo.getName()).getLastName()); - if (classInfo.getClazzRaw().getTypeParameters().length != 0) { + if (classInfo.getRaw().getTypeParameters().length != 0) { firstLine.add( String.format( "<%s>", Arrays - .stream(classInfo.getClazzRaw().getTypeParameters()) + .stream(classInfo.getRaw().getTypeParameters()) .map(TypeVariable::getName) .collect(Collectors.joining(", ")) ) @@ -138,13 +138,13 @@ public List format(int indent, int stepIndent) { // super class if (classInfo.getSuperClass() != null) { firstLine.add("extends"); - if (classInfo.getSuperClass().getClazzRaw() == Object.class) { + if (classInfo.getSuperClass().getRaw() == Object.class) { // redirect to another `Object` so that we can bypass replacement of original `Object` firstLine.add("Document.Object"); } else { firstLine.add( formatParameterized( - TypeResolver.resolveType(classInfo.getClazzRaw().getGenericSuperclass()) + TypeResolver.resolveType(classInfo.getRaw().getGenericSuperclass()) ) ); } @@ -154,7 +154,7 @@ public List format(int indent, int stepIndent) { firstLine.add(classInfo.isInterface() ? "extends" : "implements"); firstLine.add( Arrays - .stream(classInfo.getClazzRaw().getGenericInterfaces()) + .stream(classInfo.getRaw().getGenericInterfaces()) .map(TypeResolver::resolveType) .map(FormatterClass::formatParameterized) .collect(Collectors.joining(", ")) @@ -217,16 +217,14 @@ public List format(int indent, int stepIndent) { } getterMap.forEach((k, v) -> lines.addAll(v.formatBean(indent + stepIndent, stepIndent))); - setterMap.forEach((k, v) -> { - v - .stream() - .filter(m -> - !getterMap.containsKey(m.getBeanedName()) || - getterMap.get(m.getBeanedName()).getBeanTypeString().equals(m.getBeanTypeString()) - ) - .findFirst() - .ifPresent(fmtr -> lines.addAll(fmtr.formatBean(indent + stepIndent, stepIndent))); - }); + setterMap.forEach((k, v) -> v + .stream() + .filter(m -> + !getterMap.containsKey(m.getBeanedName()) || + getterMap.get(m.getBeanedName()).getBeanTypeString().equals(m.getBeanTypeString()) + ) + .findFirst() + .ifPresent(fmtr -> lines.addAll(fmtr.formatBean(indent + stepIndent, stepIndent)))); } //special processing for FunctionalInterface if (classInfo.isFunctionalInterface()) { @@ -279,11 +277,11 @@ public List format(int indent, int stepIndent) { //type conversion String origName = NameResolver.getResolvedName(classInfo.getName()).getLastName(); String underName = origName + "_"; - if (NameResolver.specialTypeFormatters.containsKey(classInfo.getClazzRaw())) { + if (NameResolver.specialTypeFormatters.containsKey(classInfo.getRaw())) { assignableTypes.add( FormatterType.of( new TypeInfoParameterized( - new TypeInfoClass(classInfo.getClazzRaw()), + new TypeInfoClass(classInfo.getRaw()), classInfo.getTypeParameters() ) ) diff --git a/src/main/java/com/probejs/info/ClassInfo.java b/src/main/java/com/probejs/info/ClassInfo.java index 6efa8307..dcc301f8 100644 --- a/src/main/java/com/probejs/info/ClassInfo.java +++ b/src/main/java/com/probejs/info/ClassInfo.java @@ -38,7 +38,7 @@ public static ClassInfo of(Class clazz) { return new ClassInfo(clazz); } - private final Class clazzRaw; + private final Class raw; private final String name; private final int modifiers; private final boolean isInterface; @@ -60,11 +60,11 @@ public static ClassInfo of(Class clazz) { private final List allFieldInfos; private ClassInfo(Class clazz) { - this.clazzRaw = clazz; - this.name = clazzRaw.getName(); - this.modifiers = clazzRaw.getModifiers(); - this.isInterface = clazzRaw.isInterface(); - this.superClass = ofCache(clazzRaw.getSuperclass()); + this.raw = clazz; + this.name = raw.getName(); + this.modifiers = raw.getModifiers(); + this.isInterface = raw.isInterface(); + this.superClass = ofCache(raw.getSuperclass()); this.superType = TypeResolver.resolveType(clazz.getGenericSuperclass()); this.interfaces = new ArrayList<>(0); @@ -80,19 +80,19 @@ private ClassInfo(Class clazz) { ); constructorInfos.addAll( Arrays - .stream(clazzRaw.getConstructors()) + .stream(raw.getConstructors()) .map(ConstructorInfo::new) .collect(Collectors.toList()) ); typeParameters.addAll( Arrays - .stream(clazzRaw.getTypeParameters()) + .stream(raw.getTypeParameters()) .map(TypeInfoVariable::new) .collect(Collectors.toList()) ); //methods Arrays - .stream(clazzRaw.getMethods()) + .stream(raw.getMethods()) .map(m -> new MethodInfo(m, clazz)) .peek(allMethodInfos::add) .filter(mInfo -> { @@ -100,7 +100,7 @@ private ClassInfo(Class clazz) { return true; } if (isInterface) { - return mInfo.getRaw().getDeclaringClass() == clazzRaw; + return mInfo.getRaw().getDeclaringClass() == raw; } return !hasIdenticalParentMethod(mInfo.getRaw(), clazz); }) @@ -109,16 +109,16 @@ private ClassInfo(Class clazz) { .forEach(methodInfos::add); //fields Arrays - .stream(clazzRaw.getFields()) + .stream(raw.getFields()) .map(f -> new FieldInfo(f, clazz)) .peek(allFieldInfos::add) - .filter(fInfo -> !ProbeJS.CONFIG.trimming || fInfo.getRaw().getDeclaringClass() == clazzRaw) + .filter(fInfo -> !ProbeJS.CONFIG.trimming || fInfo.getRaw().getDeclaringClass() == raw) .filter(f -> ClassResolver.acceptField(f.getName())) .filter(f -> !f.shouldHide()) .forEach(fieldInfos::add); } catch (NoClassDefFoundError e) { // https://github.com/ZZZank/ProbeJS-Forge/issues/2 - ProbeJS.LOGGER.error("Unable to fetch infos for class '{}'", clazzRaw.getName()); + ProbeJS.LOGGER.error("Unable to fetch infos for class '{}'", raw.getName()); e.printStackTrace(); } //Resolve types - rollback everything till Object @@ -129,7 +129,7 @@ private ClassInfo(Class clazz) { this.isFunctionalInterface = isInterface && abstracts.size() == 1; if (this.isFunctionalInterface) { NameResolver.addSpecialAssignments( - this.clazzRaw, + this.raw, () -> { FormatterMethod formatterLmbda = new FormatterMethod(abstracts.get(0)); String lmbda = String.format( @@ -162,11 +162,11 @@ private static Map resolveTypeOverrides(ITypeInfo typeInfo) { private void applySuperGenerics(List methodsToMutate, List fieldsToMutate) { if (superClass != null) { //Apply current level changes - ITypeInfo typeInfo = TypeResolver.resolveType(clazzRaw.getGenericSuperclass()); + ITypeInfo typeInfo = TypeResolver.resolveType(raw.getGenericSuperclass()); Map internalGenericMap = resolveTypeOverrides(typeInfo); applyGenerics(internalGenericMap, methodsToMutate, fieldsToMutate); Arrays - .stream(clazzRaw.getGenericInterfaces()) + .stream(raw.getGenericInterfaces()) .map(TypeResolver::resolveType) .map(ClassInfo::resolveTypeOverrides) .forEach(m -> applyGenerics(m, methodsToMutate, fieldsToMutate)); @@ -175,7 +175,7 @@ private void applySuperGenerics(List methodsToMutate, List applyGenerics(m, methodsToMutate, fieldsToMutate)); @@ -186,7 +186,7 @@ private void applySuperGenerics(List methodsToMutate, List methodsToMutate, List fieldsToMutate) { //Apply current level changes Arrays - .stream(clazzRaw.getGenericInterfaces()) + .stream(raw.getGenericInterfaces()) .map(TypeResolver::resolveType) .map(ClassInfo::resolveTypeOverrides) .forEach(m -> applyGenerics(m, methodsToMutate, fieldsToMutate)); @@ -194,7 +194,7 @@ private void applyInterfaceGenerics(List methodsToMutate, List ofCache(i.getResolvedClass()).applyInterfaceGenerics(methodsToMutate, fieldsToMutate)); //Rewind Arrays - .stream(clazzRaw.getGenericInterfaces()) + .stream(raw.getGenericInterfaces()) .map(TypeResolver::resolveType) .map(ClassInfo::resolveTypeOverrides) .forEach(m -> applyGenerics(m, methodsToMutate, fieldsToMutate)); @@ -269,11 +269,11 @@ public List getTypeParameters() { } public boolean isEnum() { - return clazzRaw.isEnum(); + return raw.isEnum(); } - public Class getClazzRaw() { - return clazzRaw; + public Class getRaw() { + return raw; } public String getName() { diff --git a/src/main/java/com/probejs/info/type/TypeInfoClass.java b/src/main/java/com/probejs/info/type/TypeInfoClass.java index 86cc79fb..b623c0d8 100644 --- a/src/main/java/com/probejs/info/type/TypeInfoClass.java +++ b/src/main/java/com/probejs/info/type/TypeInfoClass.java @@ -1,9 +1,9 @@ package com.probejs.info.type; +import com.probejs.info.ClassInfo; + import java.lang.reflect.Type; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; public class TypeInfoClass implements ITypeInfo { @@ -11,14 +11,17 @@ public static boolean test(Type type) { return type instanceof Class; } - private final Class raw; + private final ClassInfo info; public TypeInfoClass(Type type) { - this.raw = (Class) type; + if (!(type instanceof Class)){ + throw new IllegalArgumentException(); + } + this.info = ClassInfo.ofCache((Class) type); } - private TypeInfoClass(Class type) { - this.raw = type; + private TypeInfoClass(ClassInfo type) { + info = type; } @Override @@ -28,17 +31,17 @@ public ITypeInfo getBaseType() { @Override public Class getResolvedClass() { - return raw; + return this.info.getRaw(); } @Override public String getTypeName() { - return this.raw.getTypeName(); + return this.info.getRaw().getTypeName(); } @Override public ITypeInfo copy() { - return new TypeInfoClass(raw); + return new TypeInfoClass(this.info); } @Override @@ -47,18 +50,15 @@ public boolean assignableFrom(ITypeInfo info) { return false; } TypeInfoClass clazz = (TypeInfoClass) info; - return clazz.raw.isAssignableFrom(raw); + return clazz.info.getRaw().isAssignableFrom(this.info.getRaw()); } - public List getTypeVariables() { - return Arrays - .stream(raw.getTypeParameters()) - .map(TypeResolver::resolveType) - .collect(Collectors.toList()); + public List getTypeVariables() { + return this.info.getTypeParameters(); } @Override public Type getRaw() { - return this.raw; + return this.info.getRaw(); } }