Ejemplo n.º 1
0
  private void checkCompoundIds(Class<?> javaClass) throws IOException {
    String javaClassName = javaClass.getCanonicalName();
    PsiClass psiClass =
        myJavaPsiFacade.findClass(
            javaClassName, GlobalSearchScope.moduleWithLibrariesScope(myModule));
    assertNotNull(psiClass);

    for (java.lang.reflect.Method javaMethod : javaClass.getDeclaredMethods()) {
      Method method =
          new Method(
              Type.getType(javaClass).getInternalName(),
              javaMethod.getName(),
              Type.getMethodDescriptor(javaMethod));
      boolean noKey = javaMethod.getAnnotation(ExpectNoPsiKey.class) != null;
      PsiMethod psiMethod = psiClass.findMethodsByName(javaMethod.getName(), false)[0];
      checkCompoundId(method, psiMethod, noKey);
    }

    for (Constructor<?> constructor : javaClass.getDeclaredConstructors()) {
      Method method =
          new Method(
              Type.getType(javaClass).getInternalName(),
              "<init>",
              Type.getConstructorDescriptor(constructor));
      boolean noKey = constructor.getAnnotation(ExpectNoPsiKey.class) != null;
      PsiMethod[] constructors = psiClass.getConstructors();
      PsiMethod psiMethod = constructors[0];
      checkCompoundId(method, psiMethod, noKey);
    }
  }
 protected void enableInspectionToolsFromProvider(InspectionToolProvider toolProvider) {
   try {
     for (Class c : toolProvider.getInspectionClasses()) {
       enableInspectionTool((InspectionProfileEntry) c.newInstance());
     }
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 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]);
      }
    }
  }
Ejemplo n.º 4
0
  private void checkAnnotations(Class<?> javaClass) {
    PsiClass psiClass =
        myJavaPsiFacade.findClass(
            javaClass.getName(), GlobalSearchScope.moduleWithLibrariesScope(myModule));
    assertNotNull(psiClass);

    for (java.lang.reflect.Method javaMethod : javaClass.getDeclaredMethods()) {
      PsiMethod psiMethod = psiClass.findMethodsByName(javaMethod.getName(), false)[0];
      Annotation[][] annotations = javaMethod.getParameterAnnotations();

      // not-null parameters
      params:
      for (int i = 0; i < annotations.length; i++) {
        Annotation[] parameterAnnotations = annotations[i];
        PsiParameter psiParameter = psiMethod.getParameterList().getParameters()[i];
        PsiAnnotation inferredAnnotation =
            myInferredAnnotationsManager.findInferredAnnotation(
                psiParameter, AnnotationUtil.NOT_NULL);
        for (Annotation parameterAnnotation : parameterAnnotations) {
          if (parameterAnnotation.annotationType() == ExpectNotNull.class) {
            assertNotNull(javaMethod.toString() + " " + i, inferredAnnotation);
            continue params;
          }
        }
        assertNull(javaMethod.toString() + " " + i, inferredAnnotation);
      }

      // not-null result
      ExpectNotNull expectedAnnotation = javaMethod.getAnnotation(ExpectNotNull.class);
      PsiAnnotation actualAnnotation =
          myInferredAnnotationsManager.findInferredAnnotation(psiMethod, AnnotationUtil.NOT_NULL);
      assertEquals(javaMethod.toString(), expectedAnnotation == null, actualAnnotation == null);

      // contracts
      ExpectContract expectedContract = javaMethod.getAnnotation(ExpectContract.class);
      PsiAnnotation actualContractAnnotation =
          myInferredAnnotationsManager.findInferredAnnotation(
              psiMethod, ORG_JETBRAINS_ANNOTATIONS_CONTRACT);

      assertEquals(expectedContract == null, actualContractAnnotation == null);

      if (expectedContract != null) {
        String expectedContractValue = expectedContract.value();
        String actualContractValue =
            AnnotationUtil.getStringAttributeValue(actualContractAnnotation, null);
        assertEquals(javaMethod.toString(), expectedContractValue, actualContractValue);
      }
    }
  }
