Exemplo n.º 1
0
  @Test
  public void testAnalysis() {
    AnalysisContext analysisContext = simpleAnalysisContextFactory.buildContext();
    analysisContext.getAOPRepository().put(aopAnnotationASTType, methodInterceptorASTType);

    for (ASTMethod astMethod : proxyTargetASTType.getMethods()) {
      proxyAnalyzer.analyzeMethod(
          proxyTargetInjectionNode, proxyTargetASTType, astMethod, analysisContext);
    }

    assertTrue(proxyTargetInjectionNode.containsAspect(AOPProxyAspect.class));
    AOPProxyAspect aspect = proxyTargetInjectionNode.getAspect(AOPProxyAspect.class);
    for (ASTMethod astMethod : proxyTargetASTType.getMethods()) {
      assertTrue(aspect.getMethodInterceptors().containsKey(astMethod));
      Set<InjectionNode> interceptorInjectionNodes = aspect.getMethodInterceptors().get(astMethod);
      for (InjectionNode interceptorInjectionNode : interceptorInjectionNodes) {
        assertEquals(methodInterceptorASTType, interceptorInjectionNode.getASTType());
      }
    }
  }
Exemplo n.º 2
0
  @Override
  public void generate(ASTType descriptor) {

    if (descriptor.isConcreteClass()) {
      throw new TransfuseAnalysisException("Unable to build injector from concrete class");
    }

    try {
      JDefinedClass implClass =
          codeModel._class(JMod.PUBLIC, descriptor.getName() + IMPL_EXT, ClassType.CLASS);
      JClass interfaceClass = codeModel.ref(descriptor.getName());

      implClass._implements(interfaceClass);

      for (ASTMethod interfaceMethod : descriptor.getMethods()) {
        MirroredMethodGenerator mirroredMethodGenerator =
            componentBuilderFactory.buildMirroredMethodGenerator(interfaceMethod, false);
        MethodDescriptor methodDescriptor = mirroredMethodGenerator.buildMethod(implClass);
        JBlock block = methodDescriptor.getMethod().body();

        AnalysisContext context =
            analysisContextFactory.buildAnalysisContext(injectionNodeBuilderRepository);
        InjectionNodeFactory injectionNodeFactory =
            componentBuilderFactory.buildInjectionNodeFactory(
                interfaceMethod.getReturnType(), context);

        // Injections
        InjectionNode returnType = injectionNodeFactory.buildInjectionNode(methodDescriptor);
        Map<InjectionNode, TypedExpression> expressionMap =
            injectionFragmentGenerator.buildFragment(block, implClass, returnType);

        block._return(expressionMap.get(returnType).getExpression());
      }

      injectorRepositoryGenerator.generateInjectorRepository(interfaceClass, implClass);

    } catch (JClassAlreadyExistsException e) {
      throw new TransfuseAnalysisException(
          "Class already exists for generated type " + descriptor.getName(), e);
    } catch (ClassNotFoundException e) {
      throw new TransfuseAnalysisException("Target class not found", e);
    }
  }