Package gov.nasa.jpf.conformanceChecker.checkers

Source Code of gov.nasa.jpf.conformanceChecker.checkers.ModifierChecker

//
// Copyright (C) 2012 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA).  All Rights Reserved.
//
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3.  The NOSA has been approved by the Open Source
// Initiative.  See the file NOSA-1.3-JPF at the top of the distribution
// directory tree for the complete NOSA document.
//
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
//
package gov.nasa.jpf.conformanceChecker.checkers;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import gov.nasa.jpf.conformanceChecker.Inconsistency;
import gov.nasa.jpf.conformanceChecker.Inconsistency.InconsistencyType;
import gov.nasa.jpf.conformanceChecker.comparators.FieldInfoComparator;
import gov.nasa.jpf.conformanceChecker.comparators.MethodInfoComparator;
import gov.nasa.jpf.conformanceChecker.providers.ProviderFactory;
import gov.nasa.jpf.conformanceChecker.providers.StandardClassProvider;
import gov.nasa.jpf.conformanceChecker.reporters.Reporter;
import gov.nasa.jpf.conformanceChecker.util.MaybeVersion;
import gov.nasa.jpf.jvm.ClassInfo;
import gov.nasa.jpf.jvm.FieldInfo;
import gov.nasa.jpf.jvm.MethodInfo;

/**
* Checks if there are inconsistencies in the modifiers used for members.
*
* @see InconsistencyType#INCONSISTENT_MODIFIERS
*
* @author Matteo Ceccarello <matteo.ceccarello AT gmail.com>
*
*/
public class ModifierChecker extends Checker {

  StandardClassProvider provider;

  /**
   * This is the mask used to select the modifiers
   * to check. It excludes the `native` modifier, since it
   * is a widely used mechanism in JPF to handle the state
   * space explosin problem.
   */
  int mask = ~(Modifier.NATIVE);

  public ModifierChecker() {
    super();
    provider = ProviderFactory.getStandardProvider();
  }

  public ModifierChecker(List<Reporter> reporters, MemberLevel level) {
    super(reporters, level);
    provider = ProviderFactory.getStandardProvider();
  }

  public ModifierChecker(List<Reporter> reporters) {
    super(reporters);
    provider = ProviderFactory.getStandardProvider();
  }
 
  boolean modifiersEqual(MethodInfo m1, MethodInfo m2) {
    return (m1.getModifiers() & mask) == (m2.getModifiers() & mask);
  }
 
  boolean modifiersEqual(FieldInfo f1, FieldInfo f2) {
    return (f1.getModifiers() & mask) == (f2.getModifiers() & mask);
  }

  @Override
  public boolean check(ClassInfo cls) {
    Map<MaybeVersion, ClassInfo> standardVersions = provider.loadClassInfo(cls
        .getName());
    return checkMethods(cls, standardVersions)
        && checkInstanceFields(cls, standardVersions)
        && checkStaticFields(cls, standardVersions);
  }

  boolean checkMethods(ClassInfo cls, Map<MaybeVersion, ClassInfo> standardVersions) {
    boolean noInconsistencies = true;
    MethodInfoComparator cmp = new MethodInfoComparator();
    MethodInfo[] modelMethods = cls.getDeclaredMethodInfos();
    for (MaybeVersion ver : standardVersions.keySet()) {
      ClassInfo std = standardVersions.get(ver);
      MethodInfo[] stdMethods = std.getDeclaredMethodInfos();
      Arrays.sort(stdMethods, cmp);
      for (MethodInfo m : modelMethods) {
        if (MemberLevel.getMemberLevel(m).compareTo(this.level) >= 0) {
          int idx = Arrays.binarySearch(stdMethods, m, cmp);
          if (idx >= 0) {
            if (!modifiersEqual(m, stdMethods[idx])) {
              signal(new Inconsistency(
                  InconsistencyType.INCONSISTENT_MODIFIERS, ver, cls, m,
                  "The library version is: "
                      + Modifier.toString(stdMethods[idx].getModifiers())));
              noInconsistencies = false;
            }
          }
        }
      }
    }

    return noInconsistencies;
  }

  boolean checkInstanceFields(ClassInfo cls,
      Map<MaybeVersion, ClassInfo> standardVersions) {
    boolean noInconsistencies = true;
    FieldInfoComparator cmp = new FieldInfoComparator();
    FieldInfo[] modelInstanceFields = cls.getDeclaredInstanceFields();
    for (MaybeVersion ver : standardVersions.keySet()) {
      ClassInfo std = standardVersions.get(ver);
      FieldInfo[] stdInstanceFields = std.getDeclaredInstanceFields();
      Arrays.sort(stdInstanceFields, cmp);
      for (FieldInfo f : modelInstanceFields) {
        if (MemberLevel.getMemberLevel(f).compareTo(this.level) >= 0) {
          int idx = Arrays.binarySearch(stdInstanceFields, f, cmp);
          if (idx >= 0) {
            if (!modifiersEqual(f, stdInstanceFields[idx])) {
              signal(new Inconsistency(
                  InconsistencyType.INCONSISTENT_MODIFIERS, ver, cls, f,
                    "The library version is: "
                      + Modifier.toString(stdInstanceFields[idx].getModifiers())));
              noInconsistencies = false;
            }
          }
        }
      }
    }
    return noInconsistencies;
  }

  boolean checkStaticFields(ClassInfo cls,
      Map<MaybeVersion, ClassInfo> standardVersions) {
    boolean noInconsistencies = true;
    FieldInfoComparator cmp = new FieldInfoComparator();
    FieldInfo[] modelStaticFields = cls.getDeclaredStaticFields();
    for (MaybeVersion ver : standardVersions.keySet()) {
      ClassInfo std = standardVersions.get(ver);
      FieldInfo[] stdStaticFields = std.getDeclaredStaticFields();
      Arrays.sort(stdStaticFields, cmp);
      for (FieldInfo f : modelStaticFields) {
        if (MemberLevel.getMemberLevel(f).compareTo(this.level) >= 0) {
          int idx = Arrays.binarySearch(stdStaticFields, f, cmp);
          if (idx >= 0) {
            if (!modifiersEqual(f, stdStaticFields[idx])) {
              signal(new Inconsistency(
                  InconsistencyType.INCONSISTENT_MODIFIERS, ver, cls, f,
                    "The library version is: "
                      + Modifier.toString(stdStaticFields[idx].getModifiers())));
              noInconsistencies = false;
            }
          }
        }
      }
    }
    return noInconsistencies;
  }
}
TOP

Related Classes of gov.nasa.jpf.conformanceChecker.checkers.ModifierChecker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.