/**
   * Find a string return value of a method context "function() { return 'foo'}" First match wins
   */
  @Nullable
  public static String getMethodReturnAsString(
      @NotNull PhpClass phpClass, @NotNull String methodName) {

    Method method = phpClass.findMethodByName(methodName);
    if (method == null) {
      return null;
    }

    final Set<String> values = new HashSet<String>();
    method.acceptChildren(
        new PsiRecursiveElementWalkingVisitor() {
          @Override
          public void visitElement(PsiElement element) {

            if (PhpElementsUtil.getMethodReturnPattern().accepts(element)) {
              String value = PhpElementsUtil.getStringValue(element);
              if (value != null && StringUtils.isNotBlank(value)) {
                values.add(value);
              }
            }

            super.visitElement(element);
          }
        });

    if (values.size() == 0) {
      return null;
    }

    // we support only first item
    return values.iterator().next();
  }
  public static ArrayList<Method> getClassPublicMethod(PhpClass phpClass) {
    ArrayList<Method> methods = new ArrayList<Method>();

    for (Method method : phpClass.getMethods()) {
      if (method.getAccess().isPublic() && !method.getName().startsWith("__")) {
        methods.add(method);
      }
    }

    return methods;
  }
예제 #3
0
  private static void checkLeakingParameters(Class<?> jClass) throws IOException {
    final HashMap<Method, boolean[]> map = new HashMap<Method, boolean[]>();

    // collecting leakedParameters
    final ClassReader classReader =
        new ClassReader(
            new FileInputStream(
                jClass.getResource("/" + jClass.getName().replace('.', '/') + ".class").getFile()));
    classReader.accept(
        new ClassVisitor(Opcodes.ASM5) {
          @Override
          public MethodVisitor visitMethod(
              int access, String name, String desc, String signature, String[] exceptions) {
            final MethodNode node =
                new MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions);
            final Method method = new Method(classReader.getClassName(), name, desc);
            return new MethodVisitor(Opcodes.ASM5, node) {
              @Override
              public void visitEnd() {
                super.visitEnd();
                try {
                  map.put(
                      method,
                      LeakingParameters.build(classReader.getClassName(), node, false).parameters);
                } catch (AnalyzerException ignore) {
                }
              }
            };
          }
        },
        ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    for (java.lang.reflect.Method jMethod : jClass.getDeclaredMethods()) {
      Method method =
          new Method(
              Type.getType(jClass).getInternalName(),
              jMethod.getName(),
              Type.getMethodDescriptor(jMethod));
      Annotation[][] annotations = jMethod.getParameterAnnotations();
      for (int i = 0; i < annotations.length; i++) {
        boolean isLeaking = false;
        Annotation[] parameterAnnotations = annotations[i];
        for (Annotation parameterAnnotation : parameterAnnotations) {
          if (parameterAnnotation.annotationType() == ExpectLeaking.class) {
            isLeaking = true;
          }
        }
        assertEquals(method.toString() + " #" + i, isLeaking, map.get(method)[i]);
      }
    }
  }
  private static void collectMethodElementsWithParents(
      final @NotNull Method method,
      final int depth,
      @NotNull final Collection<PsiElement> elements,
      @NotNull final Processor<PsiElement> processor) {

    method.acceptChildren(
        new PsiRecursiveElementWalkingVisitor() {
          @Override
          public void visitElement(PsiElement psiElement) {

            if (processor.process(psiElement)) {
              elements.add(psiElement);
            }

            if (psiElement instanceof MethodReference
                && ((MethodReference) psiElement).getReferenceType() == PhpModifier.State.PARENT
                && method.getName().equals(((MethodReference) psiElement).getName())) {
              PsiElement resolve = ((MethodReference) psiElement).resolve();
              if (depth > 0 && resolve instanceof Method) {
                collectMethodElementsWithParents((Method) resolve, depth - 1, elements, processor);
              }
            }

            super.visitElement(psiElement);
          }
        });
  }
  public static List<Variable> getVariableReferencesInScope(
      final Variable variable, final boolean includeSelf) {

    final List<Variable> variables = new ArrayList<Variable>();

    Variable variableDecl = null;
    if (!variable.isDeclaration()) {
      PsiElement psiElement = variable.resolve();
      if (psiElement instanceof Variable) {
        variableDecl = (Variable) psiElement;
      }
    } else {
      variableDecl = variable;
    }

    if (variableDecl == null) {
      return variables;
    }

    Method method = PsiTreeUtil.getParentOfType(variable, Method.class);

    PhpPsiUtil.hasReferencesInSearchScope(
        method.getUseScope(),
        variableDecl,
        new CommonProcessors.FindProcessor<PsiReference>() {
          @Override
          protected boolean accept(PsiReference psiReference) {

            PsiElement variableRef = psiReference.getElement();
            if (variableRef instanceof Variable) {
              if (includeSelf) {
                variables.add((Variable) variableRef);
              } else {
                if (!variableRef.equals(variable)) {
                  variables.add((Variable) variableRef);
                }
              }
            }

            return false;
          }
        });

    return variables;
  }
  @Nullable
  public static PsiElement[] getMethodParameterReferences(Method method, int parameterIndex) {

    // we dont have a parameter on resolved method
    Parameter[] parameters = method.getParameters();
    if (parameters.length == 0 || parameterIndex >= parameters.length) {
      return null;
    }

    final String tempVariableName = parameters[parameterIndex].getName();
    return PsiTreeUtil.collectElements(
        method.getLastChild(),
        new PsiElementFilter() {
          @Override
          public boolean isAccepted(PsiElement element) {
            return element instanceof Variable
                && tempVariableName.equals(((Variable) element).getName());
          }
        });
  }
 protected boolean acceptLocation(
     final DebugProcessImpl debugProcess, ReferenceType classType, final Location loc) {
   Method method = loc.method();
   // Some frameworks may create synthetic methods with lines mapped to user code, see IDEA-143852
   // if (DebuggerUtils.isSynthetic(method)) { return false; }
   if (isAnonymousClass(classType)) {
     if ((method.isConstructor() && loc.codeIndex() == 0) || method.isBridge()) return false;
   }
   return ApplicationManager.getApplication()
       .runReadAction(
           new Computable<Boolean>() {
             @Override
             public Boolean compute() {
               SourcePosition position = debugProcess.getPositionManager().getSourcePosition(loc);
               if (position == null) return false;
               JavaLineBreakpointType type = getXBreakpointType();
               if (type == null) return true;
               return type.matchesPosition(LineBreakpoint.this, position);
             }
           });
 }
  private static ArrayList<Method> getImplementedMethods(
      @Nullable PhpClass phpClass, @NotNull Method method, ArrayList<Method> implementedMethods) {
    if (phpClass == null) {
      return implementedMethods;
    }

    Method[] methods = phpClass.getOwnMethods();
    for (Method ownMethod : methods) {
      if (PhpLangUtil.equalsMethodNames(ownMethod.getName(), method.getName())) {
        implementedMethods.add(ownMethod);
      }
    }

    for (PhpClass interfaceClass : phpClass.getImplementedInterfaces()) {
      getImplementedMethods(interfaceClass, method, implementedMethods);
    }

    getImplementedMethods(phpClass.getSuperClass(), method, implementedMethods);

    return implementedMethods;
  }
예제 #9
0
 @Override
 public int compare(Method m1, Method m2) {
   return LambdaMethodFilter.getLambdaOrdinal(m1.name())
       - LambdaMethodFilter.getLambdaOrdinal(m2.name());
 }
예제 #10
0
 public static String methodName(final Method m) {
   return methodName(signatureToName(m.declaringType().signature()), m.name(), m.signature());
 }
 public static Method[] getImplementedMethods(@NotNull Method method) {
   ArrayList<Method> items =
       getImplementedMethods(method.getContainingClass(), method, new ArrayList<Method>());
   return items.toArray(new Method[items.size()]);
 }