Java Code Examples for com.ibm.wala.classLoader.IMethod#getNumberOfParameters()

The following examples show how to use com.ibm.wala.classLoader.IMethod#getNumberOfParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 6 votes vote down vote up
/**
 * Get astubx style method signature. {FullyQualifiedEnclosingType}: {UnqualifiedMethodReturnType}
 * {methodName} ([{UnqualifiedArgumentType}*])
 *
 * @param mtd Method reference.
 * @return String Method signature.
 */
// TODO: handle generics and inner classes
private static String getAstubxSignature(IMethod mtd) {
  String classType =
      mtd.getDeclaringClass().getName().toString().replaceAll("/", "\\.").substring(1);
  classType = classType.replaceAll("\\$", "\\."); // handle inner class
  String returnType = mtd.isInit() ? null : getSimpleTypeName(mtd.getReturnType());
  String strArgTypes = "";
  int argi = mtd.isStatic() ? 0 : 1; // Skip 'this' parameter
  for (; argi < mtd.getNumberOfParameters(); argi++) {
    strArgTypes += getSimpleTypeName(mtd.getParameterType(argi));
    if (argi < mtd.getNumberOfParameters() - 1) strArgTypes += ", ";
  }
  return classType
      + ":"
      + (returnType == null ? "void " : returnType + " ")
      + mtd.getName().toString()
      + "("
      + strArgTypes
      + ")";
}
 
Example 2
Source File: WalaCallgraphConstructor.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * Given an IMethod, identify whether it's an object constructor&lt;clinit&gt;, class initializer&lt;clinit&gt; or a method, return the ConstructId
 *
 * @param _method
 * @return
 */
private static com.sap.psr.vulas.shared.json.model.ConstructId getCid(IMethod _method) {
    String qname = "";
    com.sap.psr.vulas.shared.json.model.ConstructId cid = null;
    if (_method.isClinit()) {
        qname = _method.getSignature().substring(0, _method.getSignature().indexOf("<") - 1);
        cid = JavaId.toSharedType(JavaId.parseClassQName(qname).getClassInit());
    } else {
        int p_size = _method.getNumberOfParameters();
        StringBuilder params = new StringBuilder("(");
        String type = "";
        if (_method.isInit()) {
            for (int i = 1; i < p_size; i++) {
                type = getRefType(_method.getParameterType(i));
                if (type.contains("$")) {
                    type = type.substring((type.lastIndexOf("$") + 1), type.length());
                }
                params.append(type);
                if (i != p_size - 1) params.append(",");
            }
            params.append(")");
            qname = _method.getSignature().substring(0, _method.getSignature().indexOf("<") - 1) + params;
            cid = JavaId.toSharedType(JavaId.parseConstructorQName(qname));
        } else {
            for (int i = (_method.isStatic() ? 0 : 1); i < p_size; i++) {
                type = getRefType(_method.getParameterType(i));
                if (type.contains("$")) {
                    type = type.substring((type.lastIndexOf("$") + 1), type.length());
                }
                params.append(type);
                if (i != p_size - 1) params.append(",");
            }
            params.append(")");
            qname = _method.getSignature().substring(0, _method.getSignature().indexOf("(")) + params;
            qname = ClassVisitor.removeParameterQualification(qname);
            cid = JavaId.toSharedType(JavaId.parseMethodQName(qname));
        }
    }
    return cid;
}
 
Example 3
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 5 votes vote down vote up
/**
 * Checks if all parameters and return value of a method have primitive types.
 *
 * @param mtd Method.
 * @return boolean True if all parameters and return value are of primitive type, otherwise false.
 */
private static boolean isAllPrimitiveTypes(IMethod mtd) {
  if (!mtd.getReturnType().isPrimitiveType()) return false;
  for (int i = (mtd.isStatic() ? 0 : 1); i < mtd.getNumberOfParameters(); i++) {
    if (!mtd.getParameterType(i).isPrimitiveType()) return false;
  }
  return true;
}
 
