aboutsummaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/osjava/jardiff/PublicDiffCriteria.java
blob: 90fb38277464c3d419c92f0557a5e89f33741d07 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Copyright 2012-2014 Julien Eluard and contributors
 * This project includes software developed by Julien Eluard: https://github.com/jeluard/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.osjava.jardiff;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * A specific type of DiffCriteria which is only true for classes, methods
 * and fields which are not synthetic and public.
 *
 * @author <a href="mailto:antony@cyberiantiger.org">Antony Riley</a>
 */
public class PublicDiffCriteria implements DiffCriteria
{
    /**
     * Check if a class is valid.
     * If the class is not synthetic and public, return true.
     *
     * @param info Info describing the class.
     * @return True if the class meets the criteria, false otherwise.
     */
    public boolean validClass(ClassInfo info) {
        return !info.isSynthetic() && info.isPublic();
    }

    /**
     * Check if a method is valid.
     * If the method is not synthetic and public, return true.
     *
     * @param info Info describing the method.
     * @return True if the method meets the criteria, false otherwise.
     */
    public boolean validMethod(MethodInfo info) {
        return !info.isSynthetic() && info.isPublic();
    }

    /**
     * Check if a field is valid.
     * If the method is not synthetic and public, return true.
     *
     * @param info Info describing the field.
     * @return True if the field meets the criteria, false otherwise.
     */
    public boolean validField(FieldInfo info) {
        return !info.isSynthetic() && info.isPublic();
    }

    /**
     * Check if there is a change between two versions of a class.
     * Returns true if the access flags differ, or if the superclass differs
     * or if the implemented interfaces differ.
     *
     * @param oldInfo Info about the old version of the class.
     * @param newInfo Info about the new version of the class.
     * @return True if the classes differ, false otherwise.
     */
    public boolean differs(ClassInfo oldInfo, ClassInfo newInfo) {
        if (Tools.isClassAccessChange(oldInfo.getAccess(), newInfo.getAccess()))
            return true;
        // Yes classes can have a null supername, e.g. java.lang.Object !
        if(oldInfo.getSupername() == null) {
            if(newInfo.getSupername() != null) {
                return true;
            }
        } else if (!oldInfo.getSupername().equals(newInfo.getSupername())) {
            return true;
        }
        final Set<String> oldInterfaces
            = new HashSet(Arrays.asList(oldInfo.getInterfaces()));
        final Set<String> newInterfaces
            = new HashSet(Arrays.asList(newInfo.getInterfaces()));
        if (!oldInterfaces.equals(newInterfaces))
            return true;
        return false;
    }

    /**
     * Check if there is a change between two versions of a method.
     * Returns true if the access flags differ, or if the thrown
     * exceptions differ.
     *
     * @param oldInfo Info about the old version of the method.
     * @param newInfo Info about the new version of the method.
     * @return True if the methods differ, false otherwise.
     */
    public boolean differs(MethodInfo oldInfo, MethodInfo newInfo) {
        if (Tools.isMethodAccessChange(oldInfo.getAccess(), newInfo.getAccess()))
            return true;
        if (oldInfo.getExceptions() == null
            || newInfo.getExceptions() == null) {
            if (oldInfo.getExceptions() != newInfo.getExceptions())
                return true;
        } else {
            final Set<String> oldExceptions
                = new HashSet(Arrays.asList(oldInfo.getExceptions()));
            final Set<String> newExceptions
                = new HashSet(Arrays.asList(newInfo.getExceptions()));
            if (!oldExceptions.equals(newExceptions))
                return true;
        }
        return false;
    }

    /**
     * Check if there is a change between two versions of a field.
     * Returns true if the access flags differ, or if the inital value
     * of the field differs.
     *
     * @param oldInfo Info about the old version of the field.
     * @param newInfo Info about the new version of the field.
     * @return True if the fields differ, false otherwise.
     */
    public boolean differs(FieldInfo oldInfo, FieldInfo newInfo) {
        if (Tools.isFieldAccessChange(oldInfo.getAccess(), newInfo.getAccess()))
            return true;
        if (oldInfo.getValue() == null || newInfo.getValue() == null) {
            if (oldInfo.getValue() != newInfo.getValue())
                return true;
        } else if (!oldInfo.getValue().equals(newInfo.getValue()))
            return true;
        return false;
    }
}