org.codehaus.groovy.ast.Variable Java Examples
The following examples show how to use
org.codehaus.groovy.ast.Variable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessVariableRenamer.java From bonita-studio with GNU General Public License v2.0 | 6 votes |
@Override public void visitVariableExpression(final VariableExpression expression) { final Variable accessedVar = expression.getAccessedVariable(); // look for dynamic variables since the parameters already have the // new names, the actual references to the parameters are using the // old names if (accessedVar instanceof DynamicVariable) { final String newName = findReplacement(accessedVar.getName()); if (newName != null) { ReplaceEdit replaceEdit = new ReplaceEdit(expression.getStart(), expression.getLength(), newName); try { edits.addChild(replaceEdit); }catch (MalformedTreeException e) { BonitaStudioLog.error(e); } } } }
Example #2
Source File: GroovyInstantRenamer.java From netbeans with Apache License 2.0 | 6 votes |
@Override public boolean isRenameAllowed(ParserResult info, int caretOffset, String[] explanationRetValue) { LOG.log(Level.FINEST, "isRenameAllowed()"); //NOI18N final AstPath path = getPathUnderCaret(ASTUtils.getParseResult(info), caretOffset); if (path != null) { final ASTNode closest = path.leaf(); if (closest instanceof Variable) { final BaseDocument doc = LexUtilities.getDocument(ASTUtils.getParseResult(info), false); if (!FindTypeUtils.isCaretOnClassNode(path, doc, caretOffset)) { return true; } else { explanationRetValue[0] = NbBundle.getMessage(GroovyInstantRenamer.class, "NoInstantRenameOnClassNode"); return false; } } else { explanationRetValue[0] = NbBundle.getMessage(GroovyInstantRenamer.class, "OnlyRenameLocalVars"); return false; } } return false; }
Example #3
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void visitForLoop(ForStatement forLoop) { if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { addOccurrences(forLoop.getVariableType(), (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset)); } else { final Parameter forLoopVar = forLoop.getVariable(); final String varName = forLoopVar.getName(); if (leaf instanceof Variable) { if (varName.equals(((Variable) leaf).getName())) { occurrences.add(forLoopVar); } } else if (leaf instanceof ForStatement) { if (varName.equals(((ForStatement) leaf).getVariable().getName())) { occurrences.add(forLoopVar); } } } super.visitForLoop(forLoop); }
Example #4
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 6 votes |
private void addExpressionOccurrences(PropertyExpression expression) { final Expression property = expression.getProperty(); final String nodeAsString = expression.getPropertyAsString(); if (nodeAsString != null) { if (leaf instanceof Variable && nodeAsString.equals(((Variable) leaf).getName())) { occurrences.add(property); } else if (leaf instanceof ConstantExpression && leafParent instanceof PropertyExpression) { PropertyExpression propertyUnderCursor = (PropertyExpression) leafParent; if (nodeAsString.equals(propertyUnderCursor.getPropertyAsString())) { occurrences.add(property); } } } }
Example #5
Source File: FindMethodUsagesVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void visitMethodCallExpression(MethodCallExpression methodCall) { Expression expression = methodCall.getObjectExpression(); if (expression instanceof VariableExpression) { VariableExpression variableExpression = ((VariableExpression) expression); Variable variable = variableExpression.getAccessedVariable(); if (variable != null) { if (variable.isDynamicTyped()) { addDynamicVarUsages(methodCall, variable); } else { addStaticVarUsages(methodCall, variable); } } else { addThisUsages(methodCall); } } else if (expression instanceof ConstructorCallExpression) { addConstructorUsages(methodCall, (ConstructorCallExpression) expression); } super.visitMethodCallExpression(methodCall); }
Example #6
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 6 votes |
@Override public void visitVariableExpression(final VariableExpression expression) { super.visitVariableExpression(expression); Map<Variable, VariableState> state = getState(); Variable key = expression.getAccessedVariable(); if (key == null) { fixVar(expression); key = expression.getAccessedVariable(); } if (key != null && !key.isClosureSharedVariable() && callback != null) { VariableState variableState = state.get(key); if ((inAssignmentRHS || inArgumentList) && (variableState == VariableState.is_uninitialized || variableState == VariableState.is_ambiguous)) { callback.variableNotAlwaysInitialized(expression); } } }
Example #7
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void visitParameters(Parameter[] parameters, Variable variable) { // method is declaring given variable, let's visit only the method, // but we need to check also parameters as those are not part of method visit for (Parameter parameter : parameters) { ClassNode paramType = parameter.getType(); if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { addOccurrences(paramType, (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset)); } else { if (parameter.getName().equals(variable.getName())) { occurrences.add(parameter); break; } } } super.visitParameters(parameters, variable); }
Example #8
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void visitArrayExpression(ArrayExpression visitedArray) { final ClassNode visitedType = visitedArray.getElementType(); final String visitedName = ElementUtils.getTypeName(visitedType); if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { ASTNode currentNode = FindTypeUtils.findCurrentNode(path, doc, cursorOffset); addOccurrences(visitedType, (ClassNode) currentNode); } else if (leaf instanceof Variable) { String varName = removeParentheses(((Variable) leaf).getName()); if (varName.equals(visitedName)) { occurrences.add(new FakeASTNode(visitedType, visitedName)); } } else if (leaf instanceof ConstantExpression && leafParent instanceof PropertyExpression) { if (visitedName.equals(((PropertyExpression) leafParent).getPropertyAsString())) { occurrences.add(new FakeASTNode(visitedType, visitedName)); } } super.visitArrayExpression(visitedArray); }
Example #9
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 6 votes |
private void checkFinalFieldAccess(final Expression expression) { BiConsumer<VariableExpression, ASTNode> checkForFinal = (expr, node) -> { Variable variable = expr.getAccessedVariable(); if (variable != null) { if (isFinal(variable.getModifiers()) && variable instanceof Parameter) { addError("Cannot assign a value to final variable '" + variable.getName() + "'", node); } // TODO: handle local variables } }; if (expression instanceof VariableExpression) { checkForFinal.accept((VariableExpression) expression, expression); } else if (expression instanceof TupleExpression) { TupleExpression tuple = (TupleExpression) expression; for (Expression tupleExpression : tuple.getExpressions()) { checkForFinal.accept((VariableExpression) tupleExpression, expression); } } // currently not looking for PropertyExpression: dealt with at runtime using ReadOnlyPropertyException }
Example #10
Source File: GroovyLanguageServerUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static CompletionItemKind astNodeToCompletionItemKind(ASTNode node) { if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; if (classNode.isInterface()) { return CompletionItemKind.Interface; } else if (classNode.isEnum()) { return CompletionItemKind.Enum; } return CompletionItemKind.Class; } else if (node instanceof MethodNode) { return CompletionItemKind.Method; } else if (node instanceof Variable) { if (node instanceof FieldNode || node instanceof PropertyNode) { return CompletionItemKind.Field; } return CompletionItemKind.Variable; } return CompletionItemKind.Property; }
Example #11
Source File: BinaryExpressionMultiTypeDispatcher.java From groovy with Apache License 2.0 | 6 votes |
private boolean doAssignmentToLocalVariable(final String method, final BinaryExpression binExp) { Expression left = binExp.getLeftExpression(); if (left instanceof VariableExpression) { VariableExpression ve = (VariableExpression) left; Variable v = ve.getAccessedVariable(); if (v instanceof DynamicVariable) return false; if (v instanceof PropertyExpression) return false; /* field and declaration we don't return false */ } else { return false; } evaluateBinaryExpression(method, binExp); controller.getOperandStack().dup(); controller.getCompileStack().pushLHS(true); binExp.getLeftExpression().visit(controller.getAcg()); controller.getCompileStack().popLHS(); return true; }
Example #12
Source File: GroovyLanguageServerUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static SymbolKind astNodeToSymbolKind(ASTNode node) { if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; if (classNode.isInterface()) { return SymbolKind.Interface; } else if (classNode.isEnum()) { return SymbolKind.Enum; } return SymbolKind.Class; } else if (node instanceof MethodNode) { return SymbolKind.Method; } else if (node instanceof Variable) { if (node instanceof FieldNode || node instanceof PropertyNode) { return SymbolKind.Field; } return SymbolKind.Variable; } return SymbolKind.Property; }
Example #13
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitIfElse(final IfStatement ifElse) { visitStatement(ifElse); ifElse.getBooleanExpression().visit(this); Map<Variable, VariableState> ifState = pushState(); ifElse.getIfBlock().visit(this); popState(); Map<Variable, VariableState> elseState = pushState(); ifElse.getElseBlock().visit(this); popState(); // merge if/else branches Map<Variable, VariableState> curState = getState(); Set<Variable> allVars = new HashSet<>(); allVars.addAll(curState.keySet()); allVars.addAll(ifState.keySet()); allVars.addAll(elseState.keySet()); for (Variable var : allVars) { VariableState beforeValue = curState.get(var); if (beforeValue != null) { VariableState ifValue = ifState.get(var); VariableState elseValue = elseState.get(var); if (ifValue == elseValue) { curState.put(var, ifValue); } else { curState.put(var, beforeValue == VariableState.is_uninitialized ? VariableState.is_ambiguous : VariableState.is_var); } } } }
Example #14
Source File: StatementMetaTypeChooser.java From groovy with Apache License 2.0 | 5 votes |
@Override public ClassNode resolveType(final Expression exp, final ClassNode current) { ClassNode type = null; if (exp instanceof ClassExpression) { type = exp.getType(); ClassNode classType = ClassHelper.makeWithoutCaching("java.lang.Class"); classType.setGenericsTypes(new GenericsType[] {new GenericsType(type)}); classType.setRedirect(ClassHelper.CLASS_Type); return classType; } OptimizingStatementWriter.StatementMeta meta = exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class); if (meta != null) type = meta.type; if (type != null) return type; if (exp instanceof VariableExpression) { VariableExpression ve = (VariableExpression) exp; if (ve.isClosureSharedVariable()) return ve.getType(); if (ve.isSuperExpression()) return current.getSuperClass(); type = ve.getOriginType(); } else if (exp instanceof Variable) { Variable v = (Variable) exp; type = v.getOriginType(); } else { type = exp.getType(); } return type.redirect(); }
Example #15
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { String name = expression.getFieldName(); //TODO: change that to get the correct scope Variable variable = findVariableDeclaration(name); checkVariableContextAccess(variable, expression); }
Example #16
Source File: ClosureWriter.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitVariableExpression(final VariableExpression expression) { Variable v = expression.getAccessedVariable(); if (v == null) return; if (!(v instanceof FieldNode)) return; String name = expression.getName(); FieldNode fn = icn.getDeclaredField(name); if (fn != null) { // only overwrite if we find something more specific expression.setAccessedVariable(fn); } }
Example #17
Source File: ConstraintInputIndexer.java From bonita-studio with GNU General Public License v2.0 | 5 votes |
protected void addRefrencedInputs(final BlockStatement blockStatement) { final Iterator<Variable> referencedClassVariablesIterator = blockStatement.getVariableScope().getReferencedClassVariablesIterator(); while (referencedClassVariablesIterator.hasNext()) { final Variable variable = referencedClassVariablesIterator.next(); for (final ContractInput in : inputs) { if (in.getName().equals(variable.getName())) { referencedInputs.add(variable.getName()); } } } }
Example #18
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitMethodCallExpression(final MethodCallExpression expression) { if (expression.isImplicitThis() && expression.getMethod() instanceof ConstantExpression) { ConstantExpression methodNameConstant = (ConstantExpression) expression.getMethod(); String methodName = methodNameConstant.getText(); if (methodName == null) { throw new GroovyBugError("method name is null"); } Variable variable = findVariableDeclaration(methodName); if (variable != null && !(variable instanceof DynamicVariable)) { checkVariableContextAccess(variable, expression); } if (variable instanceof VariableExpression || variable instanceof Parameter) { VariableExpression object = new VariableExpression(variable); object.setSourcePosition(methodNameConstant); expression.setObjectExpression(object); ConstantExpression method = new ConstantExpression("call"); method.setSourcePosition(methodNameConstant); // important for GROOVY-4344 expression.setImplicitThis(false); expression.setMethod(method); } } super.visitMethodCallExpression(expression); }
Example #19
Source File: CompileStack.java From groovy with Apache License 2.0 | 5 votes |
public BytecodeVariable defineVariable(final Variable v, final ClassNode variableType, final boolean initFromStack) { String name = v.getName(); BytecodeVariable answer = defineVar(name, variableType, v.isClosureSharedVariable(), v.isClosureSharedVariable()); stackVariables.put(name, answer); MethodVisitor mv = controller.getMethodVisitor(); Label startLabel = new Label(); answer.setStartLabel(startLabel); ClassNode type = answer.getType().redirect(); OperandStack operandStack = controller.getOperandStack(); if (!initFromStack) { if (ClassHelper.isPrimitiveType(v.getOriginType()) && ClassHelper.getWrapper(v.getOriginType()) == variableType) { pushInitValue(v.getOriginType(), mv); operandStack.push(v.getOriginType()); operandStack.box(); operandStack.remove(1); } else { pushInitValue(type, mv); } } operandStack.push(answer.getType()); if (answer.isHolder()) { operandStack.box(); operandStack.remove(1); createReference(answer); } else { operandStack.storeVar(answer); } mv.visitLabel(startLabel); return answer; }
Example #20
Source File: BinaryExpression.java From groovy with Apache License 2.0 | 5 votes |
/** * Creates an assignment expression in which the specified expression * is written into the specified variable name. */ public static BinaryExpression newAssignmentExpression(Variable variable, Expression rhs) { VariableExpression lhs = new VariableExpression(variable); Token operator = Token.newPlaceholder(Types.ASSIGN); return new BinaryExpression(lhs, operator, rhs); }
Example #21
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
private void visitCatchFinally(Map<Variable, VariableState> initialVarState, List<Map<Variable, VariableState>> afterTryCatchStates, CatchStatement catchStatement, Statement finallyStatement) { pushState(); getState().putAll(initialVarState); Statement code = catchStatement.getCode(); catchStatement.visit(this); finallyStatement.visit(this); if (code == null || !returningBlock(code)) { afterTryCatchStates.add(new HashMap<>(getState())); } popState(); }
Example #22
Source File: VariablesVisitor.java From bonita-studio with GNU General Public License v2.0 | 5 votes |
private boolean inVariableScope(final VariableExpression expression) { final Variable matchInDeclaredVariables = find(variableScope .getDeclaredVariablesIterator(), variableWithName(expression.getName()), null); final Variable matchInReferencedClassVariables = find(variableScope .getReferencedClassVariablesIterator(), variableWithName(expression.getName()), null); return matchInDeclaredVariables != null || matchInReferencedClassVariables != null; }
Example #23
Source File: GroovyNodeToStringUtils.java From groovy-language-server with Apache License 2.0 | 5 votes |
public static String variableToString(Variable variable, ASTNodeVisitor ast) { StringBuilder builder = new StringBuilder(); if (variable instanceof FieldNode) { FieldNode fieldNode = (FieldNode) variable; if (fieldNode.isPublic()) { builder.append("public "); } if (fieldNode.isProtected()) { builder.append("protected "); } if (fieldNode.isPrivate()) { builder.append("private "); } if (fieldNode.isFinal()) { builder.append("final "); } if (fieldNode.isStatic()) { builder.append("static "); } } ClassNode varType = null; if (variable instanceof ASTNode) { varType = GroovyASTUtils.getTypeOfNode((ASTNode) variable, ast); } else { varType = variable.getType(); } builder.append(varType.getNameWithoutPackage()); builder.append(" "); builder.append(variable.getName()); return builder.toString(); }
Example #24
Source File: VariablesVisitor.java From bonita-studio with GNU General Public License v2.0 | 5 votes |
private Predicate<Variable> variableWithName(final String name) { return new Predicate<Variable>() { @Override public boolean apply(final Variable input) { return Objects.equals(name, input.getName()); } }; }
Example #25
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
private static Variable getTarget(Variable v) { if (v instanceof VariableExpression) { Variable t = ((VariableExpression) v).getAccessedVariable(); if (t == v) return t; return getTarget(t); } return v; }
Example #26
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitBlockStatement(final BlockStatement block) { Set<Variable> old = declaredFinalVariables; declaredFinalVariables = new HashSet<>(); super.visitBlockStatement(block); declaredFinalVariables = old; }
Example #27
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
private void checkPrePostfixOperation(final Expression variable, final Expression originalExpression) { if (variable instanceof Variable) { recordAssignment((Variable) variable, false, false, true, originalExpression); if (variable instanceof VariableExpression) { Variable accessed = ((VariableExpression) variable).getAccessedVariable(); if (accessed != variable) { recordAssignment(accessed, false, false, true, originalExpression); } } } }
Example #28
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
private void cleanLocalVars(Map<Variable, VariableState> origState, Map<Variable, VariableState> state) { // clean local vars added during visit of closure for (Iterator<Map.Entry<Variable, VariableState>> iter = state.entrySet().iterator(); iter.hasNext(); ) { Map.Entry<Variable, VariableState> next = iter.next(); Variable key = next.getKey(); if (key instanceof VariableExpression && ((VariableExpression)key).getAccessedVariable() == key && !origState.containsKey(key)) { // remove local variable iter.remove(); } } }
Example #29
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitClosureExpression(final ClosureExpression expression) { boolean old = inAssignmentRHS; inAssignmentRHS = false; Map<Variable, VariableState> origState = new StateMap(); origState.putAll(getState()); super.visitClosureExpression(expression); cleanLocalVars(origState, getState()); inAssignmentRHS = old; }
Example #30
Source File: FinalVariableAnalyzer.java From groovy with Apache License 2.0 | 5 votes |
private void recordFinalVars(Expression leftExpression) { if (leftExpression instanceof VariableExpression) { VariableExpression var = (VariableExpression) leftExpression; if (Modifier.isFinal(var.getModifiers())) { declaredFinalVariables.add(var); } } else if (leftExpression instanceof TupleExpression) { TupleExpression te = (TupleExpression) leftExpression; for (Expression next : te.getExpressions()) { if (next instanceof Variable) { declaredFinalVariables.add((Variable) next); } } } }