@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;
 }
Exemplo n.º 2
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]);
      }
    }
  }
Exemplo n.º 3
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);
      }
    }
  }
 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);
   }
 }
 @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;
 }