X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fstructure%2FMethodDescriptor.java;h=96825fd21b3f3bbcb94746839a6049199d46afa1;hb=7d1259aea992843c47f29c932434a88ea9364f7e;hp=cff1f8a77d055c1a0cbdf8df9646d7a6545e0a1c;hpb=cd718904f053ee6901e9b21d76a024ef646bb01c;p=javainspect.git diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java index cff1f8a..96825fd 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/MethodDescriptor.java @@ -1,7 +1,7 @@ /* * JavaInspect - Utility to visualize java software - * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2013-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or * modify it under the terms of version 3 of the GNU Lesser General Public License * or later as published by the Free Software Foundation. @@ -15,157 +15,189 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +/** + * This class corresponds to single method within a java class. + */ public class MethodDescriptor implements GraphElement, - Comparable { - - /** - * This class corresponds to single method within a java class. - */ - - public String methodName; - public ClassDescriptor returnType; - private final ClassDescriptor parentClass; - - List argumentTypes = new ArrayList(); - - public MethodDescriptor(final Method method, final ClassDescriptor parent, - final ClassGraph dump) { - - parentClass = parent; - - methodName = method.getName(); - - if (!method.getDeclaringClass().getName() - .equals(parent.classFullyQualifiedName)) - // do not index inherited methods - return; - - parent.methods.add(this); - - returnType = dump.addClass(method.getReturnType()); - returnType.registerReference(); - - final Type genericType = method.getGenericReturnType(); - if (genericType instanceof ParameterizedType) { - final ParameterizedType pt = (ParameterizedType) genericType; - for (final Type t : pt.getActualTypeArguments()) - if (t instanceof Class) { - final Class cl = (Class) t; - final ClassDescriptor classDescriptor = dump.addClass(cl); - classDescriptor.registerReference(); - argumentTypes.add(classDescriptor); - } - - } - } - - @Override - public int compareTo(final MethodDescriptor o) { - - final int nameComparisonResult = methodName.compareTo(o.methodName); - if (nameComparisonResult != 0) - return nameComparisonResult; - - return toString().compareTo(o.toString()); - } - - @Override - public String getDot() { - - if (!isVisible()) - return ""; - - final StringBuffer result = new StringBuffer(); - - // describe associated types - for (final ClassDescriptor classDescriptor : argumentTypes) - if (classDescriptor.isVisible()) - if (classDescriptor.areReferencesShown()) - result.append(" " + getGraphId() + " -> " - + classDescriptor.getGraphId() + "[label=\"" + methodName - + "\", color=\"" + classDescriptor.getColor() - + "\", style=\"dotted, bold\"];\n"); - - if (!returnType.isVisible()) - return result.toString(); - - // main type - if (returnType.areReferencesShown()) - result.append(" " + getGraphId() + " -> " - + returnType.getGraphId() + "[label=\"" + methodName + "\"," - + " color=\"" + returnType.getColor() - + "\", style=\"dotted, bold\"];\n"); - - return result.toString(); - } + Comparable { + + private final String methodName; + private final ClassDescriptor parentClass; + private final List argumentTypes = new ArrayList<>(); + private ClassDescriptor returnType; + private boolean isInherited; + + public MethodDescriptor(final ClassDescriptor parent, + final String methodName) { + parentClass = parent; + this.methodName = methodName; + } + + public void analyze(final Method method) { + + if (!method.getDeclaringClass().getName() + .equals(parentClass.getFullyQualifiedName())) + isInherited = true; + + returnType = parentClass.getClassGraph().getOrCreateClassDescriptor( + method.getReturnType()); + returnType.registerReference(); + + final Type genericType = method.getGenericReturnType(); + if (genericType instanceof ParameterizedType) { + final ParameterizedType pt = (ParameterizedType) genericType; + for (final Type t : pt.getActualTypeArguments()) + if (t instanceof Class) { + final Class cl = (Class) t; + final ClassDescriptor classDescriptor = parentClass + .getClassGraph().getOrCreateClassDescriptor(cl); + classDescriptor.registerReference(); + argumentTypes.add(classDescriptor); + } + + } + } - @Override - public String getEmbeddedDot() { - if (!isVisible()) - return ""; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MethodDescriptor)) return false; + + MethodDescriptor that = (MethodDescriptor) o; - final StringBuffer result = new StringBuffer(); + if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false; + if (parentClass != null ? !parentClass.equals(that.parentClass) : that.parentClass != null) return false; + return argumentTypes != null ? argumentTypes.equals(that.argumentTypes) : that.argumentTypes == null; - result.append(" // " + methodName + "\n"); + } + + + @Override + public int hashCode() { + int result = methodName != null ? methodName.hashCode() : 0; + result = 31 * result + (parentClass != null ? parentClass.hashCode() : 0); + result = 31 * result + (argumentTypes != null ? argumentTypes.hashCode() : 0); + return result; + } + + @Override + public String getDot() { + + if (!isVisible()) + return ""; + + final StringBuilder result = new StringBuilder(); + + // describe associated types + for (final ClassDescriptor classDescriptor : argumentTypes) + if (classDescriptor.isVisible()) + if (classDescriptor.areReferencesShown()) + result.append(" " + getGraphId() + " -> " + + classDescriptor.getGraphId() + "[label=\"" + + methodName + "\", color=\"" + + classDescriptor.getColor() + + "\", style=\"dotted, bold\"];\n"); - result.append(" " - + "" + returnType.getClassName(true) - + "" + "" - + getMethodLabel() + "\n"); + if (returnType == null) return result.toString(); + if (!returnType.isVisible()) return result.toString(); + + // main type + if (returnType.areReferencesShown()) + result.append(" " + getGraphId() + " -> " + + returnType.getGraphId() + "[label=\"" + methodName + + "\"," + " color=\"" + returnType.getColor() + + "\", style=\"dotted, bold\"];\n"); + + return result.toString(); + } + + @Override + public String getEmbeddedDot() { + if (!isVisible()) + return ""; + + final StringBuilder result = new StringBuilder(); + + result.append(" // " + methodName + "\n"); + + result.append(" " + + "" + describeReturnType() + + "" + "" + + getMethodLabel() + "\n"); - return result.toString(); - } + return result.toString(); + } + + private String describeReturnType() { + if (returnType == null) return "-null-"; - @Override - public String getGraphId() { - return parentClass.getGraphId() + ":" + methodName; - } + return returnType.getClassName(true); + } - public String getMethodLabel() { - return methodName; - } + @Override + public String getGraphId() { + return parentClass.getGraphId() + ":" + methodName; + } + + private String getMethodLabel() { + return methodName; + } + + protected int getOutsideVisibleReferencesCount() { + int result = 0; - public int getOutsideVisibleReferencesCount() { - int result = 0; + if (returnType != null) + if (returnType.isVisible()) + result++; - if (returnType.isVisible()) - result++; + for (final ClassDescriptor classDescriptor : argumentTypes) + if (classDescriptor.isVisible()) + result++; - for (final ClassDescriptor classDescriptor : argumentTypes) - if (classDescriptor.isVisible()) - result++; + return result; + } - return result; - } + @Override + public boolean isVisible() { - @Override - public boolean isVisible() { + // hide inherited methods + if (isInherited) + return false; - // hide common object methods - if (Utils.isCommonObjectMethod(methodName)) - return false; + // hide common object methods + if (Utils.isCommonObjectMethod(methodName)) + return false; - // hide common Enumeration methods - if (parentClass.isEnum && Utils.isEnumMethod(methodName)) - return false; + // hide common Enumeration methods + if (parentClass.isEnum && Utils.isEnumMethod(methodName)) + return false; + + // hide get/set methods for the field of the same name + if (methodName.startsWith("get") || methodName.startsWith("set")) + if (parentClass.hasFieldIgnoreCase(methodName.substring(3))) + return false; - // hide get/set methods for the field of the same name - if (methodName.startsWith("get") || methodName.startsWith("set")) - if (parentClass.hasFieldIgnoreCase(methodName.substring(3))) - return false; + // hide is methods for the boolean field of the same name + if (methodName.startsWith("is")) { + final FieldDescriptor field = parentClass + .getFieldIgnoreCase(methodName.substring(2)); + if (field != null) + if ("boolean".equals(field.getType().getFullyQualifiedName())) + return false; + } - // hide is methods for the boolean field of the same name - if (methodName.startsWith("is")) { - final FieldDescriptor field = parentClass.getFieldIgnoreCase(methodName - .substring(2)); - if (field != null) - if ("boolean".equals(field.getType().classFullyQualifiedName)) - return false; - } + return true; + + } - return true; + @Override + public int compareTo(MethodDescriptor that) { + if (this == that) return 0; - } + int comparisonResult = methodName.compareTo(that.methodName); + if (comparisonResult != 0) return comparisonResult; + return parentClass.compareTo(that.parentClass); + } }