List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationExpression setType
public void setType(Type type)
From source file:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static VariableDeclarationExpression newVariableDeclarationExpression(AST ast, IVariableBinding binding, Expression initializer) { VariableDeclarationExpression decl = ast .newVariableDeclarationExpression(newVariableDeclarationFragment(ast, binding, initializer)); decl.setType(newType(ast, binding.getType())); ASTUtil.getModifiers(decl).addAll(newModifiers(ast, binding.getModifiers())); Types.addBinding(decl, binding.getType()); return decl;/* w ww . j av a 2 s . c o m*/ }
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.VariableDeclarationExpression node) { VariableDeclarationExpression element = (VariableDeclarationExpression) this.binding.get(node); this.initializeNode(element, node); if (this.binding.get(node.getType()) != null) element.setType((NamedElementRef) this.binding.get(node.getType())); for (Iterator<?> i = node.fragments().iterator(); i.hasNext();) { VariableDeclarationFragment itElement = (VariableDeclarationFragment) this.binding.get(i.next()); if (itElement != null) element.getFragments().add(itElement); }/*ww w .j a v a 2 s . co m*/ // visibility modifiers for (Object modifierNode : node.modifiers()) { if ((((IExtendedModifier) modifierNode).isModifier()) && (this.binding.get(modifierNode) != null)) { element.getModifiers().add((Modifier) this.binding.get(modifierNode)); } } }
From source file:org.autorefactor.refactoring.rules.ReduceVariableScopeRefactoring.java
License:Open Source License
private void replace(VariableAccess varDecl, VariableAccess varAccess) { final ASTBuilder b = this.ctx.getASTBuilder(); final AST ast = b.getAST(); final ASTNode scope = varAccess.getScope(); final Name varName = varAccess.getVariableName(); final Type varType = getType(varDecl.getVariableName().getParent()); if (scope instanceof Block) { final List<Statement> stmts = statements((Block) scope); for (int i = 0; i < stmts.size(); i++) { final Statement stmt = stmts.get(i); final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0 final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0 if (stmt.equals(parentStmt)) { final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName); final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf); vds.setType(varType);/*w ww .ja v a 2 s.c o m*/ this.ctx.getRefactorings().replace(stmt, vds); break; } } } else if (scope instanceof EnhancedForStatement) { final EnhancedForStatement efs = (EnhancedForStatement) scope; final EnhancedForStatement newEfs = b.copy(efs); newEfs.setParameter(b.copy(efs.getParameter())); newEfs.setExpression(b.copy(efs.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(efs.getBody(), parentStmt)) { newEfs.setBody(copy(efs.getBody(), varName)); } this.ctx.getRefactorings().replace(efs, newEfs); } else if (scope instanceof ForStatement) { final ForStatement fs = (ForStatement) scope; final ForStatement newFs = b.copy(fs); final List<Expression> initializers = initializers(newFs); if (initializers.size() == 1) { final Expression init = initializers.remove(0); final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName); final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf); vde.setType(varType); initializers.add(vde); this.ctx.getRefactorings().replace(fs, newFs); // TODO JNR // if (equalNotNull(fs.getBody(), parentStmt)) { // newFs.setBody(copy(fs.getBody())); // } } else { throw new NotImplementedException(scope, "for more than one initializer in for loop."); } } else if (scope instanceof WhileStatement) { final WhileStatement ws = (WhileStatement) scope; final WhileStatement newWs = ast.newWhileStatement(); newWs.setExpression(b.copy(ws.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(ws.getBody(), parentStmt)) { newWs.setBody(copy(ws.getBody(), varName)); } this.ctx.getRefactorings().replace(ws, newWs); } else if (scope instanceof IfStatement) { final IfStatement is = (IfStatement) scope; final IfStatement newIs = ast.newIfStatement(); newIs.setExpression(b.copy(is.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(is.getThenStatement(), parentStmt)) { newIs.setThenStatement(copy(is.getThenStatement(), varName)); if (is.getElseStatement() != null) { newIs.setElseStatement(b.copy(is.getElseStatement())); } this.ctx.getRefactorings().replace(is, newIs); } else if (equalNotNull(is.getElseStatement(), parentStmt)) { if (is.getThenStatement() != null) { newIs.setThenStatement(b.copy(is.getThenStatement())); } newIs.setElseStatement(copy(is.getElseStatement(), varName)); this.ctx.getRefactorings().replace(is, newIs); } else { throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is); } } else { throw new NotImplementedException(scope); } }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
protected void setTypeForVariableDeclarationExpression( VariableDeclarationExpression variableDeclarationExpression, Type type, int extraDimension) { if (extraDimension != 0) { if (type.isArrayType()) { ArrayType arrayType = (ArrayType) type; int remainingDimensions = arrayType.getDimensions() - extraDimension; if (remainingDimensions == 0) { // the dimensions are after the name so the type of the fieldDeclaration is a simpleType Type elementType = arrayType.getElementType(); // cut the child loose from its parent (without creating garbage) elementType.setParent(null, null); this.ast.getBindingResolver().updateKey(type, elementType); variableDeclarationExpression.setType(elementType); } else { int start = type.getStartPosition(); ArrayType subarrayType = arrayType; int index = extraDimension; while (index > 0) { subarrayType = (ArrayType) subarrayType.getComponentType(); index--;//from w w w .j av a2 s.co m } int end = retrieveProperRightBracketPosition(remainingDimensions, start); subarrayType.setSourceRange(start, end - start + 1); // cut the child loose from its parent (without creating garbage) subarrayType.setParent(null, null); updateInnerPositions(subarrayType, remainingDimensions); variableDeclarationExpression.setType(subarrayType); this.ast.getBindingResolver().updateKey(type, subarrayType); } } else { variableDeclarationExpression.setType(type); } } else { variableDeclarationExpression.setType(type); } }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java
License:Open Source License
@Override public void endVisit(final org.eclipse.jdt.core.dom.VariableDeclarationExpression node) { VariableDeclarationExpression element = (VariableDeclarationExpression) this.binding.get(node); initializeNode(element, node);//from www . j av a2 s . c o m if (this.binding.get(node.getType()) != null) { element.setType(JDTVisitorUtils.completeTypeAccess(this.binding.get(node.getType()), this)); } for (Iterator<?> i = node.fragments().iterator(); i.hasNext();) { VariableDeclarationFragment itElement = (VariableDeclarationFragment) this.binding.get(i.next()); if (itElement != null) { element.getFragments().add(itElement); } } // visibility modifiers Modifier modiscoModifier = this.factory.createModifier(); element.setModifier(modiscoModifier); modiscoModifier.setVariableDeclarationExpression(element); for (Object modifierNode : node.modifiers()) { if (((IExtendedModifier) modifierNode).isModifier()) { manageModifier((org.eclipse.jdt.core.dom.Modifier) modifierNode, modiscoModifier); } } // annotation modifiers for (Object modifierNode : node.modifiers()) { if (((IExtendedModifier) modifierNode).isAnnotation()) { Annotation annotation = (Annotation) this.binding.get(modifierNode); if (annotation != null) { element.getAnnotations().add(annotation); } } } }
From source file:org.whole.lang.java.util.JDTTransformerVisitor.java
License:Open Source License
public boolean visit(VariableDeclarationExpression node) { org.whole.lang.java.model.VariableDeclarationExpression varDecl = lf .create(JavaEntityDescriptorEnum.VariableDeclarationExpression); List<?> modifiers = node.modifiers(); if (!modifiers.isEmpty()) { varDecl.setModifiers(lf.create(JavaEntityDescriptorEnum.ExtendedModifiers)); setExtendedModifiers(varDecl.getModifiers(), modifiers); }/*from w ww . java 2 s .c o m*/ acceptChild(node.getType()); varDecl.setType(type); VariableDeclarationFragments fragments; varDecl.setFragments(fragments = lf.create(JavaEntityDescriptorEnum.VariableDeclarationFragments)); Iterator<?> i = node.fragments().iterator(); while (i.hasNext()) { ((ASTNode) i.next()).accept(this); fragments.wAdd(varFrag); } exp = varDecl; return false; }
From source file:refactorer.Refactorer.java
License:Apache License
@SuppressWarnings("unchecked") protected void processTypeDeclaration(AbstractTypeDeclaration type) { if (type == null) return;//from ww w. j ava 2 s .co m 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); } } } } } } } } } } }