@NotNull
 private static List<PsiMethod> findMethodsBySignature(
     @NotNull PsiClass aClass,
     @NotNull PsiMethod patternMethod,
     boolean checkBases,
     boolean stopOnFirst) {
   final PsiMethod[] methodsByName = aClass.findMethodsByName(patternMethod.getName(), checkBases);
   if (methodsByName.length == 0) return Collections.emptyList();
   final List<PsiMethod> methods = new SmartList<PsiMethod>();
   final MethodSignature patternSignature = patternMethod.getSignature(PsiSubstitutor.EMPTY);
   for (final PsiMethod method : methodsByName) {
     final PsiClass superClass = method.getContainingClass();
     final PsiSubstitutor substitutor;
     if (checkBases && !aClass.equals(superClass)) {
       substitutor =
           TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, PsiSubstitutor.EMPTY);
     } else {
       substitutor = PsiSubstitutor.EMPTY;
     }
     final MethodSignature signature = method.getSignature(substitutor);
     if (signature.equals(patternSignature)) {
       methods.add(method);
       if (stopOnFirst) {
         break;
       }
     }
   }
   return methods;
 }
Esempio n. 2
0
 private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
   Map<K, V> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey(), rowBounds);
   } else {
     result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey());
   }
   return result;
 }
Esempio n. 3
0
 private <T> Cursor<T> executeForCursor(SqlSession sqlSession, Object[] args) {
   Cursor<T> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<T>selectCursor(command.getName(), param, rowBounds);
   } else {
     result = sqlSession.<T>selectCursor(command.getName(), param);
   }
   return result;
 }
  @Nullable
  public static PsiMethod findMethodBySignature(
      GrTypeDefinition grType, PsiMethod patternMethod, boolean checkBases) {
    final MethodSignature patternSignature = patternMethod.getSignature(PsiSubstitutor.EMPTY);
    for (PsiMethod method : findMethodsByName(grType, patternMethod.getName(), checkBases, false)) {
      MethodSignature signature = getSignatureForInheritor(method, grType);
      if (patternSignature.equals(signature)) return method;
    }

    return null;
  }
 private static PsiMethod[] findMethodsBySignature(
     GrTypeDefinition grType,
     PsiMethod patternMethod,
     boolean checkBases,
     boolean includeSynthetic) {
   ArrayList<PsiMethod> result = new ArrayList<PsiMethod>();
   final MethodSignature patternSignature = patternMethod.getSignature(PsiSubstitutor.EMPTY);
   for (PsiMethod method :
       findMethodsByName(grType, patternMethod.getName(), checkBases, includeSynthetic)) {
     MethodSignature signature = getSignatureForInheritor(method, grType);
     if (patternSignature.equals(signature)) {
       result.add(method);
     }
   }
   return result.toArray(new PsiMethod[result.size()]);
 }
Esempio n. 6
0
 @Nullable
 private static List<MethodSignature> hasSubsignature(List<MethodSignature> signatures) {
   for (MethodSignature signature : signatures) {
     boolean subsignature = true;
     for (MethodSignature methodSignature : signatures) {
       if (!signature.equals(methodSignature)) {
         if (!MethodSignatureUtil.isSubsignature(signature, methodSignature)) {
           subsignature = false;
           break;
         }
       }
     }
     if (subsignature) return Collections.singletonList(signature);
   }
   return signatures;
 }
Esempio n. 7
0
 private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
   MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
   if (void.class.equals(ms.getResultMaps().get(0).getType())) {
     throw new BindingException(
         "method "
             + command.getName()
             + " needs either a @ResultMap annotation, a @ResultType annotation,"
             + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
   }
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
   } else {
     sqlSession.select(command.getName(), param, method.extractResultHandler(args));
   }
 }
Esempio n. 8
0
 private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
   List<E> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
   } else {
     result = sqlSession.<E>selectList(command.getName(), param);
   }
   // issue #510 Collections & arrays support
   if (!method.getReturnType().isAssignableFrom(result.getClass())) {
     if (method.getReturnType().isArray()) {
       return convertToArray(result);
     } else {
       return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
     }
   }
   return result;
 }
Esempio n. 9
0
 @Nullable
 private static PsiMethod getMethod(PsiClass psiClass, MethodSignature methodSignature) {
   final PsiMethod[] methodsByName = psiClass.findMethodsByName(methodSignature.getName(), true);
   for (PsiMethod psiMethod : methodsByName) {
     if (MethodSignatureUtil.areSignaturesEqual(
         getMethodSignature(psiMethod, psiClass, psiMethod.getContainingClass()),
         methodSignature)) {
       return psiMethod;
     }
   }
   return null;
 }
Esempio n. 10
0
 private Object rowCountResult(int rowCount) {
   final Object result;
   if (method.returnsVoid()) {
     result = null;
   } else if (Integer.class.equals(method.getReturnType())
       || Integer.TYPE.equals(method.getReturnType())) {
     result = Integer.valueOf(rowCount);
   } else if (Long.class.equals(method.getReturnType())
       || Long.TYPE.equals(method.getReturnType())) {
     result = Long.valueOf(rowCount);
   } else if (Boolean.class.equals(method.getReturnType())
       || Boolean.TYPE.equals(method.getReturnType())) {
     result = Boolean.valueOf(rowCount > 0);
   } else {
     throw new BindingException(
         "Mapper method '"
             + command.getName()
             + "' has an unsupported return type: "
             + method.getReturnType());
   }
   return result;
 }
