1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.diff; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import java.util.*; |
37 |
| |
38 |
| import com.jeantessier.classreader.*; |
39 |
| |
40 |
| public class ListBasedDifferenceStrategy extends DifferenceStrategyDecorator { |
41 |
| private Collection<String> allowedElements = new HashSet<String>(); |
42 |
| |
43 |
1
| public ListBasedDifferenceStrategy(DifferenceStrategy delegate, String filename) throws IOException {
|
44 |
1
| super(delegate);
|
45 |
| |
46 |
1
| load(filename);
|
47 |
| } |
48 |
| |
49 |
1
| public ListBasedDifferenceStrategy(DifferenceStrategy delegate, File file) throws IOException {
|
50 |
1
| super(delegate);
|
51 |
| |
52 |
1
| load(file);
|
53 |
| } |
54 |
| |
55 |
23
| public ListBasedDifferenceStrategy(DifferenceStrategy delegate, BufferedReader in) throws IOException {
|
56 |
23
| super(delegate);
|
57 |
| |
58 |
23
| load(in);
|
59 |
| } |
60 |
| |
61 |
1
| public void load(String filename) throws IOException {
|
62 |
1
| BufferedReader in = null;
|
63 |
| |
64 |
1
| try {
|
65 |
1
| in = new BufferedReader(new FileReader(filename));
|
66 |
0
| load(in);
|
67 |
| } finally { |
68 |
1
| if (in != null) {
|
69 |
0
| in.close();
|
70 |
| } |
71 |
| } |
72 |
| } |
73 |
| |
74 |
1
| public void load(File file) throws IOException {
|
75 |
1
| BufferedReader in = null;
|
76 |
| |
77 |
1
| try {
|
78 |
1
| in = new BufferedReader(new FileReader(file));
|
79 |
0
| load(in);
|
80 |
| } finally { |
81 |
1
| if (in != null) {
|
82 |
0
| in.close();
|
83 |
| } |
84 |
| } |
85 |
| } |
86 |
| |
87 |
27
| public void load(BufferedReader in) throws IOException {
|
88 |
27
| String line;
|
89 |
?
| while ((line = in.readLine()) != null) {
|
90 |
18
| if (line.length() > 0) {
|
91 |
18
| line = line.trim();
|
92 |
18
| int pos = line.lastIndexOf(" [");
|
93 |
18
| if (pos != -1) {
|
94 |
6
| allowedElements.add(line.substring(0, pos));
|
95 |
| } else { |
96 |
12
| allowedElements.add(line);
|
97 |
| } |
98 |
| } |
99 |
| } |
100 |
| } |
101 |
| |
102 |
17
| public boolean isPackageAllowed(String name) {
|
103 |
17
| return isAllowed(name);
|
104 |
| } |
105 |
| |
106 |
16
| public boolean isClassAllowed(String name) {
|
107 |
16
| return isAllowed(name);
|
108 |
| } |
109 |
| |
110 |
50
| public boolean isFeatureAllowed(String name) {
|
111 |
50
| return isAllowed(name);
|
112 |
| } |
113 |
| |
114 |
83
| public boolean isAllowed(String name) {
|
115 |
83
| return allowedElements.contains(name);
|
116 |
| } |
117 |
| |
118 |
17
| public boolean isPackageDifferent(Map<String, Classfile> oldPackage, Map<String, Classfile> newPackage) {
|
119 |
17
| Map<String, Classfile> nonEmptyPackage = oldPackage.isEmpty() ? newPackage : oldPackage;
|
120 |
17
| String className = nonEmptyPackage.keySet().iterator().next();
|
121 |
17
| String packageName = "";
|
122 |
17
| int pos = className.lastIndexOf('.');
|
123 |
17
| if (pos != -1) {
|
124 |
17
| packageName = className.substring(0, pos);
|
125 |
| } |
126 |
| |
127 |
17
| boolean result = isPackageAllowed(packageName);
|
128 |
17
| if (result) {
|
129 |
6
| result = super.isPackageDifferent(oldPackage, newPackage);
|
130 |
| } |
131 |
| |
132 |
17
| return result;
|
133 |
| } |
134 |
| |
135 |
16
| public boolean isClassDifferent(Classfile oldClass, Classfile newClass) {
|
136 |
16
| Classfile classfile = oldClass != null ? oldClass : newClass;
|
137 |
| |
138 |
16
| boolean result = isClassAllowed(classfile.getClassName());
|
139 |
16
| if (result) {
|
140 |
3
| result = super.isClassDifferent(oldClass, newClass);
|
141 |
| } |
142 |
| |
143 |
16
| return result;
|
144 |
| } |
145 |
| |
146 |
28
| public boolean isFieldDifferent(Field_info oldField, Field_info newField) {
|
147 |
28
| Field_info field = oldField != null ? oldField : newField;
|
148 |
| |
149 |
28
| boolean result = isFeatureAllowed(field.getFullSignature());
|
150 |
28
| if (result) {
|
151 |
3
| result = super.isFieldDifferent(oldField, newField);
|
152 |
| } |
153 |
| |
154 |
28
| return result;
|
155 |
| } |
156 |
| |
157 |
22
| public boolean isMethodDifferent(Method_info oldMethod, Method_info newMethod) {
|
158 |
22
| Method_info method = oldMethod != null ? oldMethod : newMethod;
|
159 |
| |
160 |
22
| boolean result = isFeatureAllowed(method.getFullSignature());
|
161 |
22
| if (result) {
|
162 |
5
| result = super.isMethodDifferent(oldMethod, newMethod);
|
163 |
| } |
164 |
| |
165 |
22
| return result;
|
166 |
| } |
167 |
| } |