|
| 1 | +package de.upb.soot.diff; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.builder.DiffBuilder; |
| 4 | +import org.apache.commons.lang3.builder.DiffResult; |
| 5 | +import org.apache.commons.lang3.builder.Diffable; |
| 6 | +import org.apache.commons.lang3.builder.ToStringStyle; |
| 7 | +import soot.SootClass; |
| 8 | +import soot.SootField; |
| 9 | +import soot.SootMethod; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | + |
| 16 | +//FIXME: remove the old version |
| 17 | +/** @author Andreas Dann created on 06.12.18 */ |
| 18 | +public class DiffSootClass implements Diffable<DiffSootClass> { |
| 19 | + |
| 20 | + public SootClass sootClass; |
| 21 | + |
| 22 | + public DiffSootClass(SootClass sootClass) { |
| 23 | + this.sootClass = sootClass; |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + //instead of this it may make sense to subclass the diffbuilder.... |
| 28 | + |
| 29 | + public DiffResult diff(DiffSootClass rhs) { |
| 30 | + DiffBuilder diffBuilder = new DiffBuilder(this, rhs, ToStringStyle.JSON_STYLE); |
| 31 | + diffBuilder = |
| 32 | + diffBuilder.append("name", sootClass.getJavaStyleName(), rhs.sootClass.getJavaStyleName()); |
| 33 | + |
| 34 | + diffBuilder = |
| 35 | + diffBuilder.append("modifier", sootClass.getModifiers(), rhs.sootClass.getModifiers()); |
| 36 | + |
| 37 | + // check the implemented interfaces |
| 38 | + |
| 39 | + List<String> lhsInterFaces = |
| 40 | + sootClass |
| 41 | + .getInterfaces() |
| 42 | + .stream() |
| 43 | + .map(intf -> intf.getJavaStyleName()) |
| 44 | + .collect(Collectors.toList()); |
| 45 | + |
| 46 | + List<String> rhsInterfaces = |
| 47 | + rhs.sootClass |
| 48 | + .getInterfaces() |
| 49 | + .stream() |
| 50 | + .map(intf -> intf.getJavaStyleName()) |
| 51 | + .collect(Collectors.toList()); |
| 52 | + |
| 53 | + diffBuilder = diffBuilder.append("interfaces", lhsInterFaces, rhsInterfaces); |
| 54 | + |
| 55 | + // check the super classes |
| 56 | + |
| 57 | + String lhsSuperClasses = sootClass.getSuperclass().getJavaStyleName(); |
| 58 | + |
| 59 | + String rhsSuperClasses = rhs.sootClass.getSuperclass().getJavaStyleName(); |
| 60 | + |
| 61 | + diffBuilder = diffBuilder.append("superclass", lhsSuperClasses, rhsSuperClasses); |
| 62 | + |
| 63 | + // check the fields |
| 64 | + |
| 65 | + List<SootField> rhsFieldsToVisit = new ArrayList<>(rhs.sootClass.getFields()); |
| 66 | + |
| 67 | + // append the diff results of the methods ... |
| 68 | + for (SootField lhsField : this.sootClass.getFields()) { |
| 69 | + try { |
| 70 | + SootField rhsField = rhs.sootClass.getField(lhsField.getSubSignature()); |
| 71 | + diffBuilder = |
| 72 | + diffBuilder.append( |
| 73 | + lhsField.getName(), lhsField.getSignature(), rhsField.getSignature()); |
| 74 | + |
| 75 | + // remove from the methods to visit |
| 76 | + rhsFieldsToVisit.remove(rhsField); |
| 77 | + |
| 78 | + } catch (RuntimeException ex) { |
| 79 | + // no corresponding righthand method found |
| 80 | + // dummy method to compute diff |
| 81 | + diffBuilder = diffBuilder.append(lhsField.getName(), lhsField.getSignature(), null); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // get the diff for fields that are in the right class but not in the lhs |
| 86 | + for (SootField rhsField : rhsFieldsToVisit) { |
| 87 | + // no lhsMethod exists |
| 88 | + // create dummy lhsMethod |
| 89 | + // SootField lhsField = new SootField(null, null, 0); |
| 90 | + diffBuilder = diffBuilder.append(rhsField.getName(), null, rhsField.getSignature()); |
| 91 | + } |
| 92 | + |
| 93 | + // check the methods |
| 94 | + List<SootMethod> rhsMethodsToVisit = new ArrayList<>(rhs.sootClass.getMethods()); |
| 95 | + |
| 96 | + // append the diff results of the methods ... |
| 97 | + for (SootMethod lhsMethod : this.sootClass.getMethods()) { |
| 98 | + try { |
| 99 | + SootMethod rhsMethod = rhs.sootClass.getMethod(lhsMethod.getSubSignature()); |
| 100 | + DiffResult diff = new DiffSootMethod(lhsMethod).diff(new DiffSootMethod(rhsMethod)); |
| 101 | + diffBuilder = diffBuilder.append(lhsMethod.getName(), diff); |
| 102 | + |
| 103 | + // remove from the methods to visit |
| 104 | + rhsMethodsToVisit.remove(rhsMethod); |
| 105 | + |
| 106 | + } catch (RuntimeException ex) { |
| 107 | + // no corresponding righthand method found |
| 108 | + // dummy method to compute diff |
| 109 | + diffBuilder = diffBuilder.append(lhsMethod.getName(), new DiffSootMethod(lhsMethod), null); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // get the diff for methods that are in the right class but not in the lhs |
| 114 | + for (SootMethod rhsMethod : rhsMethodsToVisit) { |
| 115 | + // no lhsMethod exists |
| 116 | + // create dummy lhsMethod |
| 117 | + SootMethod lhsMethod = null; |
| 118 | + // DiffResult diff = new DiffSootMethod(lhsMethod).diff(new DiffSootMethod(rhsMethod)); |
| 119 | + diffBuilder = diffBuilder.append(rhsMethod.getName(), null, new DiffSootMethod(rhsMethod)); |
| 120 | + } |
| 121 | + |
| 122 | + return diffBuilder.build(); |
| 123 | + } |
| 124 | +} |
0 commit comments