Ejemplo n.º 5
0
 private String getPrefix() {
   final Class<? extends Intention> aClass = getClass();
   final String name = aClass.getSimpleName();
   final StringBuilder buffer = new StringBuilder(name.length() + 10);
   buffer.append(Character.toLowerCase(name.charAt(0)));
   for (int i = 1; i < name.length(); i++) {
     final char c = name.charAt(i);
     if (Character.isUpperCase(c)) {
       buffer.append('.');
       buffer.append(Character.toLowerCase(c));
     } else {
       buffer.append(c);
     }
   }
   return buffer.toString();
 }
 protected static LocalInspectionTool[] createLocalInspectionTools(
     final InspectionToolProvider... provider) {
   final ArrayList<LocalInspectionTool> result = new ArrayList<LocalInspectionTool>();
   for (InspectionToolProvider toolProvider : provider) {
     for (Class aClass : toolProvider.getInspectionClasses()) {
       try {
         final Object tool = aClass.newInstance();
         assertTrue(tool instanceof LocalInspectionTool);
         result.add((LocalInspectionTool) tool);
       } catch (Exception e) {
         LOG.error(e);
       }
     }
   }
   return result.toArray(new LocalInspectionTool[result.size()]);
 }
 @Nullable
 protected <T> T findChildByClass(Class<T> aClass) {
   for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) {
     if (aClass.isInstance(cur)) return (T) cur;
   }
   return null;
 }
Ejemplo n.º 8
0
 @Nullable
 public <T> T findChildByClass(Class<T> aClass) {
   for (PsiElement child : getChildren()) {
     if (aClass.isInstance(child)) return (T) child;
   }
   return null;
 }
 static {
   try {
     final Class<MyData> flavorClass = MyData.class;
     final Thread currentThread = Thread.currentThread();
     final ClassLoader currentLoader = currentThread.getContextClassLoader();
     try {
       currentThread.setContextClassLoader(flavorClass.getClassLoader());
       ourDataFlavor =
           new DataFlavor(
               DataFlavor.javaJVMLocalObjectMimeType + ";class=" + flavorClass.getName());
     } finally {
       currentThread.setContextClassLoader(currentLoader);
     }
   } catch (ClassNotFoundException e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 10
0
 @NotNull
 public <T> T[] findChildrenByClass(Class<T> aClass) {
   List<T> result = new ArrayList<T>();
   for (PsiElement child : getChildren()) {
     if (aClass.isInstance(child)) result.add((T) child);
   }
   return result.toArray((T[]) Array.newInstance(aClass, result.size()));
 }
 @NotNull
 protected <T> T[] findChildrenByClass(Class<T> aClass) {
   List<T> result = new ArrayList<T>();
   for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) {
     if (aClass.isInstance(cur)) result.add((T) cur);
   }
   return result.toArray((T[]) Array.newInstance(aClass, result.size()));
 }
Ejemplo n.º 12
0
  private void resetClassFields(final Class<?> aClass) {
    try {
      UsefulTestCase.clearDeclaredFields(this, aClass);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }

    if (aClass == LightPlatformTestCase.class) return;
    resetClassFields(aClass.getSuperclass());
  }
 @Nullable
 public FindUsagesHandler getNewFindUsagesHandler(
     @NotNull PsiElement element, final boolean forHighlightUsages) {
   for (FindUsagesHandlerFactory factory :
       Extensions.getExtensions(FindUsagesHandlerFactory.EP_NAME, myProject)) {
     if (factory.canFindUsages(element)) {
       Class<? extends FindUsagesHandlerFactory> aClass = factory.getClass();
       FindUsagesHandlerFactory copy =
           (FindUsagesHandlerFactory)
               new ConstructorInjectionComponentAdapter(aClass.getName(), aClass)
                   .getComponentInstance(myProject.getPicoContainer());
       final FindUsagesHandler handler = copy.createFindUsagesHandler(element, forHighlightUsages);
       if (handler == FindUsagesHandler.NULL_HANDLER) return null;
       if (handler != null) {
         return handler;
       }
     }
   }
   return null;
 }
 @NotNull
 @Override
 public PsiElement getNavigationElement() {
   if (myAntClass != null) {
     final PsiClass psiClass =
         JavaPsiFacade.getInstance(getProject())
             .findClass(myAntClass.getName(), myPlace.getResolveScope());
     if (psiClass != null) {
       return psiClass;
     }
   }
   return this;
 }
 private <T> List<T> getSelectedElements(Class<T> klass) {
   final Object[] elements = getSelectedNodeElements();
   ArrayList<T> result = new ArrayList<T>();
   if (elements == null) {
     return result;
   }
   for (Object element : elements) {
     if (element == null) continue;
     if (klass.isAssignableFrom(element.getClass())) {
       result.add((T) element);
     }
   }
   return result;
 }