/** * Filters the classes to be transformed. * * @param definition the definition * @param classMetaData the meta-data for the class * @param cg the class to filter * @return boolean true if the method should be filtered away */ private boolean classFilter( final AspectWerkzDefinition definition, final ClassMetaData classMetaData, final ClassGen cg) { if (cg.isInterface() || TransformationUtil.hasSuperClass( classMetaData, "org.codehaus.aspectwerkz.attribdef.aspect.Aspect") || TransformationUtil.hasSuperClass( classMetaData, "org.codehaus.aspectwerkz.xmldef.advice.AroundAdvice") || TransformationUtil.hasSuperClass( classMetaData, "org.codehaus.aspectwerkz.xmldef.advice.PreAdvice") || TransformationUtil.hasSuperClass( classMetaData, "org.codehaus.aspectwerkz.xmldef.advice.PostAdvice")) { return true; } String className = cg.getClassName(); if (definition.inExcludePackage(className)) { return true; } if (!definition.inIncludePackage(className)) { return true; } if (definition.hasExecutionPointcut(classMetaData) || definition.hasThrowsPointcut(classMetaData)) { return false; } return true; }
/** * Transforms the call side pointcuts. * * @param context the transformation context * @param klass the class set. */ public void transform(final Context context, final Klass klass) throws NotFoundException, CannotCompileException { List definitions = SystemDefinitionContainer.getDefinitionsContext(); m_joinPointIndex = TransformationUtil.getJoinPointIndex(klass.getCtClass()); // TODO thread safe reentrant for (Iterator it = definitions.iterator(); it.hasNext(); ) { final SystemDefinition definition = (SystemDefinition) it.next(); final CtClass ctClass = klass.getCtClass(); final ClassMetaData classMetaData = context.getMetaDataMaker().createClassMetaData(ctClass); // filter caller classes if (classFilter(definition, classMetaData, ctClass)) { continue; } ctClass.instrument( new ExprEditor() { public void edit(MethodCall methodCall) throws CannotCompileException { try { CtBehavior where = null; try { where = methodCall.where(); } catch (RuntimeException e) { // <clinit> access leads to a bug in Javassist where = ctClass.getClassInitializer(); } // filter caller methods if (methodFilterCaller(where)) { return; } // get the callee method name, signature and class name CtMethod calleeMethod = methodCall.getMethod(); String calleeClassName = methodCall.getClassName(); // filter callee classes if (!definition.inIncludePackage(calleeClassName)) { return; } // filter callee methods if (methodFilterCallee(calleeMethod)) { return; } // create the class meta-data ClassMetaData calleeSideClassMetaData; try { calleeSideClassMetaData = context .getMetaDataMaker() .createClassMetaData(context.getClassPool().get(calleeClassName)); } catch (NotFoundException e) { // TODO - AV - 20040507 small fix for test.aopc. that use on the fly generated // classes if (calleeClassName.equals(ctClass.getName())) { calleeSideClassMetaData = classMetaData; } else { throw new WrappedRuntimeException(e); } } // create the method meta-data MethodMetaData calleeSideMethodMetaData = JavassistMetaDataMaker.createMethodMetaData(methodCall.getMethod()); // is this a caller side method pointcut? if (definition.isPickedOutByCallPointcut( calleeSideClassMetaData, calleeSideMethodMetaData)) { // // TODO: should this caller data be passed to the // join point? It is possible. // String callerMethodName = callerBehaviour.getName(); // String callerMethodSignature = // callerBehaviour.getSignature(); // CtClass[] callerMethodParameterTypes = // callerBehaviour.getParameterTypes(); // int callerMethodModifiers = // callerBehaviour.getModifiers(); // check the callee class is not the same as target class, if that is the case // then we have have class loaded and set in the ___AW_clazz already String declaringClassMethodName = TransformationUtil.STATIC_CLASS_FIELD; CtMethod method = methodCall.getMethod(); CtClass declaringClass = method.getDeclaringClass(); if (!declaringClass .getName() .replace('/', '.') .equals(where.getDeclaringClass().getName().replace('/', '.'))) { declaringClassMethodName = addCalleeMethodDeclaringClassField(ctClass, method); } // call the wrapper method instead of the callee method StringBuffer body = new StringBuffer(); StringBuffer callBody = new StringBuffer(); callBody.append(TransformationUtil.JOIN_POINT_MANAGER_FIELD); callBody.append('.'); callBody.append(TransformationUtil.PROCEED_WITH_CALL_JOIN_POINT_METHOD); callBody.append('('); callBody.append(TransformationUtil.calculateHash(method)); callBody.append(','); callBody.append(m_joinPointIndex); callBody.append(", args"); callBody.append(", $0, declaringClassMethodName, "); callBody.append(TransformationUtil.JOIN_POINT_TYPE_METHOD_CALL); callBody.append(");"); body.append('{'); if (method.getParameterTypes().length > 0) { body.append("Object[] args = $args; "); } else { body.append("Object[] args = null; "); } body.append("Class declaringClassMethodName = "); body.append(declaringClassMethodName); body.append("; "); if (methodCall.getMethod().getReturnType() == CtClass.voidType) { body.append("$_ = ").append(callBody.toString()).append("}"); } else if (!methodCall.getMethod().getReturnType().isPrimitive()) { body.append("$_ = ($r)"); body.append(callBody.toString()); body.append("}"); } else { String localResult = TransformationUtil.ASPECTWERKZ_PREFIX + "res"; body.append("Object ").append(localResult).append(" = "); body.append(callBody.toString()); body.append("if (").append(localResult).append(" != null)"); body.append("$_ = ($r) ").append(localResult).append("; else "); body.append("$_ = "); body.append( JavassistHelper.getDefaultPrimitiveValue( methodCall.getMethod().getReturnType())); body.append("; }"); } methodCall.replace(body.toString()); context.markAsAdvised(); m_joinPointIndex++; } } catch (NotFoundException nfe) { nfe.printStackTrace(); // TODO: should we swallow this exception? } } }); } TransformationUtil.setJoinPointIndex(klass.getCtClass(), m_joinPointIndex); }
private void deployNode( InstanceSpecification mainInstance, Model existingModel, Model tmpModel, InstanceSpecification newRootIS, EList<InstanceSpecification> nodes, int nodeIndex, InstanceSpecification node) throws TransformationException, InterruptedException { ModelManagement genModelManagement = createTargetModel(existingModel, MapUtil.rootModelName, false); Model generatedModel = genModelManagement.getModel(); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- // new model has name "root" and contains a package with the // existing model // Package originalRoot = genModel.createNestedPackage // (existingModel.getName ()); LazyCopier targetCopy = new LazyCopier(tmpModel, generatedModel, true, true); // TODO: distribution to nodes is currently not done. How // can it be realized with a copy filter ? targetCopy.preCopyListeners.add(FilterStateMachines.getInstance()); targetCopy.preCopyListeners.add(FilterRuleApplication.getInstance()); monitor.setTaskName( String.format(Messages.InstantiateDepPlan_InfoDeployingForNode, node.getName())); ILangSupport langSupport = configureLanguageSupport(mainInstance, existingModel, node); if (langSupport == null) { return; } Deploy deployment = new Deploy(targetCopy, langSupport, node, nodeIndex, nodes.size()); InstanceSpecification nodeRootIS = deployment.distributeToNode(newRootIS); TransformationUtil.updateDerivedInterfaces(nodeRootIS); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- removeDerivedInterfacesInRoot(generatedModel); CompImplTrafos.transform( deployment.getBootloader(), targetCopy, generatedModel, modelIsObjectOriented); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- destroyDeploymentPlanFolder(generatedModel); if (generateCode) { GenerateCode codeGenerator = new GenerateCode(langSupport.getProject(), langSupport, genModelManagement, monitor); boolean option = (generationOptions & GenerationOptions.ONLY_CHANGED) != 0; codeGenerator.generate(node, getTargetLanguage(mainInstance), option); } genModelManagement.dispose(); }
protected void executeTransformation() throws Exception { modelIsObjectOriented = true; ModelManagement intermediateModelManagement = null; InstanceSpecification mainInstance = DepUtils.getMainInstance(srcModelComponentDeploymentPlan); EList<InstanceSpecification> nodes = AllocUtils.getAllNodes(mainInstance); initiateProgressMonitor(generateCode, nodes); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- // 1a: create a new model (and applies same profiles / imports) Model existingModel = srcModelComponentDeploymentPlan.getModel(); TransformationContext.sourceRoot = existingModel; intermediateModelManagement = createTargetModel(existingModel, existingModel.getName(), true); // Declare that the new model is a derivedElement (kind of hack, // since the source element (attribute of derive element) remains // undefined). This is used to deactivate automatic transformations // that should not be applied to the generated model. // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- // get the temporary model Model intermediateModel = intermediateModelManagement.getModel(); // create a package for global enumerations that are used by Acceleo code EnumService.createEnumPackage(intermediateModel); // create a lazy copier towards the intermediate model LazyCopier intermediateModelCopier = new LazyCopier(existingModel, intermediateModel, false, true); // add pre-copy and post-copy listeners to the copier intermediateModelCopier.preCopyListeners.add(FilterTemplate.getInstance()); // 1b: reify the connectors "into" the new model monitor.subTask(Messages.InstantiateDepPlan_InfoExpandingConnectors); // obtain the component deployment plan in target model Package intermediateModelComponentDeploymentPlan = (Package) intermediateModelCopier.shallowCopy(srcModelComponentDeploymentPlan); intermediateModelCopier.createShallowContainer(srcModelComponentDeploymentPlan); AbstractContainerTrafo.init(); InstanceConfigurator.onNodeModel = false; MainModelTrafo mainModelTrafo = new MainModelTrafo(intermediateModelCopier, intermediateModelComponentDeploymentPlan); InstanceSpecification newMainInstance = mainModelTrafo.transformInstance(mainInstance, null); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- TransformationUtil.applyInstanceConfigurators(newMainInstance); FlattenInteractionComponents.getInstance().flattenAssembly(newMainInstance, null); TransformationUtil.propagateAllocation(newMainInstance); intermediateModelManagement.saveModel(project, TEMP_MODEL_FOLDER, TEMP_MODEL_POSTFIX); // -------------------------------------------------------------------- checkProgressStatus(); // -------------------------------------------------------------------- if (!generateCACOnly) { deployOnNodes(mainInstance, existingModel, intermediateModel, newMainInstance); } intermediateModelManagement.dispose(); if (AcceleoDriver.hasErrors()) { throw new AcceleoException(); } }