List of usage examples for org.eclipse.jdt.core.dom Block statements
ASTNode.NodeList statements
To view the source code for org.eclipse.jdt.core.dom Block statements.
Click Source Link
From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java
License:Open Source License
/** Create a set checkpoint method invocation for an assignment block. * * @param ast The {@link AST} object./*from ww w . ja v a 2 s . c o m*/ * @return The statement that tests whether the checkpoint objects of the * current object and the new value are the same; if not, assign the * checkpoint object of the current object to the checkpoint of the new * value. */ private Statement _createSetCheckpointInvocation(AST ast) { InfixExpression test = ast.newInfixExpression(); InfixExpression condition1 = ast.newInfixExpression(); condition1.setLeftOperand(ast.newSimpleName("newValue")); condition1.setOperator(InfixExpression.Operator.NOT_EQUALS); condition1.setRightOperand(ast.newNullLiteral()); InfixExpression condition2 = ast.newInfixExpression(); condition2.setLeftOperand(ast.newSimpleName(CHECKPOINT_NAME)); condition2.setOperator(InfixExpression.Operator.NOT_EQUALS); MethodInvocation getCheckpoint = ast.newMethodInvocation(); getCheckpoint.setExpression(ast.newSimpleName("newValue")); getCheckpoint.setName(ast.newSimpleName(_getGetCheckpointMethodName(false))); condition2.setRightOperand(getCheckpoint); test.setLeftOperand(condition1); test.setOperator(InfixExpression.Operator.CONDITIONAL_AND); test.setRightOperand(condition2); IfStatement ifStatement = ast.newIfStatement(); ifStatement.setExpression(test); Block thenBranch = ast.newBlock(); ifStatement.setThenStatement(thenBranch); MethodInvocation setCheckpoint = ast.newMethodInvocation(); setCheckpoint.setExpression(ast.newSimpleName("newValue")); setCheckpoint.setName(ast.newSimpleName(SET_CHECKPOINT_NAME)); setCheckpoint.arguments().add(ast.newSimpleName(CHECKPOINT_NAME)); thenBranch.statements().add(ast.newExpressionStatement(setCheckpoint)); return ifStatement; }
From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java
License:Open Source License
/** Create a set checkpoint method for a class, which sets its checkpoint * object.//from w w w . jav a2 s. c om * * @param ast The {@link AST} object. * @param root The root of the AST. * @param state The current state of the type analyzer. * @param isAnonymous Whether the current class is anonymous. * @param isInterface Whether the current type is an interface. * @return The declaration of the method that sets the checkpoint object. */ private MethodDeclaration _createSetCheckpointMethod(AST ast, CompilationUnit root, TypeAnalyzerState state, boolean isAnonymous, boolean isInterface) { String methodName = _getSetCheckpointMethodName(isAnonymous); Class currentClass = state.getCurrentClass(); Class parent = currentClass.getSuperclass(); // Check if the method is duplicated (possibly because the source // program is refactored twice). if (hasMethod(currentClass, methodName, new Class[] { Checkpoint.class }, true)) { throw new ASTDuplicatedMethodException(currentClass.getName(), methodName); } if (!isAnonymous && (parent != null) && (state.getCrossAnalyzedTypes().contains(parent.getName()) || hasMethod(parent, methodName, new Class[] { Checkpoint.class }))) { return null; } MethodDeclaration method = ast.newMethodDeclaration(); // Set the method name. method.setName(ast.newSimpleName(methodName)); // Add a checkpoint parameter. SingleVariableDeclaration checkpoint = ast.newSingleVariableDeclaration(); String checkpointType = getClassName(Checkpoint.class, state, root); checkpoint.setType(createType(ast, checkpointType)); checkpoint.setName(ast.newSimpleName("checkpoint")); method.parameters().add(checkpoint); // Return type is Object. method.setReturnType2(createType(ast, getClassName(Object.class, state, root))); if (!isInterface) { // The test. IfStatement test = ast.newIfStatement(); InfixExpression testExpression = ast.newInfixExpression(); testExpression.setLeftOperand(ast.newSimpleName(CHECKPOINT_NAME)); testExpression.setOperator(InfixExpression.Operator.NOT_EQUALS); testExpression.setRightOperand(ast.newSimpleName("checkpoint")); test.setExpression(testExpression); // The "then" branch of the test. Block thenBranch = ast.newBlock(); test.setThenStatement(thenBranch); Block body = ast.newBlock(); body.statements().add(test); method.setBody(body); // Backup the old checkpoint. VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName("oldCheckpoint")); fragment.setInitializer(ast.newSimpleName(CHECKPOINT_NAME)); VariableDeclarationStatement tempDeclaration = ast.newVariableDeclarationStatement(fragment); tempDeclaration.setType(createType(ast, checkpointType)); thenBranch.statements().add(tempDeclaration); // Record the old checkpoint if the new checkpoint is not null. // If it is null, it is impossible to roll back to the previous // checkpoint. IfStatement testNewCheckpoint = ast.newIfStatement(); InfixExpression testNull = ast.newInfixExpression(); testNull.setLeftOperand(ast.newSimpleName("checkpoint")); testNull.setOperator(InfixExpression.Operator.NOT_EQUALS); testNull.setRightOperand(ast.newNullLiteral()); testNewCheckpoint.setExpression(testNull); Block testNewCheckpointBody = ast.newBlock(); testNewCheckpoint.setThenStatement(testNewCheckpointBody); MethodInvocation record = ast.newMethodInvocation(); record.setExpression(ast.newSimpleName(CHECKPOINT_RECORD_NAME)); record.setName(ast.newSimpleName("add")); record.arguments().add(ast.newSimpleName(CHECKPOINT_NAME)); MethodInvocation getTimestamp = ast.newMethodInvocation(); getTimestamp.setExpression(ast.newSimpleName("checkpoint")); getTimestamp.setName(ast.newSimpleName("getTimestamp")); record.arguments().add(getTimestamp); testNewCheckpointBody.statements().add(ast.newExpressionStatement(record)); MethodInvocation pushStates = ast.newMethodInvocation(); String recordType = getClassName(FieldRecord.class, state, root); pushStates.setExpression(createName(ast, recordType)); pushStates.setName(ast.newSimpleName("pushState")); pushStates.arguments().add(ast.newSimpleName(RECORDS_NAME)); testNewCheckpointBody.statements().add(ast.newExpressionStatement(pushStates)); thenBranch.statements().add(testNewCheckpoint); // Assign the new checkpoint. Assignment assignment = ast.newAssignment(); assignment.setLeftHandSide(ast.newSimpleName(CHECKPOINT_NAME)); assignment.setRightHandSide(ast.newSimpleName("checkpoint")); ExpressionStatement statement = ast.newExpressionStatement(assignment); thenBranch.statements().add(statement); // Propagate the change to other objects monitored by the same old // checkpoint. MethodInvocation propagate = ast.newMethodInvocation(); propagate.setExpression(ast.newSimpleName("oldCheckpoint")); propagate.setName(ast.newSimpleName("setCheckpoint")); propagate.arguments().add(ast.newSimpleName("checkpoint")); thenBranch.statements().add(ast.newExpressionStatement(propagate)); // Add this object to the list in the checkpoint. MethodInvocation addInvocation = ast.newMethodInvocation(); addInvocation.setExpression(ast.newSimpleName("checkpoint")); addInvocation.setName(ast.newSimpleName("addObject")); addInvocation.arguments().add(_createRollbackableObject(ast, isAnonymous)); thenBranch.statements().add(ast.newExpressionStatement(addInvocation)); // Return this object. ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ast.newThisExpression()); body.statements().add(returnStatement); } method.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); if (!isInterface) { method.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); } if (!isAnonymous && (parent != null)) { addToLists(_nodeSubstitution, parent.getName(), new NodeReplace(method, null)); } return method; }
From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java
License:Open Source License
/** Handle a class declaration or anonymous class declaration. Records and * assignment methods are added to the declaration. * * @param node The AST node of class declaration or anonymous class * declaration./* www . j av a 2s. c o m*/ * @param bodyDeclarations The list of body declarations in the class. * @param state The current state of the type analyzer. */ private void _handleDeclaration(ASTNode node, List bodyDeclarations, TypeAnalyzerState state) { Class currentClass = state.getCurrentClass(); @SuppressWarnings("unused") Class parent = currentClass.getSuperclass(); List newMethods = new LinkedList(); List newFields = new LinkedList(); AST ast = node.getAST(); CompilationUnit root = (CompilationUnit) node.getRoot(); List fieldNames = new LinkedList(); List fieldTypes = new LinkedList(); // Iterate over all the body declarations. Iterator bodyIter = bodyDeclarations.iterator(); while (bodyIter.hasNext()) { Object nextDeclaration = bodyIter.next(); // Handle only field declarations. if (nextDeclaration instanceof FieldDeclaration) { FieldDeclaration fieldDecl = (FieldDeclaration) nextDeclaration; boolean isStatic = Modifier.isStatic(fieldDecl.getModifiers()); // If HANDLE_STATIC_FIELDS is set to false, do not refactor // static fields. if (isStatic && (HANDLE_STATIC_FIELDS != true)) { continue; } // Handle only private fields or the $CHECKPOINT special field. if (Modifier.isPrivate(fieldDecl.getModifiers())) { Type type = Type.getType(fieldDecl); // Iterate over all the fragments in the field declaration. Iterator fragmentIter = fieldDecl.fragments().iterator(); while (fragmentIter.hasNext()) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragmentIter.next(); String fieldName = fragment.getName().getIdentifier(); // Get the list of numbers of indices. Hashtable[] tables = new Hashtable[] { _accessedFields, _specialAccessedFields, _backupFields }; for (int i = 0; i < tables.length; i++) { List indicesList = _getAccessedField(tables[i], currentClass.getName(), fieldName); if (indicesList == null) { continue; } // Iterate over all the numbers of indices. Iterator indicesIter = indicesList.iterator(); while (indicesIter.hasNext()) { int indices = ((Integer) indicesIter.next()).intValue(); // Create an extra method for every different // number of indices. if (tables[i] == _backupFields) { newMethods.add(_createBackupMethod(ast, root, state, fieldName, type, indices, isStatic)); } else { newMethods.add(_createAssignMethod(ast, root, state, fieldName, type, indices, tables[i] == _specialAccessedFields, isStatic)); } } } fieldNames.add(fieldName); fieldTypes.add(type); // Create a record field. FieldDeclaration field = _createFieldRecord(ast, root, state, fieldName, type.dimensions(), isStatic); if (field != null) { newFields.add(field); } } } } } boolean isInterface = node instanceof TypeDeclaration && ((TypeDeclaration) node).isInterface(); boolean isAnonymous = node instanceof AnonymousClassDeclaration; if (isAnonymous) { Class[] interfaces = currentClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (state.getCrossAnalyzedTypes().contains(interfaces[i].getName())) { isAnonymous = false; } } } RehandleDeclarationRecord declarationRecord = null; if (isAnonymous) { Class[] interfaces = currentClass.getInterfaces(); if (interfaces.length == 1) { declarationRecord = new RehandleDeclarationRecord(bodyDeclarations); addToLists(_rehandleDeclaration, interfaces[0].getName(), declarationRecord); } } // Do not handle anonymous class declarations in a static method. boolean ignore = !_isInStatic.isEmpty() && (_isInStatic.peek().equals(Boolean.TRUE)) && isAnonymous; // Add an array of all the records. if (!isInterface && !ignore) { newFields.add(_createRecordArray(ast, root, state, fieldNames)); } // Add a commit method. MethodDeclaration commitMethod = null; if (!ignore) { commitMethod = _createCommitMethod(ast, root, state, fieldNames, fieldTypes, isAnonymous, isInterface); newMethods.add(commitMethod); } if (declarationRecord != null) { if (!ignore) { declarationRecord._addExtendedDeclaration(commitMethod); } MethodDeclaration fixedCommitMethod = _createCommitMethod(ast, root, state, fieldNames, fieldTypes, false, isInterface); declarationRecord._addFixedDeclaration(fixedCommitMethod); } // Add a restore method. MethodDeclaration restoreMethod = null; if (!ignore) { restoreMethod = _createRestoreMethod(ast, root, state, fieldNames, fieldTypes, isAnonymous, isInterface); newMethods.add(restoreMethod); } if (declarationRecord != null) { if (!ignore) { declarationRecord._addExtendedDeclaration(restoreMethod); } MethodDeclaration fixedRestoreMethod = _createRestoreMethod(ast, root, state, fieldNames, fieldTypes, false, isInterface); declarationRecord._addFixedDeclaration(fixedRestoreMethod); } // Get checkpoint method. MethodDeclaration getCheckpoint = null; if (!ignore) { getCheckpoint = _createGetCheckpointMethod(ast, root, state, isAnonymous, isInterface); if (getCheckpoint != null) { newMethods.add(getCheckpoint); } } if (declarationRecord != null) { if (!ignore) { declarationRecord._addExtendedDeclaration(getCheckpoint); } MethodDeclaration fixedGetCheckpoint = _createGetCheckpointMethod(ast, root, state, false, isInterface); declarationRecord._addFixedDeclaration(fixedGetCheckpoint); } // Set checkpoint method. MethodDeclaration setCheckpoint = null; if (!ignore) { setCheckpoint = _createSetCheckpointMethod(ast, root, state, isAnonymous, isInterface); if (setCheckpoint != null) { newMethods.add(setCheckpoint); } } if (declarationRecord != null) { if (!ignore) { declarationRecord._addExtendedDeclaration(setCheckpoint); } MethodDeclaration fixedSetCheckpoint = _createSetCheckpointMethod(ast, root, state, false, isInterface); declarationRecord._addFixedDeclaration(fixedSetCheckpoint); } // Add an interface. if (!ignore) { if (isAnonymous) { TypeDeclaration proxy = _createProxyClass(ast, root, state); bodyDeclarations.add(proxy); if (declarationRecord != null) { declarationRecord._addExtendedDeclaration(proxy); } } else { // Set the class to implement Rollbackable. if (node instanceof TypeDeclaration) { String rollbackType = getClassName(Rollbackable.class, state, root); ((TypeDeclaration) node).superInterfaceTypes() .add(ast.newSimpleType(createName(ast, rollbackType))); } if (!isInterface) { // Create a checkpoint field. FieldDeclaration checkpointField = _createCheckpointField(ast, root, state); if (checkpointField != null) { bodyDeclarations.add(0, checkpointField); } // Create a record for the checkpoint field. FieldDeclaration record = _createCheckpointRecord(ast, root, state); if (record != null) { newFields.add(0, record); } } } } // Add all the methods and then all the fields. if (!ignore) { bodyDeclarations.addAll(newMethods); bodyDeclarations.addAll(newFields); } else { if (declarationRecord != null) { declarationRecord._addFixedDeclarations(newMethods); declarationRecord._addFixedDeclarations(newFields); } } if (isAnonymous && !ignore) { // Create a simple initializer. Initializer initializer = ast.newInitializer(); Block body = ast.newBlock(); initializer.setBody(body); MethodInvocation addInvocation = ast.newMethodInvocation(); addInvocation.setExpression(ast.newSimpleName(CHECKPOINT_NAME)); addInvocation.setName(ast.newSimpleName("addObject")); ClassInstanceCreation proxy = ast.newClassInstanceCreation(); proxy.setType(ast.newSimpleType(ast.newSimpleName(_getProxyName()))); addInvocation.arguments().add(proxy); body.statements().add(ast.newExpressionStatement(addInvocation)); bodyDeclarations.add(initializer); if (declarationRecord != null) { declarationRecord._addExtendedDeclaration(initializer); } } }
From source file:refactorer.Refactorer.java
License:Apache License
@SuppressWarnings("unchecked") protected void processTypeDeclaration(AbstractTypeDeclaration type) { if (type == null) return;// ww w. j ava 2 s .com List<BodyDeclaration> bodies = type.bodyDeclarations(); for (BodyDeclaration body : bodies) { processBodyDeclaration(body); } if (type.getNodeType() == ASTNode.TYPE_DECLARATION) { TypeDeclaration typeDeclaration = (TypeDeclaration) type; if (typeDeclaration.getSuperclassType() != null) { ITypeBinding superclass = typeDeclaration.getSuperclassType().resolveBinding(); if (superclass != null) { if ("javax.media.opengl.GLCanvas".equals(superclass.getQualifiedName())) { addImportIfRequired("javax.media.opengl.awt.GLCanvas"); } else if ("javax.media.opengl.GLJPanel".equals(superclass.getQualifiedName())) { addImportIfRequired("javax.media.opengl.awt.GLJPanel"); } } } for (Type interfaceType : (List<Type>) typeDeclaration.superInterfaceTypes()) { ITypeBinding binding = interfaceType.resolveBinding(); if (binding != null) { String interfaceName = binding.getQualifiedName(); if ("javax.media.opengl.GLEventListener".equals(interfaceName)) { for (int i = bodies.size() - 1; i >= 0; i--) { BodyDeclaration body = bodies.get(i); if (body.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration methodDeclaration = (MethodDeclaration) body; if ("displayChanged".equals(methodDeclaration.getName().getFullyQualifiedName())) { bodies.remove(i); } } } AST ast = typeDeclaration.getAST(); MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); methodDeclaration.setName(ast.newSimpleName("dispose")); methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block block = ast.newBlock(); methodDeclaration.setBody(block); SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration(); parameter.setName(ast.newSimpleName("glDrawable")); parameter.setType(ast.newSimpleType(ast.newName("GLAutoDrawable"))); methodDeclaration.parameters().add(parameter); bodies.add(methodDeclaration); if ("performance.VBORenderer.GLDisplay.MyGLEventListener" .equals(type.resolveBinding().getQualifiedName())) { ForStatement forStatement = ast.newForStatement(); VariableDeclarationFragment forInitializerFragment = ast .newVariableDeclarationFragment(); forInitializerFragment.setName(ast.newSimpleName("i")); forInitializerFragment.setInitializer(ast.newNumberLiteral("0")); VariableDeclarationExpression forInitializer = ast .newVariableDeclarationExpression(forInitializerFragment); forInitializer.setType(ast.newPrimitiveType(PrimitiveType.INT)); forStatement.initializers().add(forInitializer); InfixExpression forExpression = ast.newInfixExpression(); forExpression.setLeftOperand(ast.newSimpleName("i")); forExpression.setOperator(InfixExpression.Operator.LESS); MethodInvocation rightOperand = ast.newMethodInvocation(); rightOperand.setExpression(ast.newSimpleName("eventListeners")); rightOperand.setName(ast.newSimpleName("size")); forExpression.setRightOperand(rightOperand); forStatement.setExpression(forExpression); PostfixExpression forUpdater = ast.newPostfixExpression(); forUpdater.setOperand(ast.newSimpleName("i")); forUpdater.setOperator(PostfixExpression.Operator.INCREMENT); forStatement.updaters().add(forUpdater); Block forBlock = ast.newBlock(); MethodInvocation mi1 = ast.newMethodInvocation(); mi1.setName(ast.newSimpleName("dispose")); mi1.arguments().add(ast.newName("glDrawable")); CastExpression castExpression = ast.newCastExpression(); castExpression.setType(ast.newSimpleType(ast.newName("GLEventListener"))); MethodInvocation mi2 = ast.newMethodInvocation(); mi2.setExpression(ast.newName("eventListeners")); mi2.setName(ast.newSimpleName("get")); mi2.arguments().add(ast.newName("i")); castExpression.setExpression(mi2); ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(castExpression); mi1.setExpression(parenthesizedExpression); forBlock.statements().add(ast.newExpressionStatement(mi1)); forStatement.setBody(forBlock); block.statements().add(forStatement); } } else if ("com.sun.opengl.impl.packrect.BackingStoreManager".equals(interfaceName)) { AST ast = typeDeclaration.getAST(); MethodDeclaration newMethodDeclaration = ast.newMethodDeclaration(); newMethodDeclaration.setName(ast.newSimpleName("canCompact")); newMethodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); newMethodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN)); Block block = ast.newBlock(); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ast.newBooleanLiteral(false)); block.statements().add(returnStatement); newMethodDeclaration.setBody(block); bodies.add(newMethodDeclaration); if ("gov.nasa.worldwind.util.TextureAtlas.AtlasBackingStore" .equals(type.resolveBinding().getQualifiedName())) { for (BodyDeclaration body : (List<BodyDeclaration>) typeDeclaration .bodyDeclarations()) { if (body.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration methodDeclaration = (MethodDeclaration) body; if ("additionFailed" .equals(methodDeclaration.getName().getFullyQualifiedName())) { methodDeclaration .setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN)); Block body2 = methodDeclaration.getBody(); if (!body2.statements().isEmpty()) { Statement firstStatement = (Statement) body2.statements().get(0); if (firstStatement.getNodeType() == ASTNode.IF_STATEMENT) { IfStatement ifStatement = (IfStatement) firstStatement; Expression expression = ifStatement.getExpression(); ifStatement.setExpression(ast.newBooleanLiteral(true)); ReturnStatement newReturnStatement = ast.newReturnStatement(); newReturnStatement.setExpression(expression); body2.statements().clear(); body2.statements().add(newReturnStatement); } } } } } } } } } } }
From source file:refactorer.Refactorer.java
License:Apache License
@SuppressWarnings("unchecked") protected void processStatement(Statement statement) { if (statement == null) return;//from w w w .ja v a 2 s .c o m switch (statement.getNodeType()) { case ASTNode.ASSERT_STATEMENT: case ASTNode.BREAK_STATEMENT: case ASTNode.CONTINUE_STATEMENT: case ASTNode.EMPTY_STATEMENT: case ASTNode.LABELED_STATEMENT: //ignore break; case ASTNode.BLOCK: variables.push(); Block block = (Block) statement; for (Statement s : (List<Statement>) block.statements()) { processStatement(s); } variables.pop(); break; case ASTNode.CONSTRUCTOR_INVOCATION: ConstructorInvocation constructorInvocation = (ConstructorInvocation) statement; for (Expression e : (List<Expression>) constructorInvocation.arguments()) { processExpression(e); } break; case ASTNode.DO_STATEMENT: DoStatement doStatement = (DoStatement) statement; processExpression(doStatement.getExpression()); processStatement(doStatement.getBody()); break; case ASTNode.ENHANCED_FOR_STATEMENT: EnhancedForStatement enhancedForStatement = (EnhancedForStatement) statement; processSingleVariableDeclaration(enhancedForStatement.getParameter()); processExpression(enhancedForStatement.getExpression()); processStatement(enhancedForStatement.getBody()); break; case ASTNode.EXPRESSION_STATEMENT: ExpressionStatement expressionStatement = (ExpressionStatement) statement; processExpression(expressionStatement.getExpression()); break; case ASTNode.FOR_STATEMENT: ForStatement forStatement = (ForStatement) statement; for (Expression e : (List<Expression>) forStatement.initializers()) { processExpression(e); } processExpression(forStatement.getExpression()); for (Expression e : (List<Expression>) forStatement.updaters()) { processExpression(e); } processStatement(forStatement.getBody()); break; case ASTNode.IF_STATEMENT: IfStatement ifStatement = (IfStatement) statement; processExpression(ifStatement.getExpression()); processStatement(ifStatement.getThenStatement()); processStatement(ifStatement.getElseStatement()); break; case ASTNode.RETURN_STATEMENT: ReturnStatement returnStatement = (ReturnStatement) statement; processExpression(returnStatement.getExpression()); break; case ASTNode.SUPER_CONSTRUCTOR_INVOCATION: SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) statement; processExpression(superConstructorInvocation.getExpression()); for (Expression e : (List<Expression>) superConstructorInvocation.arguments()) { processExpression(e); } break; case ASTNode.SWITCH_CASE: SwitchCase switchCase = (SwitchCase) statement; processExpression(switchCase.getExpression()); break; case ASTNode.SWITCH_STATEMENT: SwitchStatement switchStatement = (SwitchStatement) statement; processExpression(switchStatement.getExpression()); for (Statement s : (List<Statement>) switchStatement.statements()) { processStatement(s); } break; case ASTNode.SYNCHRONIZED_STATEMENT: SynchronizedStatement synchronizedStatement = (SynchronizedStatement) statement; processExpression(synchronizedStatement.getExpression()); processStatement(synchronizedStatement.getBody()); break; case ASTNode.THROW_STATEMENT: ThrowStatement throwStatement = (ThrowStatement) statement; processExpression(throwStatement.getExpression()); break; case ASTNode.TRY_STATEMENT: TryStatement tryStatement = (TryStatement) statement; processStatement(tryStatement.getBody()); for (CatchClause cc : (List<CatchClause>) tryStatement.catchClauses()) { processStatement(cc.getBody()); } processStatement(tryStatement.getFinally()); break; case ASTNode.TYPE_DECLARATION_STATEMENT: TypeDeclarationStatement typeDeclarationStatement = (TypeDeclarationStatement) statement; processTypeDeclaration(typeDeclarationStatement.getDeclaration()); break; case ASTNode.VARIABLE_DECLARATION_STATEMENT: VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) statement; processVariableDeclarationStatement(variableDeclarationStatement); break; case ASTNode.WHILE_STATEMENT: WhileStatement whileStatement = (WhileStatement) statement; processExpression(whileStatement.getExpression()); processStatement(whileStatement.getBody()); break; } }
From source file:refactorer.Refactorer.java
License:Apache License
@SuppressWarnings("unchecked") protected void processMethodInvocation(MethodInvocation methodInvocation) { if (methodInvocation == null) return;/* w ww . j a va 2s. com*/ Expression methodExpression = methodInvocation.getExpression(); if (methodExpression != null) { ITypeBinding type = methodExpression.resolveTypeBinding(); String methodName = methodInvocation.getName().getFullyQualifiedName(); if (type != null && methodName != null) { AST ast = methodExpression.getAST(); String typeName = type.getQualifiedName(); if (("javax.media.opengl.GLContext".equals(typeName) || "javax.media.opengl.GLAutoDrawable".equals(type.getQualifiedName())) && "getGL".equals(methodName)) { Expression newExpression = ast.newName("IGNORED"); newExpression = (Expression) ASTNode.copySubtree(newExpression.getAST(), methodInvocation.getExpression()); MethodInvocation newInvocation = ast.newMethodInvocation(); newInvocation.setExpression(newExpression); newInvocation.setName(ast.newSimpleName("getGL")); methodInvocation.setExpression(newInvocation); methodInvocation.setName(ast.newSimpleName("getGL2")); } else if ("javax.media.opengl.GL".equals(typeName)) { if (methodName.endsWith("EXT") || methodName.endsWith("ARB")) { methodInvocation .setName(ast.newSimpleName(methodName.substring(0, methodName.length() - 3))); } } else if ("com.sun.opengl.util.BufferUtil".equals(typeName)) { methodInvocation.setExpression(ast.newSimpleName("Buffers")); if ("newByteBuffer".equals(methodName)) { methodInvocation.setName(ast.newSimpleName("newDirectByteBuffer")); } else if ("newIntBuffer".equals(methodName)) { methodInvocation.setName(ast.newSimpleName("newDirectIntBuffer")); } else if ("newFloatBuffer".equals(methodName)) { methodInvocation.setName(ast.newSimpleName("newDirectFloatBuffer")); } else if ("newDoubleBuffer".equals(methodName)) { methodInvocation.setName(ast.newSimpleName("newDirectDoubleBuffer")); } } else if ("com.sun.opengl.util.texture.TextureIO".equals(typeName)) { boolean newTexture = "newTexture".equals(methodName); boolean newTextureData = "newTextureData".equals(methodName); if (newTexture || newTextureData) { boolean addGLProfile = newTextureData; if (methodInvocation.arguments().size() == 2) { Expression secondArgument = (Expression) methodInvocation.arguments().get(0); if ("java.awt.image.BufferedImage" .equals(secondArgument.resolveTypeBinding().getQualifiedName())) { methodInvocation.setExpression(ast.newSimpleName("AWTTextureIO")); addImportIfRequired("com.jogamp.opengl.util.texture.awt.AWTTextureIO"); addGLProfile = true; } } if (addGLProfile) { MethodInvocation newMethodInvocation = ast.newMethodInvocation(); newMethodInvocation.setExpression(ast.newSimpleName("GLProfile")); newMethodInvocation.setName(ast.newSimpleName("get")); newMethodInvocation.arguments() .add(ast.newQualifiedName(ast.newName("GLProfile"), ast.newSimpleName("GL2"))); methodInvocation.arguments().add(0, newMethodInvocation); addImportIfRequired("javax.media.opengl.GLProfile"); } } } else if ("com.sun.opengl.util.texture.Texture".equals(typeName) && ("bind".equals(methodName) || "getTextureObject".equals(methodName) || "setTexParameteri".equals(methodName) || "dispose".equals(methodName) || "enable".equals(methodName) || "disable".equals(methodName) || "updateSubImage".equals(methodName) || "updateImage".equals(methodName))) { List<LocalVariable> locals = variables.getLocalVariablesMatchingType("javax.media.opengl.GL"); if (!locals.isEmpty()) { Name name = ast.newName(locals.get(0).name); methodInvocation.arguments().add(name); } else { locals = variables.getLocalVariablesMatchingType("gov.nasa.worldwind.render.DrawContext"); if (!locals.isEmpty()) { MethodInvocation newMethodInvocation = ast.newMethodInvocation(); newMethodInvocation.setExpression(ast.newSimpleName(locals.get(0).name)); newMethodInvocation.setName(ast.newSimpleName("getGL")); methodInvocation.arguments().add(0, newMethodInvocation); } else { //GLContext.getCurrent().getGL().getGL2() MethodInvocation mi1 = ast.newMethodInvocation(); mi1.setExpression(ast.newName("GLContext")); mi1.setName(ast.newSimpleName("getCurrent")); MethodInvocation mi2 = ast.newMethodInvocation(); mi2.setExpression(mi1); mi2.setName(ast.newSimpleName("getGL")); MethodInvocation mi3 = ast.newMethodInvocation(); mi3.setExpression(mi2); mi3.setName(ast.newSimpleName("getGL2")); methodInvocation.arguments().add(0, mi3); addImportIfRequired("javax.media.opengl.GLContext"); } } if ("dispose".equals(methodName)) { methodInvocation.setName(ast.newSimpleName("destroy")); } } else if ("javax.media.opengl.GLAutoDrawable".equals(typeName) && "repaint".equals(methodName)) { ASTNode parent = methodInvocation.getParent().getParent(); if (parent.getNodeType() == ASTNode.BLOCK || parent.getNodeType() == ASTNode.IF_STATEMENT) { IfStatement ifStatement = ast.newIfStatement(); InstanceofExpression instanceofExpression = ast.newInstanceofExpression(); ifStatement.setExpression(instanceofExpression); Expression leftOperation = ast.newName("IGNORED"); leftOperation = (Expression) ASTNode.copySubtree(leftOperation.getAST(), methodInvocation.getExpression()); instanceofExpression.setLeftOperand(leftOperation); instanceofExpression.setRightOperand(ast.newSimpleType(ast.newName("Component"))); Expression castExpressionExpression = ast.newName("IGNORED"); castExpressionExpression = (Expression) ASTNode .copySubtree(castExpressionExpression.getAST(), methodInvocation.getExpression()); CastExpression castExpression = ast.newCastExpression(); castExpression.setExpression(castExpressionExpression); castExpression.setType(ast.newSimpleType(ast.newName("Component"))); ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(castExpression); methodInvocation.setExpression(parenthesizedExpression); if (parent.getNodeType() == ASTNode.BLOCK) { Block parentBlock = (Block) parent; parentBlock.statements().remove(methodInvocation.getParent()); parentBlock.statements().add(ifStatement); } else if (parent.getNodeType() == ASTNode.IF_STATEMENT) { IfStatement parentIfStatement = (IfStatement) parent; if (parentIfStatement.getThenStatement() == methodInvocation.getParent()) { parentIfStatement.setThenStatement(ifStatement); } else if (parentIfStatement.getElseStatement() == methodInvocation.getParent()) { parentIfStatement.setElseStatement(ifStatement); } } ifStatement.setThenStatement((Statement) methodInvocation.getParent()); addImportIfRequired("java.awt.Component"); } } else if ("com.sun.opengl.util.Screenshot".equals(typeName)) { methodInvocation.setExpression(ast.newSimpleName("Screenshot")); addImportIfRequired("com.jogamp.opengl.util.awt.Screenshot"); } } } processExpression(methodInvocation.getExpression()); for (Expression e : (List<Expression>) methodInvocation.arguments()) { processExpression(e); } }
From source file:sharpen.core.CSharpBuilder.java
License:Open Source License
private void processBlock(BodyDeclaration node, Block block, final CSBlock targetBlock) { if (containsJavadoc(node, SharpenAnnotations.SHARPEN_REMOVE_FIRST)) { block.statements().remove(0); }/*from w ww . j a va2s.c o m*/ BodyDeclaration savedDeclaration = _currentBodyDeclaration; _currentBodyDeclaration = node; if (Modifier.isSynchronized(node.getModifiers())) { CSLockStatement lock = new CSLockStatement(node.getStartPosition(), getLockTarget(node)); targetBlock.addStatement(lock); visitBlock(lock.body(), block); } else { visitBlock(targetBlock, block); } _currentBodyDeclaration = savedDeclaration; }
From source file:sourcecodefilter.filter.AssignmentFilter.java
License:Open Source License
@SuppressWarnings("unchecked") public void removeUnallowedAssignments() { CompilationUnit cu = catroidSource.getSourceAst(); final List<AbstractTypeDeclaration> types = cu.types(); assert types.size() > 0; for (AbstractTypeDeclaration abstractTypeDecl : types) { for (BodyDeclaration bodyDecl : new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) { if (bodyDecl.getNodeType() != ASTNode.METHOD_DECLARATION) { continue; }/*from w ww .j a v a2s.c om*/ MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDecl; Block body = methodDeclaration.getBody(); if ((body == null) || (body.statements().size() == 0)) { continue; } methodDeclaration.accept(this); } } }
From source file:sourcecodefilter.filter.IfElseFilter.java
License:Open Source License
@SuppressWarnings("unchecked") public void removeUnallowedIfElseBlocks() { CompilationUnit cu = catroidSource.getSourceAst(); final List<AbstractTypeDeclaration> types = cu.types(); assert types.size() > 0; for (AbstractTypeDeclaration abstractTypeDecl : types) { for (BodyDeclaration bodyDecl : new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) { if (bodyDecl.getNodeType() != ASTNode.METHOD_DECLARATION) { continue; }/* w w w.j a v a 2s.co m*/ MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDecl; Block body = methodDeclaration.getBody(); if ((body == null) || (body.statements().size() == 0)) { continue; } methodDeclaration.accept(this); } } }
From source file:sourcecodefilter.filter.MethodInvocationFilter.java
License:Open Source License
@SuppressWarnings("unchecked") public void removeUnallowedMethodInvocations() { CompilationUnit cu = catroidSource.getSourceAst(); final List<AbstractTypeDeclaration> types = cu.types(); assert types.size() > 0; if (catroidSource.getQualifiedClassName().equals("org.catrobat.catroid.content.Scene")) { return;// w w w . java2s . c om } for (AbstractTypeDeclaration abstractTypeDecl : types) { for (BodyDeclaration bodyDecl : new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) { if (bodyDecl.getNodeType() != ASTNode.METHOD_DECLARATION) { continue; } MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDecl; Block body = methodDeclaration.getBody(); if ((body == null) || (body.statements().size() == 0)) { continue; } try { methodDeclaration.accept(this); } catch (java.lang.IllegalArgumentException ex) { System.err .println("Unable to filter methods in: " + this.catroidSource.getQualifiedClassName()); } } } }