Example 4
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
private void analyzeFile(String pkgName, String inPath, boolean includeNonPublicClasses)
    throws IOException, ClassHierarchyException {
  InputStream jarIS = null;
  if (inPath.endsWith(".jar") || inPath.endsWith(".aar")) {
    jarIS = getInputStream(inPath);
    if (jarIS == null) {
      return;
    }
  } else if (!new File(inPath).exists()) {
    return;
  }
  AnalysisScope scope = AnalysisScopeReader.makePrimordialScope(null);
  scope.setExclusions(
      new FileOfClasses(
          new ByteArrayInputStream(DEFAULT_EXCLUSIONS.getBytes(StandardCharsets.UTF_8))));
  if (jarIS != null) scope.addInputStreamForJarToScope(ClassLoaderReference.Application, jarIS);
  else AnalysisScopeReader.addClassPathToScope(inPath, scope, ClassLoaderReference.Application);
  AnalysisOptions options = new AnalysisOptions(scope, null);
  AnalysisCache cache = new AnalysisCacheImpl();
  IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope);
  Warnings.clear();

  // Iterate over all classes:methods in the 'Application' and 'Extension' class loaders
  for (IClassLoader cldr : cha.getLoaders()) {
    if (!cldr.getName().toString().equals("Primordial")) {
      for (IClass cls : Iterator2Iterable.make(cldr.iterateAllClasses())) {
        if (cls instanceof PhantomClass) continue;
        // Only process classes in specified classpath and not its dependencies.
        // TODO: figure the right way to do this
        if (!pkgName.isEmpty() && !cls.getName().toString().startsWith(pkgName)) continue;
        // Skip non-public / ABI classes
        if (!cls.isPublic() && !includeNonPublicClasses) continue;
        LOG(DEBUG, "DEBUG", "analyzing class: " + cls.getName().toString());
        for (IMethod mtd : Iterator2Iterable.make(cls.getDeclaredMethods().iterator())) {
          // Skip methods without parameters, abstract methods, native methods
          // some Application classes are Primordial (why?)
          if (shouldCheckMethod(mtd)) {
            Preconditions.checkNotNull(mtd, "method not found");
            DefinitelyDerefedParams analysisDriver = null;
            String sign = "";
            // Parameter analysis
            if (mtd.getNumberOfParameters() > (mtd.isStatic() ? 0 : 1)) {
              // Skip methods by looking at bytecode
              try {
                if (!CodeScanner.getFieldsRead(mtd).isEmpty()
                    || !CodeScanner.getFieldsWritten(mtd).isEmpty()
                    || !CodeScanner.getCallSites(mtd).isEmpty()) {
                  analysisDriver = getAnalysisDriver(mtd, options, cache);
                  Set<Integer> result = analysisDriver.analyze();
                  sign = getSignature(mtd);
                  LOG(DEBUG, "DEBUG", "analyzed method: " + sign);
                  if (!result.isEmpty() || DEBUG) {
                    nonnullParams.put(sign, result);
                    LOG(
                        DEBUG,
                        "DEBUG",
                        "Inferred Nonnull param for method: " + sign + " = " + result.toString());
                  }
                }
              } catch (Exception e) {
                LOG(
                    DEBUG,
                    "DEBUG",
                    "Exception while scanning bytecodes for " + mtd + " " + e.getMessage());
              }
            }
            analyzeReturnValue(options, cache, mtd, analysisDriver, sign);
          }
        }
      }
    }
  }
  long endTime = System.currentTimeMillis();
  LOG(
      VERBOSE,
      "Stats",
      inPath
          + " >> time(ms): "
          + (endTime - analysisStartTime)
          + ", bytecode size: "
          + analyzedBytes
          + ", rate (ms/KB): "
          + (analyzedBytes > 0 ? (((endTime - analysisStartTime) * 1000) / analyzedBytes) : 0));
}
 
Example 5
Source File: LibApiComparator.java    From LibScout with Apache License 2.0 4 votes vote down vote up
/**
 * Check for alternative APIs in case an API is no longer available in new library version
 * Tests include
 *   1. whether only the method name was renamed (same descriptor)
 *   2. whether method name/return type are the same but one or more argument types have been generalized
 *      (e.g. ArrayList to List|Collection)
 *   3. Same method one new argument was prepended/appended
 *   4. Same method same arguments, different return type (e.g. String -> String[])
 * @param target
 * @param test
 * @return
 */
