List of usage examples for org.eclipse.jdt.core.dom SimpleName getAST
public final AST getAST()
From source file:com.flamefire.importsmalinames.astutils.RenameVariablesVisitor.java
License:Open Source License
@Override public boolean visit(SimpleName node) { // We have to be inside a method if (curMethod == null) return false; // Only replace variables IBinding binding = node.resolveBinding(); if (binding == null) { if ((node.getParent() instanceof LabeledStatement) && ((LabeledStatement) node.getParent()).getLabel().equals(node)) return false; if ((node.getParent() instanceof BreakStatement) && ((BreakStatement) node.getParent()).getLabel().equals(node)) return false; if (node.getParent() instanceof QualifiedName) return false; // This may happen System.err.println("Detected SimpleName without binding: " + node + "; Parent:" + node.getParent() + "\nThis may happen if there are compile errors"); // return false; } else if (binding.getKind() != IBinding.VARIABLE) return false; // Check if we need to add a "this" // Do this if current node is a field and we may replace a variable with // its name//from w w w.j ava2 s . c o m AST ast = node.getAST(); IVariableBinding vBinding = (IVariableBinding) binding; // Check for field acceses if (vBinding != null && vBinding.isField()) { // Add this if necessary if (renaming.containsValue(node.toString()) && !(node.getParent() instanceof FieldAccess) && !(node.getParent() instanceof QualifiedName)) { FieldAccess fa = ast.newFieldAccess(); fa.setExpression(ast.newThisExpression()); fa.setName(ast.newSimpleName(node.toString())); astRewrite.replace(node, fa, null); } return false; } String newName = renaming.get(node.toString()); if (newName == null || newName == "") return false; astRewrite.replace(node, ast.newSimpleName(newName), null); return false; }
From source file:net.sf.eclipsecs.ui.quickfixes.coding.RequireThisQuickfix.java
License:Open Source License
/** * {@inheritDoc}/*from w w w.ja va 2 s . co m*/ */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) { return new ASTVisitor() { @Override public boolean visit(final SimpleName node) { if (containsPosition(node, markerStartOffset)) { replace(node, findFieldReplacement(node, node, 0)); } return false; } @Override public boolean visit(final MethodInvocation node) { if (containsPosition(node, markerStartOffset)) { replace(node, findMethodReplacement(node.getName(), node, node, 0)); } return false; } private Expression findFieldReplacement(final SimpleName name, final ASTNode node, int typeLevel) { int level = typeLevel; final ASTNode parent = node.getParent(); if (parent instanceof TypeDeclaration) { level++; final TypeDeclaration type = (TypeDeclaration) parent; for (final FieldDeclaration fieldDeclaration : type.getFields()) { @SuppressWarnings("unchecked") final List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments(); for (final VariableDeclarationFragment fragment : fragments) { if (name.getFullyQualifiedName().equals(fragment.getName().getFullyQualifiedName())) { return createFieldAccessReplacement(level == 1 ? null : type, name); } } } } return findFieldReplacement(name, parent, level); } private FieldAccess createFieldAccessReplacement(final TypeDeclaration type, final SimpleName name) { final AST ast = name.getAST(); final FieldAccess fieldAccess = ast.newFieldAccess(); final ThisExpression thisExpr = ast.newThisExpression(); if (type != null) { thisExpr.setQualifier(copy(type.getName())); } fieldAccess.setExpression(thisExpr); fieldAccess.setName(copy(name)); return fieldAccess; } private Expression findMethodReplacement(final SimpleName name, ASTNode contextNode, final MethodInvocation node, int typeLevel) { int level = typeLevel; final ASTNode parent = contextNode.getParent(); if (parent instanceof TypeDeclaration) { level++; final TypeDeclaration type = (TypeDeclaration) parent; for (final MethodDeclaration methodDeclaration : type.getMethods()) { if (name.getFullyQualifiedName() .equals(methodDeclaration.getName().getFullyQualifiedName())) { return createMethodInvocationReplacement(level == 1 ? null : type, node); } } } return findMethodReplacement(name, parent, node, level); } private Expression createMethodInvocationReplacement(final TypeDeclaration type, MethodInvocation origMethodInvocation) { final AST ast = origMethodInvocation.getAST(); final MethodInvocation methodInvocation = copy(origMethodInvocation); final ThisExpression thisExpr = ast.newThisExpression(); if (type != null) { thisExpr.setQualifier(copy(type.getName())); } methodInvocation.setExpression(thisExpr); return methodInvocation; } }; }
From source file:org.eclipse.wb.tests.designer.core.AbstractJavaTest.java
License:Open Source License
/** * Checks several things about AST tree. <li>all {@link SimpleName}'s in given {@link AstEditor} * has valid source;</li> <li>variables have {@link IVariableBinding} with not-null * {@link ITypeBinding};</li> <li> * import, types and variable declaration have not-null {@link ITypeBinding};</li> <li>statements * of know types begin and start with known characters;</li> <br/> * Of course this does not mean that ALL is good, but this is better than nothing. ;-) *//*from w ww .j av a2 s .c o m*/ protected static void assertAST(final AstEditor editor) { final AST editorAST = editor.getAstUnit().getAST(); editor.getAstUnit().accept(new ASTVisitor(true) { @Override public void endVisit(SimpleName node) { assertEquals(node.getIdentifier(), getSource(node)); boolean isVariable = false; if (node.getParent() instanceof MethodInvocation) { MethodInvocation invocation = (MethodInvocation) node.getParent(); IMethodBinding binding = AstNodeUtils.getMethodBinding(invocation); if (!Modifier.isStatic(binding.getModifiers())) { isVariable |= invocation.getExpression() == node; isVariable |= invocation.arguments().contains(node); } } isVariable |= node.getParent() instanceof InfixExpression; if (isVariable) { IVariableBinding variableBinding = AstNodeUtils.getVariableBinding(node); assertNotNull(variableBinding); assertNotNull(variableBinding.getType()); } } @Override public void endVisit(TextElement node) { assertEquals(node.getText(), getSource(node)); } @Override public void endVisit(NumberLiteral node) { assertEquals(node.getToken(), getSource(node)); } @Override public void endVisit(VariableDeclarationFragment node) { SimpleName name = node.getName(); assertEquals(name.getStartPosition(), node.getStartPosition()); } @Override public void preVisit(ASTNode node) { assertSame(editorAST, node.getAST()); // check positions for parent/child { ASTNode parent = node.getParent(); if (parent != null) { int begin = AstNodeUtils.getSourceBegin(node); int end = AstNodeUtils.getSourceEnd(node); int parent_begin = AstNodeUtils.getSourceBegin(parent); int parent_end = AstNodeUtils.getSourceEnd(parent); assertTrue(begin >= parent_begin); assertTrue(end <= parent_end); } } // check bindings { if (node instanceof ImportDeclaration) { ImportDeclaration declaration = (ImportDeclaration) node; if (!declaration.isOnDemand()) { assertNotNull(AstNodeUtils.getTypeBinding(declaration.getName())); } } if (node instanceof Type) { Type type = (Type) node; assertNotNull(AstNodeUtils.getTypeBinding(type)); } if (node instanceof VariableDeclaration) { VariableDeclaration declaration = (VariableDeclaration) node; assertNotNull(AstNodeUtils.getTypeBinding(declaration.getName())); if (declaration.getInitializer() != null) { assertNotNull(AstNodeUtils.getTypeBinding(declaration.getInitializer())); } } if (node instanceof MethodInvocation) { MethodInvocation invocation = (MethodInvocation) node; IMethodBinding methodBinding = AstNodeUtils.getMethodBinding(invocation); assertNotNull(methodBinding); // check that names in MethodInvocation and IMethodBinding are same assertEquals(invocation.getName().getIdentifier(), methodBinding.getName()); // // check that types of parameters and arguments are compatible ITypeBinding[] parameterTypes = methodBinding.getParameterTypes(); List<?> arguments = invocation.arguments(); for (int i = 0; i < parameterTypes.length; i++) { ITypeBinding parameterType = parameterTypes[i]; if (i == parameterTypes.length - 1 && parameterType.isArray()) { // ellipsis support (last parameter is array) if (i == arguments.size()) { // ellipsis parameter skipped break; } { // check for exactly array Expression argument = (Expression) arguments.get(i); String argumentClassName = AstNodeUtils.getFullyQualifiedName(argument, false); if (AstNodeUtils.isSuccessorOf(parameterType, argumentClassName)) { // parameter is exactly array break; } } // check all other arguments ITypeBinding parameterElementType = parameterType.getElementType(); for (int j = i; j < arguments.size(); j++) { Expression argument = (Expression) arguments.get(j); String argumentClassName = AstNodeUtils.getFullyQualifiedName(argument, false); assertTrue(AstNodeUtils.isSuccessorOf(parameterElementType, argumentClassName)); } break; } Expression argument = (Expression) arguments.get(i); String argumentClassName = AstNodeUtils.getFullyQualifiedName(argument, false); // FIXME //assertTrue( AstNodeUtils.isSuccessorOf(parameterType, argumentClassName); // ); } } if (node instanceof MethodDeclaration) { MethodDeclaration declaration = (MethodDeclaration) node; IMethodBinding methodBinding = AstNodeUtils.getMethodBinding(declaration); assertNotNull(methodBinding); // check that names in MethodDeclaration and IMethodBinding are same assertEquals(declaration.getName().getIdentifier(), methodBinding.getName()); // check return type { Type returnType = declaration.getReturnType2(); if (returnType != null) { ITypeBinding bindingType = methodBinding.getReturnType(); ITypeBinding declarationType = AstNodeUtils.getTypeBinding(returnType); assertEqualTypes(bindingType, declarationType); } } // check parameters ITypeBinding[] bindingTypes = methodBinding.getParameterTypes(); List<SingleVariableDeclaration> parameters = DomGenerics.parameters(declaration); assertEquals(parameters.size(), bindingTypes.length); // check that types of parameters and arguments are same for (int i = 0; i < bindingTypes.length; i++) { ITypeBinding bindingType = bindingTypes[i]; SingleVariableDeclaration parameter = parameters.get(i); ITypeBinding parameterType = AstNodeUtils.getTypeBinding(parameter); assertEqualTypes(bindingType, parameterType); } } } // check source prefix/suffix { if (node instanceof ExpressionStatement || node instanceof VariableDeclarationStatement || node instanceof FieldDeclaration || node instanceof ImportDeclaration || node instanceof PackageDeclaration) { checkNodeSuffix(node, ";"); } if (node instanceof MethodInvocation) { checkNodeSuffix(node, ")"); } if (node instanceof ParenthesizedExpression) { checkNodePrefix(node, "("); checkNodeSuffix(node, ")"); } if (node instanceof Block) { checkNodePrefix(node, "{"); checkNodeSuffix(node, "}"); } if (node instanceof TypeDeclaration || node instanceof AnonymousClassDeclaration) { checkNodeSuffix(node, "}"); } if (node instanceof StringLiteral) { checkNodePrefix(node, "\""); checkNodeSuffix(node, "\""); } } // check Block: statements order if (node instanceof Block) { Block block = (Block) node; int lastStatementEnd = -1; for (Statement statement : DomGenerics.statements(block)) { assertTrue(lastStatementEnd <= statement.getStartPosition()); lastStatementEnd = AstNodeUtils.getSourceEnd(statement); } } // check TypeDeclaration: declarations order if (node instanceof TypeDeclaration) { TypeDeclaration typeDeclaration = (TypeDeclaration) node; int lastDeclarationEnd = -1; for (BodyDeclaration declaration : DomGenerics.bodyDeclarations(typeDeclaration)) { assertTrue(lastDeclarationEnd <= declaration.getStartPosition()); lastDeclarationEnd = AstNodeUtils.getSourceEnd(declaration); } } } private void assertEqualTypes(ITypeBinding bindingType, ITypeBinding parameterType) { String bindingTypeName = AstNodeUtils.getFullyQualifiedName(bindingType, false); String parameterTypeName = AstNodeUtils.getFullyQualifiedName(parameterType, false); assertEquals(bindingTypeName, parameterTypeName); } //////////////////////////////////////////////////////////////////////////// // // Checks // //////////////////////////////////////////////////////////////////////////// private void checkNodePrefix(ASTNode node, String prefix) { assertEquals(prefix, getSource(node.getStartPosition(), prefix.length())); } private void checkNodeSuffix(ASTNode node, String suffix) { int end = node.getStartPosition() + node.getLength(); assertEquals(suffix, getSource(end - suffix.length(), suffix.length())); } //////////////////////////////////////////////////////////////////////////// // // Source access // //////////////////////////////////////////////////////////////////////////// private String getSource(int start, int length) { try { return editor.getSource(start, length); } catch (Throwable e) { throw ReflectionUtils.propagate(e); } } private String getSource(ASTNode node) { return getSource(node.getStartPosition(), node.getLength()); } }); }
From source file:org.spoofax.interpreter.adapter.ecj.ECJFactory.java
License:LGPL
private SimpleName asSimpleName(IStrategoTerm term) { SimpleName x = ((WrappedSimpleName) term).getWrappee(); return x.getParent() == null && x.getAST() == ast ? x : (SimpleName) ASTNode.copySubtree(ast, x); }