List of usage examples for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY
ChildListPropertyDescriptor STATEMENTS_PROPERTY
To view the source code for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY.
Click Source Link
From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.impl.PathWithUncertaintyTestStrategy.java
License:Open Source License
/** * /*from w w w . ja v a 2 s. co m*/ * @param state - the state for which we inspect the transitions and generate the test plan * @param rewrite - rewriter used to modify the generated code * @param planMethodDeclaration - the method in which the code must be added * @param parrentPlanBlock - the block of code where the new state code must be added. Can be a method body, the body of an If statement, etc * @param pathTransitions - used to avoid testing cycles and ensure test plan keeps uniqueness on transitions */ private void generatePlanForState(final StateMachineState state, final ASTRewrite rewrite, final MethodDeclaration planMethodDeclaration, final Set<StateMachineStateTransition> pathTransitions) { AST ast = planMethodDeclaration.getAST(); Block parrentPlanBlock = planMethodDeclaration.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(parrentPlanBlock, Block.STATEMENTS_PROPERTY); /** * First we create an abstract method to assert that we have reached current state and we call it * only if the state is not a choice. Choices are "virtual" states that signal splits in transition. * */ { //only create method if not previously created String stateName = state.getName(); String methodName = ASSERT_STATE_LEADING + stateName; if (!generatedAbstractMethods.containsKey(stateName)) { MethodDeclaration method = createAbstractMethodForState(state, planMethodDeclaration.getAST()); generatedAbstractMethods.put(stateName, method); } /** * Call the assert state method to check if we have reached the current state. * For the initial state this assert can also reset the system to initial state. */ { //invoke guard as Assert statement AssertStatement assertStatement = ast.newAssertStatement(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(methodName)); assertStatement.setExpression(invocation); parrentPlanBlock.statements().add(assertStatement); // listRewrite.insertFirst(rewrite.createStringPlaceholder("//Call the assert state method to check if we have reached the current state.", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("//For the initial state this assert can also reset the system to initial state.", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(assertStatement, null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); } } /** * If from one state we have multiple triggers, or from a choice we can go to multiple classes * then we generate for each of these transitions paths a separate test plan. * This means we clone the previous method into how many we need */ List<StateMachineStateTransition> transitions = state.getOutTransitions(); //if 1 transition, then we add to same plan //if more, we need separate test plans for each branching if (transitions.isEmpty() && !(state.getVertex() instanceof FinalState)) { //notify user that something is wrong with the model we are converting notifyUser("State \"" + state.getName() + "\"is not final and does not have any transitions. All state machine flows must reach a FinalState."); System.err.println(state.getName() + " is not final and does not have any transitions. All state machine flows must reach a FinalState to be converted in test plans."); } else if (transitions.size() == 1) { StateMachineStateTransition transition = transitions.get(0); //if we have visited this transition, continue if (pathTransitions.contains(transition)) { return; } else { // add transition to visited transitions pathTransitions.add(transition); } // listRewrite.insertLast(rewrite.createStringPlaceholder("//Test transition " + transition.getTransition().getName(), ASTNode.EMPTY_STATEMENT), null); /** * Must assert before any transition that the guard condition is fulfilled */ { //get transition condition (could also be Rule, currently we get only Guard transitions) Constraint guard = transition.getTransition().getGuard(); if (guard != null) { for (Element element : guard.allOwnedElements()) { //currently condition retrieved as plain text that will need to be parsed and evaluated OpaqueExpression expression = (OpaqueExpression) element; for (String body : expression.getBodies()) { if (body.isEmpty()) { notifyUser("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); System.err.println( "Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); continue; } MethodDeclaration method = createAbstractMethodForGuard(body, ast); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke guard as Assert statement AssertStatement assertStatement = ast.newAssertStatement(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); assertStatement.setExpression(invocation); parrentPlanBlock.statements().add(assertStatement); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Assert guard condition for next transition is true", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(assertStatement, null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); } } } } //get all transition triggers List<Trigger> triggers = transition.getTransition().getTriggers(); //for each trigger for (Trigger trigger : triggers) { /** * If we have not created it already, we create an abstract method to invoke the trigger */ { //TODO: update so we do not generate the trigger if it was already generated MethodDeclaration method = createAbstractTriggerInvocation(trigger, planMethodDeclaration.getAST()); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke trigger MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke transition trigger", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } if (!(state.getVertex() instanceof FinalState)) { //continue from target state with plan generation StateMachineState targetState = transition.getTargetState(); generatePlanForState(targetState, rewrite, planMethodDeclaration, pathTransitions); } else { if (transition.getTargetState() == null) { notifyUser(state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); System.err.println( state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); } } } else if (transitions.size() > 1) { for (StateMachineStateTransition transition : transitions) { //clone transitions to use clean path for each sub-trees Set<StateMachineStateTransition> transitionsCopy = new HashSet<>(); transitionsCopy.addAll(pathTransitions); //if we have visited this transition, continue if (transitionsCopy.contains(transition)) { continue; } else { // add transition to visited transitions transitionsCopy.add(transition); } //for each transition we do a clone of the plan until now MethodDeclaration transitionMethod = cloneMethodDeclaration(planMethodDeclaration); transitionMethod.setName(ast .newSimpleName(PLAN_METHOD_LEADING + (UUID.randomUUID().toString().replaceAll("\\W", "")))); //shadowing to local parrentPlanBlock parrentPlanBlock = transitionMethod.getBody(); //shadowing to local ListRewrite // listRewrite = rewrite.getListRewrite(transitionMethod.getBody(), Block.STATEMENTS_PROPERTY); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Forcing transition " + transition.getTransition().getName() + " by ensuring guard conditions are met and triggers are invoked.", ASTNode.EMPTY_STATEMENT), null); /** * Must force-set all guard conditions to navigate to this particular execution branch */ { //get transition condition (could also be Rule, currently we get only Guard transitions) //force for the current test the transition condition to true, to enable the system to navigate to expected state Constraint guard = transition.getTransition().getGuard(); if (guard != null) { for (Element element : guard.allOwnedElements()) { //currently condition retrieved as plain text that will need to be parsed and evaluated OpaqueExpression expression = (OpaqueExpression) element; for (String body : expression.getBodies()) { if (body.isEmpty()) { notifyUser("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); System.err.println("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); continue; } MethodDeclaration method = createAbstractForceConditionMethod(body, ast); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke method to force guard condition to true MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke method to force guard condition to true: " + body, ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } } } //get all transition triggers and execute them, like if we had only one transition List<Trigger> triggers = transition.getTransition().getTriggers(); //for each trigger for (Trigger trigger : triggers) { /** * If we have not created it already, we create an abstract method to invoke the trigger */ { //TODO: update so we do not generate the trigger if it was already generated MethodDeclaration method = createAbstractTriggerInvocation(trigger, transitionMethod.getAST()); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke trigger MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke transition trigger", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } if (!(state.getVertex() instanceof FinalState)) { //continue from target state with plan generation StateMachineState targetState = transition.getTargetState(); generatePlanForState(targetState, rewrite, transitionMethod, transitionsCopy); } else { if (transition.getTargetState() == null) { notifyUser(state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); System.err.println( state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); } } } } if (state.getVertex() instanceof FinalState) { //store generated method in methods //check and store only if there is at least one transition with an uncertain state boolean hasUncertainty = false; for (StateMachineStateTransition transition : pathTransitions) { //TODO: remove constant and make this efficient //check for all transitions only initial state for uncertainties //as for next transition, the initial will be the target of this one (except for final state) for (Stereotype stereotype : transition.getSourceState().getVertex().getAppliedStereotypes()) { //check if the applied stereotype is InfrastructureLevelUncertainty if (stereotype.getName().equals(INFRASTRUCTURE_UNCERTAINTY_NAME)) { hasUncertainty = true; break; } } } //if does not have any uncertainty on it, do not add it to generated plans if (hasUncertainty) { generatedPlans.put(planMethodDeclaration.getName().toString(), planMethodDeclaration); } } }
From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.impl.TransitionCorrectnessTestStrategy.java
License:Open Source License
/** * /*from ww w .j ava 2 s . c o m*/ * @param state - the state for which we inspect the transitions and generate the test plan * @param rewrite - rewriter used to modify the generated code * @param planMethodDeclaration - the method in which the code must be added * @param parrentPlanBlock - the block of code where the new state code must be added. Can be a method body, the body of an If statement, etc * @param pathTransitions - used to avoid testing cycles and ensure test plan keeps uniqueness on transitions */ private void generatePlanForState(final StateMachineState state, final ASTRewrite rewrite, final MethodDeclaration planMethodDeclaration, final Set<StateMachineStateTransition> pathTransitions) { AST ast = planMethodDeclaration.getAST(); Block parrentPlanBlock = planMethodDeclaration.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(parrentPlanBlock, Block.STATEMENTS_PROPERTY); /** * First we create an abstract method to assert that we have reached current state and we call it * only if the state is not a choice. Choices are "virtual" states that signal splits in transition. * */ { //only create method if not previously created String stateName = state.getName(); String methodName = ASSERT_STATE_LEADING + stateName; if (!generatedAbstractMethods.containsKey(stateName)) { MethodDeclaration method = createAbstractMethodForState(state, planMethodDeclaration.getAST()); generatedAbstractMethods.put(stateName, method); } /** * Call the assert state method to check if we have reached the current state. * For the initial state this assert can also reset the system to initial state. */ { //invoke guard as Assert statement AssertStatement assertStatement = ast.newAssertStatement(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(methodName)); assertStatement.setExpression(invocation); parrentPlanBlock.statements().add(assertStatement); // listRewrite.insertFirst(rewrite.createStringPlaceholder("//Call the assert state method to check if we have reached the current state.", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("//For the initial state this assert can also reset the system to initial state.", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(assertStatement, null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); } } /** * If from one state we have multiple triggers, or from a choice we can go to multiple classes * then we generate for each of these transitions paths a separate test plan. * This means we clone the previous method into how many we need */ List<StateMachineStateTransition> transitions = state.getOutTransitions(); //if 1 transition, then we add to same plan //if more, we need separate test plans for each branching if (transitions.isEmpty() && !(state.getVertex() instanceof FinalState)) { //notify user that something is wrong with the model we are converting notifyUser("State \"" + state.getName() + "\"is not final and does not have any transitions. All state machine flows must reach a FinalState."); System.err.println(state.getName() + " is not final and does not have any transitions. All state machine flows must reach a FinalState to be converted in test plans."); } else if (transitions.size() == 1) { StateMachineStateTransition transition = transitions.get(0); //if we have visited this transition, continue if (pathTransitions.contains(transition)) { return; } else { // add transition to visited transitions pathTransitions.add(transition); } // listRewrite.insertLast(rewrite.createStringPlaceholder("//Test transition " + transition.getTransition().getName(), ASTNode.EMPTY_STATEMENT), null); /** * Must assert before any transition that the guard condition is fulfilled */ { //get transition condition (could also be Rule, currently we get only Guard transitions) Constraint guard = transition.getTransition().getGuard(); if (guard != null) { for (Element element : guard.allOwnedElements()) { //currently condition retrieved as plain text that will need to be parsed and evaluated OpaqueExpression expression = (OpaqueExpression) element; for (String body : expression.getBodies()) { if (body.isEmpty()) { notifyUser("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); System.err.println( "Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); continue; } MethodDeclaration method = createAbstractMethodForGuard(body, ast); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke guard as Assert statement AssertStatement assertStatement = ast.newAssertStatement(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); assertStatement.setExpression(invocation); parrentPlanBlock.statements().add(assertStatement); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Assert guard condition for next transition is true", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(assertStatement, null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); } } } } //get all transition triggers List<Trigger> triggers = transition.getTransition().getTriggers(); //for each trigger for (Trigger trigger : triggers) { /** * If we have not created it already, we create an abstract method to invoke the trigger */ { //TODO: update so we do not generate the trigger if it was already generated MethodDeclaration method = createAbstractTriggerInvocation(trigger, planMethodDeclaration.getAST()); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke trigger MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke transition trigger", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } if (!(state.getVertex() instanceof FinalState)) { //continue from target state with plan generation StateMachineState targetState = transition.getTargetState(); generatePlanForState(targetState, rewrite, planMethodDeclaration, pathTransitions); } else { if (transition.getTargetState() == null) { notifyUser(state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); System.err.println( state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); } } } else if (transitions.size() > 1) { for (StateMachineStateTransition transition : transitions) { //clone transitions to use clean path for each sub-trees //cloning is done here as we are generating different paths for each transition at this point Set<StateMachineStateTransition> transitionsCopy = new HashSet<>(); transitionsCopy.addAll(pathTransitions); //if we have visited this transition, continue if (transitionsCopy.contains(transition)) { continue; } else { // add transition to visited transitions transitionsCopy.add(transition); } //for each transition we do a clone of the plan until now MethodDeclaration transitionMethod = cloneMethodDeclaration(planMethodDeclaration); transitionMethod.setName(ast .newSimpleName(PLAN_METHOD_LEADING + (UUID.randomUUID().toString().replaceAll("\\W", "")))); //shadowing to local parrentPlanBlock parrentPlanBlock = transitionMethod.getBody(); //shadowing to local ListRewrite // listRewrite = rewrite.getListRewrite(transitionMethod.getBody(), Block.STATEMENTS_PROPERTY); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Forcing transition " + transition.getTransition().getName() + " by ensuring guard conditions are met and triggers are invoked.", ASTNode.EMPTY_STATEMENT), null); /** * Must force-set all guard conditions to navigate to this particular execution branch */ { //get transition condition (could also be Rule, currently we get only Guard transitions) //force for the current test the transition condition to true, to enable the system to navigate to expected state Constraint guard = transition.getTransition().getGuard(); if (guard != null) { for (Element element : guard.allOwnedElements()) { //currently condition retrieved as plain text that will need to be parsed and evaluated OpaqueExpression expression = (OpaqueExpression) element; for (String body : expression.getBodies()) { if (body.isEmpty()) { notifyUser("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); System.err.println("Guard condition for transition " + transition.getTransition().getName() + " from state " + state.getName() + " is empty"); continue; } MethodDeclaration method = createAbstractForceConditionMethod(body, ast); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke method to force guard condition to true MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke method to force guard condition to true: " + body, ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } } } //get all transition triggers and execute them, like if we had only one transition List<Trigger> triggers = transition.getTransition().getTriggers(); //for each trigger for (Trigger trigger : triggers) { /** * If we have not created it already, we create an abstract method to invoke the trigger */ { //TODO: update so we do not generate the trigger if it was already generated MethodDeclaration method = createAbstractTriggerInvocation(trigger, transitionMethod.getAST()); if (!generatedAbstractMethods.containsKey(method.getName().toString())) { generatedAbstractMethods.put(method.getName().toString(), method); } //invoke trigger MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(method.getName().toString())); // listRewrite.insertLast(rewrite.createStringPlaceholder("//Invoke transition trigger", ASTNode.EMPTY_STATEMENT), null); // listRewrite.insertLast(ast.newExpressionStatement(invocation), null); // listRewrite.insertLast(rewrite.createStringPlaceholder("", ASTNode.EMPTY_STATEMENT), null); parrentPlanBlock.statements().add(ast.newExpressionStatement(invocation)); } } if (!(state.getVertex() instanceof FinalState)) { //continue from target state with plan generation StateMachineState targetState = transition.getTargetState(); generatePlanForState(targetState, rewrite, transitionMethod, transitionsCopy); } else { if (transition.getTargetState() == null) { notifyUser(state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); System.err.println( state.getName() + " is not final and does not have a target state on transition " + transition.getTransition().getName()); } } } } if (state.getVertex() instanceof FinalState) { //store generated method in methods generatedPlans.put(planMethodDeclaration.getName().toString(), planMethodDeclaration); } }
From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseUtilityClassQuickFix.java
License:Open Source License
@SuppressWarnings("unchecked") private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) { final AST ast = typeDeclaration.getAST(); final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class); constructor.setConstructor(true);/*from w w w. j av a 2 s . com*/ final Modifier modifier = (Modifier) ast.createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD); constructor.modifiers().add(modifier); constructor.setName(ASTUtil.copy(typeDeclaration.getName())); final Block body = (Block) ast.createInstance(Block.class); constructor.setBody(body); final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY); final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT); statementRewrite.insertFirst(comment, null); final int position = findConstructorPosition(typeDeclaration); final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); bodyDeclarationRewrite.insertAt(constructor, position, null); }
From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java
License:Apache License
/** * TODO extract this possibly, but I think I'll wait till I get into slots * before I do it see what is common./*from w ww . j a v a 2 s .c o m*/ */ private void createPresenterGinlink(ICompilationUnit unit, String modulePackageName, String moduleName, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { unit.createImport(modulePackageName + "." + moduleName, null, monitor); Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); // creation of ASTRewrite ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); // find the configure method MethodDeclaration method = findMethod(astRoot, "configure"); if (method == null) { logger.severe("createPresenterGinLink() unit did not have configure implementation."); return; } // presenter configure method install(new Module()); String installModuleStatement = "install(new " + moduleName + "());"; Block block = method.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); ASTNode placeHolder = rewrite.createStringPlaceholder(installModuleStatement, ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(placeHolder, null); // computation of the text edits TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); // computation of the new source code edits.apply(document); String newSource = document.get(); // update of the compilation unit and save it IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }
From source file:com.motorola.studio.android.model.java.ActivityClass.java
License:Apache License
@Override protected void addComments() throws AndroidException { ASTNode todoComment;/*www. j a va 2s .c o m*/ ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); compUnit = (CompilationUnit) parser.createAST(null); ast = compUnit.getAST(); rewrite = ASTRewrite.create(ast); todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere, ASTNode.EMPTY_STATEMENT); TypeDeclaration activityClass = (TypeDeclaration) compUnit.types().get(0); MethodDeclaration method; Block block; // Adds the Override annotation and ToDo comment to all overridden methods for (int i = 0; i < activityClass.bodyDeclarations().size(); i++) { method = (MethodDeclaration) activityClass.bodyDeclarations().get(i); // Adds the Override annotation rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null); // Adds the ToDo comment block = method.getBody(); rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(todoComment, null); } try { // Writes the modifications TextEdit modifications = rewrite.rewriteAST(document, null); modifications.apply(document); } catch (IllegalArgumentException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ActivityClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ActivityClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ActivityClass.class, errMsg, e); throw new AndroidException(errMsg); } }
From source file:com.motorola.studio.android.model.java.BroadcastReceiverClass.java
License:Apache License
@Override protected void addComments() throws AndroidException { ASTNode todoComment;/* w w w . java2 s.com*/ ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); compUnit = (CompilationUnit) parser.createAST(null); ast = compUnit.getAST(); rewrite = ASTRewrite.create(ast); todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere, ASTNode.EMPTY_STATEMENT); TypeDeclaration receiverClass = (TypeDeclaration) compUnit.types().get(0); MethodDeclaration method; Block block; // Adds the Override annotation and ToDo comment to all overridden methods for (int i = 0; i < receiverClass.bodyDeclarations().size(); i++) { method = (MethodDeclaration) receiverClass.bodyDeclarations().get(i); // Adds the Override annotation rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null); // Adds the ToDo comment block = method.getBody(); rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null); } try { // Writes the modifications TextEdit modifications = rewrite.rewriteAST(document, null); modifications.apply(document); } catch (IllegalArgumentException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } }
From source file:com.motorola.studio.android.model.java.ContentProviderClass.java
License:Apache License
@Override protected void addComments() throws AndroidException { ASTNode todoComment;// ww w . j ava2 s. c om ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); compUnit = (CompilationUnit) parser.createAST(null); ast = compUnit.getAST(); rewrite = ASTRewrite.create(ast); todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere, ASTNode.EMPTY_STATEMENT); TypeDeclaration cpClass = (TypeDeclaration) compUnit.types().get(0); MethodDeclaration method; Block block; // Adds the Override annotation and ToDo comment to all abstract methods for (int i = 0; i < cpClass.bodyDeclarations().size(); i++) { BodyDeclaration bodyDecl = (BodyDeclaration) cpClass.bodyDeclarations().get(i); if (bodyDecl instanceof MethodDeclaration) { method = (MethodDeclaration) bodyDecl; // Adds the Override annotation rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null); // Adds the ToDo comment block = method.getBody(); rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null); } } try { // Writes the modifications TextEdit modifications = rewrite.rewriteAST(document, null); modifications.apply(document); } catch (IllegalArgumentException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ContentProviderClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ContentProviderClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ContentProviderClass.class, errMsg, e); throw new AndroidException(errMsg); } }
From source file:com.motorola.studio.android.model.java.ServiceClass.java
License:Apache License
@Override protected void addComments() throws AndroidException { ASTNode todoComment;//w w w . j a va2 s . c o m ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); compUnit = (CompilationUnit) parser.createAST(null); ast = compUnit.getAST(); rewrite = ASTRewrite.create(ast); todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere, ASTNode.EMPTY_STATEMENT); TypeDeclaration serviceClass = (TypeDeclaration) compUnit.types().get(0); MethodDeclaration method; Block block; // Adds the Override annotation and ToDo comment to all abstract methods for (int i = 0; i < serviceClass.bodyDeclarations().size(); i++) { method = (MethodDeclaration) serviceClass.bodyDeclarations().get(i); // Adds the Override annotation rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null); // Adds the ToDo comment block = method.getBody(); rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null); } try { // Writes the modifications TextEdit modifications = rewrite.rewriteAST(document, null); modifications.apply(document); } catch (IllegalArgumentException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ServiceClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ServiceClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(ServiceClass.class, errMsg, e); throw new AndroidException(errMsg); } }
From source file:com.motorola.studio.android.model.java.WidgetProviderClass.java
License:Apache License
@Override protected void addComments() throws AndroidException { ASTNode todoComment;/*from w w w . j av a 2 s.co m*/ ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); compUnit = (CompilationUnit) parser.createAST(null); ast = compUnit.getAST(); rewrite = ASTRewrite.create(ast); todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere, ASTNode.EMPTY_STATEMENT); TypeDeclaration widgetProviderClass = (TypeDeclaration) compUnit.types().get(0); MethodDeclaration method; Block block; // Adds the Override annotation and ToDo comment to all overridden methods for (int i = 0; i < widgetProviderClass.bodyDeclarations().size(); i++) { method = (MethodDeclaration) widgetProviderClass.bodyDeclarations().get(i); // Adds the Override annotation rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null); // Adds the ToDo comment block = method.getBody(); rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null); } try { // Writes the modifications TextEdit modifications = rewrite.rewriteAST(document, null); modifications.apply(document); } catch (IllegalArgumentException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className); StudioLogger.error(BroadcastReceiverClass.class, errMsg, e); throw new AndroidException(errMsg); } }
From source file:de.ovgu.cide.configuration.jdt.DeleteHiddenNodesVisitor.java
License:Open Source License
private void rewriteIfWhileEtc(ASTNode node, ASTNode parent, StructuralPropertyDescriptor prop, List<ASTNode> replacements) { Block replacement = node.getAST().newBlock(); rewrite.set(parent, prop, replacement, null); if (replacements.size() > 0) { ListRewrite blockRewriteList = getRewriteList(replacement, Block.STATEMENTS_PROPERTY); for (ASTNode s : replacements) for (ASTNode n : resolveBlock(s)) { blockRewriteList.insertLast(move(n), null); }//from w ww .j ava 2s . c o m } else if (prop == IfStatement.ELSE_STATEMENT_PROPERTY) rewrite.set(parent, prop, null, null); }