com.sap.psr.vulas.java.sign.ASTUtil Maven / Gradle / Ivy
/**
* This file is part of Eclipse Steady.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
*/
package com.sap.psr.vulas.java.sign;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.validation.constraints.NotNull;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ch.uzh.ifi.seal.changedistiller.model.classifiers.ChangeType;
import ch.uzh.ifi.seal.changedistiller.model.classifiers.EntityType;
import ch.uzh.ifi.seal.changedistiller.model.classifiers.java.JavaEntityType;
import ch.uzh.ifi.seal.changedistiller.model.entities.SourceCodeEntity;
import ch.uzh.ifi.seal.changedistiller.treedifferencing.Node;
/**
* Implements different methods for comparing nodes (incl. their descendents).
*/
public class ASTUtil {
private static final Log log = LogFactory.getLog(ASTUtil.class);
public static enum NODE_COMPARE_MODE { ENTITY_TYPE, VALUE };
/**
* Returns true if the nodes are equal with regard to parameter mode.
*
* @param _mode a {@link com.sap.psr.vulas.java.sign.ASTUtil.NODE_COMPARE_MODE} object.
* @param _left a {@link ch.uzh.ifi.seal.changedistiller.treedifferencing.Node} object.
* @param _right a {@link ch.uzh.ifi.seal.changedistiller.treedifferencing.Node} object.
* @return a boolean.
* @throws java.lang.IllegalArgumentException if any.
*/
public static final boolean isEqual(@NotNull Node _left, @NotNull Node _right, @NotNull NODE_COMPARE_MODE _mode) throws IllegalArgumentException {
boolean is_equal = true;
final Enumeration enum_left = _left.depthFirstEnumeration();
final Enumeration enum_right = _right.depthFirstEnumeration();
Node node_left = null, node_right = null;
// Traverse left tree and compare one by one
while(enum_left.hasMoreElements()) {
node_left = (Node)enum_left.nextElement();
// Compare the nodes
if(enum_right.hasMoreElements()) {
node_right = (Node)enum_right.nextElement();
if(_mode.equals(NODE_COMPARE_MODE.ENTITY_TYPE)) {
if(!node_left.getLabel().equals(node_right.getLabel())) {
is_equal = false;
}
}
else if(_mode.equals(NODE_COMPARE_MODE.VALUE)) {
if(!node_left.getValue().equals(node_right.getValue())) {
is_equal = false;
}
}
else {
throw new IllegalArgumentException("Illegal comparison mode: [" + _mode + "]");
}
}
// Right tree has less elements
else {
is_equal = false;
}
// Stop comparison upon first difference
if(!is_equal) break;
}
// Equal until now but right tree has more nodes?
if(is_equal && enum_right.hasMoreElements()) {
is_equal = false;
}
return is_equal;
}
/**
* intersectSourceCodeChanges.
*
* @param _a a {@link java.util.Collection} object.
* @param _b a {@link java.util.Collection} object.
* @param _relaxed a boolean.
* @return a {@link java.util.Set} object.
*/
public static final Set
© 2015 - 2024 Weber Informatics LLC | Privacy Policy