Esempio n. 11
0
 public Object execute(SqlSession sqlSession, Object[] args) {
   Object result;
   if (SqlCommandType.INSERT == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.insert(command.getName(), param));
   } else if (SqlCommandType.UPDATE == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.update(command.getName(), param));
   } else if (SqlCommandType.DELETE == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.delete(command.getName(), param));
   } else if (SqlCommandType.SELECT == command.getType()) {
     if (method.returnsVoid() && method.hasResultHandler()) {
       executeWithResultHandler(sqlSession, args);
       result = null;
     } else if (method.returnsMany()) {
       result = executeForMany(sqlSession, args);
     } else if (method.returnsMap()) {
       result = executeForMap(sqlSession, args);
     } else if (method.returnsCursor()) {
       result = executeForCursor(sqlSession, args);
     } else {
       Object param = method.convertArgsToSqlCommandParam(args);
       result = sqlSession.selectOne(command.getName(), param);
     }
   } else if (SqlCommandType.FLUSH == command.getType()) {
     result = sqlSession.flushStatements();
   } else {
     throw new BindingException("Unknown execution method for: " + command.getName());
   }
   if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
     throw new BindingException(
         "Mapper method '"
             + command.getName()
             + " attempted to return null from a method with a primitive return type ("
             + method.getReturnType()
             + ").");
   }
   return result;
 }
Esempio n. 12
0
 @SuppressWarnings("unchecked")
 private <E> E[] convertToArray(List<E> list) {
   E[] array = (E[]) Array.newInstance(method.getReturnType().getComponentType(), list.size());
   array = list.toArray(array);
   return array;
 }
Esempio n. 13
0
 private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
   Object collection = config.getObjectFactory().create(method.getReturnType());
   MetaObject metaObject = config.newMetaObject(collection);
   metaObject.addAll(list);
   return collection;
 }
Esempio n. 14
0
  public static boolean isAcceptable(
      PsiLambdaExpression lambdaExpression, final PsiType leftType, boolean checkReturnType) {
    if (leftType instanceof PsiIntersectionType) {
      for (PsiType conjunctType : ((PsiIntersectionType) leftType).getConjuncts()) {
        if (isAcceptable(lambdaExpression, conjunctType, checkReturnType)) return true;
      }
      return false;
    }
    final PsiClassType.ClassResolveResult resolveResult =
        PsiUtil.resolveGenericsClassInType(GenericsUtil.eliminateWildcards(leftType));
    final PsiClass psiClass = resolveResult.getElement();
    if (psiClass instanceof PsiAnonymousClass) {
      return isAcceptable(
          lambdaExpression, ((PsiAnonymousClass) psiClass).getBaseClassType(), checkReturnType);
    }
    final MethodSignature methodSignature = getFunction(psiClass);
    if (methodSignature == null) return false;
    final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
    final PsiType[] parameterTypes = methodSignature.getParameterTypes();
    if (lambdaParameters.length != parameterTypes.length) return false;
    for (int lambdaParamIdx = 0, length = lambdaParameters.length;
        lambdaParamIdx < length;
        lambdaParamIdx++) {
      PsiParameter parameter = lambdaParameters[lambdaParamIdx];
      final PsiTypeElement typeElement = parameter.getTypeElement();
      if (typeElement != null) {
        final PsiType lambdaFormalType = typeElement.getType();
        final PsiType methodParameterType = parameterTypes[lambdaParamIdx];
        if (lambdaFormalType instanceof PsiPrimitiveType) {
          if (methodParameterType instanceof PsiPrimitiveType)
            return methodParameterType.equals(lambdaFormalType);
          return false;
        }

        if (!TypeConversionUtil.erasure(lambdaFormalType)
            .isAssignableFrom(
                TypeConversionUtil.erasure(
                    GenericsUtil.eliminateWildcards(
                        resolveResult
                            .getSubstitutor()
                            .substitute(
                                methodSignature
                                    .getSubstitutor()
                                    .substitute(methodParameterType)))))) {
          return false;
        }
      }
    }
    if (checkReturnType) {
      final String uniqueVarName =
          JavaCodeStyleManager.getInstance(lambdaExpression.getProject())
              .suggestUniqueVariableName("l", lambdaExpression, true);
      String canonicalText = leftType.getCanonicalText();
      if (leftType instanceof PsiEllipsisType) {
        canonicalText = ((PsiEllipsisType) leftType).toArrayType().getCanonicalText();
      }
      final PsiStatement assignmentFromText =
          JavaPsiFacade.getElementFactory(lambdaExpression.getProject())
              .createStatementFromText(
                  canonicalText + " " + uniqueVarName + " = " + lambdaExpression.getText(),
                  lambdaExpression);
      final PsiLocalVariable localVariable =
          (PsiLocalVariable)
              ((PsiDeclarationStatement) assignmentFromText).getDeclaredElements()[0];
      LOG.assertTrue(psiClass != null);
      PsiType methodReturnType = getReturnType(psiClass, methodSignature);
      if (methodReturnType != null) {
        methodReturnType =
            resolveResult
                .getSubstitutor()
                .substitute(methodSignature.getSubstitutor().substitute(methodReturnType));
        return LambdaHighlightingUtil.checkReturnTypeCompatible(
                (PsiLambdaExpression) localVariable.getInitializer(), methodReturnType)
            == null;
      }
    }
    return true;
  }