protected static boolean isAlternativeApi(IMethod target, IMethod test) {
    // Test2
    if (isApiCompatible(target, test))
        return true;

    // check whether both APIs reside in the same code Package/Class
    if (! WalaUtils.simpleName(target.getDeclaringClass()).equals(WalaUtils.simpleName(test.getDeclaringClass())))
        return false;

    // check for changes in access specifier
    if (JvmMethodAccessFlags.getMethodAccessCode(target) != JvmMethodAccessFlags.getMethodAccessCode(test)) {
        logger.trace("Access Flags incompatible: old: " + JvmMethodAccessFlags.flags2Str(target) + "   new: " + JvmMethodAccessFlags.flags2Str(test));
        return false;
    }

    // Test1: check whether method was renamed (with same descriptor)
    // Since this is very fuzzy, we further require
    //   - constructors can't be an alternative to non-constructors
    //   - at least one argument  (still fuzzy for methods with one primitive/String arg)
    // TODO:  at least one non-framework arg || at least two prim/framework args
    if (! WalaUtils.getName(target).equals(WalaUtils.getName(test))) {
        int numberOfArgs = target.getNumberOfParameters() - (target.isStatic()? 0 : 1);

        return target.getDescriptor().toString().equals(test.getDescriptor().toString()) &&
                numberOfArgs > 0 &&
                (!WalaUtils.getName(target).equals("<init>")) &&
                (!WalaUtils.getName(test).equals("<init>"));
    }

    // Test3: introduction of new argument at first/last position
    if (WalaUtils.getName(target).equals(WalaUtils.getName(test)) &&   // same method name
        (target.getReturnType().toString().equals(test.getReturnType().toString())) &&   // same return type
        (target.getNumberOfParameters() == test.getNumberOfParameters()-1)) {  // one more arg

        // check if new arg was prepended
        boolean check = true;
        for (int i = (target.isStatic()? 0 : 1); i < target.getNumberOfParameters(); i++) {
            if (!target.getParameterType(i).getName().toString().equals(test.getParameterType(i+1).getName().toString())) {
                check = false;
                break;
            }
        }

        if (check) return true;   // prepended

        // check if new arg was appended
        check = true;
        for (int i = (target.isStatic()? 0 : 1); i < target.getNumberOfParameters(); i++) {
            if (!target.getParameterType(i).getName().toString().equals(test.getParameterType(i).getName().toString())) {
                check = false;
                break;
            }
        }

        if (check) return true;   // appended
    }

    // Test4: same arg list: different return type
    if (WalaUtils.getName(target).equals(WalaUtils.getName(test)) &&   // same method name
        (!target.getReturnType().toString().equals(test.getReturnType().toString())) &&   // different return type
        (target.getNumberOfParameters() == test.getNumberOfParameters())) {  // same number of args

        // check that all arg types are equal
        boolean equal = true;
        for (int i = (target.isStatic()? 0 : 1); i < target.getNumberOfParameters(); i++) {
            if (!target.getParameterType(i).getName().toString().equals(test.getParameterType(i).getName().toString())) {
                equal = false;
                break;
            }
        }

        if (equal) return true;
    }

    return false;
}
 
Example 6
Source File: LibApiComparator.java    From LibScout with Apache License 2.0 4 votes vote down vote up
/**
 * Check whether two API are compatible, i.e. no code changes (calls to APIs) have to be made by app developers
 * This checks whether method name/return type are the same but one or more argument types have been generalized
 * (e.g. ArrayList to List|Collection
 */
protected static boolean isApiCompatible(IMethod target, IMethod test) {
    // check whether both APIs reside in the same code Package/Class
    if (! WalaUtils.simpleName(target.getDeclaringClass()).equals(WalaUtils.simpleName(test.getDeclaringClass())))
        return false;

    // check for changes in access specifier
    if (JvmMethodAccessFlags.getMethodAccessCode(target) != JvmMethodAccessFlags.getMethodAccessCode(test)) {
        logger.trace("Access Flags incompatible: old: " + JvmMethodAccessFlags.flags2Str(target) + "   new: " + JvmMethodAccessFlags.flags2Str(test));
        return false;
    }

    // check whether method name changed
    if (! WalaUtils.getName(target).equals(WalaUtils.getName(test))) {
        return false;
    }

    // if method name changed, check whether non primitive arguments were generified
    //   - first check wether return types are the same
    if (! target.getReturnType().toString().equals(test.getReturnType().toString()))
        return false;

    //   - check argument types for generalization, i.e. when a ArrayList argument was changed to List
    if (target.getNumberOfParameters() == test.getNumberOfParameters()) {
        for (int i = (target.isStatic()? 0 : 1); i < target.getNumberOfParameters(); i++) {
            if (!target.getParameterType(i).toString().equals(test.getParameterType(i).toString())) {
                // skip primitive types
                if (test.getParameterType(i).isPrimitiveType()) return false;

                // check if test argument type is supertype
                IClass paramTestClazz = test.getDeclaringClass().getClassHierarchy().lookupClass(test.getParameterType(i));

                // could be null because it's a type of a different library (sub-dependency) that is not part of the cha
                if (paramTestClazz == null) {
                    logger.warn("Could not lookup superclazz (maybe sub-dependency):");
                    logger.warn(Utils.INDENT + "target param type: " + target.getParameterType(i).getName().toString());
                    logger.warn(Utils.INDENT + "test param type  : " + test.getParameterType(i).getName().toString());

                    return false;
                }

                List<IClass> superClazzes = WalaUtils.getSuperClasses(paramTestClazz);

                boolean found = false;
                for (IClass ic: superClazzes) {
                    if (WalaUtils.simpleName(target.getDeclaringClass()).equals(WalaUtils.simpleName(ic))) {
                        found = true;
                        break;
                    }
                }

                if (!found)
                    return false;   // incompatible type replacement in test API
            }
        }
        return true;  // all arguments are the same
    }

    return false;

}