/** {@inheritDoc} */ @Override protected EclipseNode buildTree(ASTNode node, Kind kind) { switch (kind) { case COMPILATION_UNIT: return buildCompilationUnit((CompilationUnitDeclaration) node); case TYPE: return buildType((TypeDeclaration) node); case FIELD: return buildField((FieldDeclaration) node); case INITIALIZER: return buildInitializer((Initializer) node); case METHOD: return buildMethod((AbstractMethodDeclaration) node); case ARGUMENT: return buildLocal((Argument) node, kind); case LOCAL: return buildLocal((LocalDeclaration) node, kind); case STATEMENT: return buildStatement((Statement) node); case ANNOTATION: return buildAnnotation((Annotation) node, false); default: throw new AssertionError("Did not expect to arrive here: " + kind); } }
private void removeLocals(Statement[] statements, int start, int end) { if (statements != null) { for (int i = 0; i < statements.length; i++) { if (statements[i] instanceof LocalDeclaration) { LocalDeclaration localDeclaration = (LocalDeclaration) statements[i]; int j = indexOfFisrtNameAfter(start); done : while (j != -1) { int nameStart = this.potentialVariableNameStarts[j]; if (start <= nameStart && nameStart <= end) { if (CharOperation.equals(this.potentialVariableNames[j], localDeclaration.name, false)) { removeNameAt(j); } } if (end < nameStart) break done; j = indexOfNextName(j); } } } } }
protected void checkAndAddMultipleLocalDeclaration(org.eclipse.jdt.internal.compiler.ast.Statement[] stmts, int index, List blockStatements) { if (index > 0 && stmts[index - 1] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { org.eclipse.jdt.internal.compiler.ast.LocalDeclaration local1 = (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) stmts[index - 1]; org.eclipse.jdt.internal.compiler.ast.LocalDeclaration local2 = (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) stmts[index]; if (local1.declarationSourceStart == local2.declarationSourceStart) { // we have a multiple local declarations // We retrieve the existing VariableDeclarationStatement to add the new VariableDeclarationFragment VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) blockStatements.get(blockStatements.size() - 1); variableDeclarationStatement.fragments().add(convertToVariableDeclarationFragment((org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)stmts[index])); } else { // we can create a new FieldDeclaration blockStatements.add(convertToVariableDeclarationStatement((org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)stmts[index])); } } else { // we can create a new FieldDeclaration blockStatements.add(convertToVariableDeclarationStatement((org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)stmts[index])); } }
public Block convert(org.eclipse.jdt.internal.compiler.ast.Block statement) { Block block = new Block(this.ast); if (statement.sourceEnd > 0) { block.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1); } org.eclipse.jdt.internal.compiler.ast.Statement[] statements = statement.statements; if (statements != null) { int statementsLength = statements.length; for (int i = 0; i < statementsLength; i++) { if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { checkAndAddMultipleLocalDeclaration(statements, i, block.statements()); } else { Statement statement2 = convert(statements[i]); if (statement2 != null) { block.statements().add(statement2); } } } } return block; }
public SwitchStatement convert(org.eclipse.jdt.internal.compiler.ast.SwitchStatement statement) { SwitchStatement switchStatement = new SwitchStatement(this.ast); switchStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1); switchStatement.setExpression(convert(statement.expression)); org.eclipse.jdt.internal.compiler.ast.Statement[] statements = statement.statements; if (statements != null) { int statementsLength = statements.length; for (int i = 0; i < statementsLength; i++) { if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { checkAndAddMultipleLocalDeclaration(statements, i, switchStatement.statements()); } else { final Statement currentStatement = convert(statements[i]); if (currentStatement != null) { switchStatement.statements().add(currentStatement); } } } } return switchStatement; }
synchronized IVariableBinding resolveVariable(VariableDeclaration variable) { final Object node = this.newAstToOldAst.get(variable); if (node instanceof AbstractVariableDeclaration) { AbstractVariableDeclaration abstractVariableDeclaration = (AbstractVariableDeclaration) node; IVariableBinding variableBinding = null; if (abstractVariableDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) { org.eclipse.jdt.internal.compiler.ast.FieldDeclaration fieldDeclaration = (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) abstractVariableDeclaration; variableBinding = this.getVariableBinding(fieldDeclaration.binding, variable); } else { variableBinding = this.getVariableBinding(((LocalDeclaration) abstractVariableDeclaration).binding, variable); } if (variableBinding == null) { return null; } this.bindingsToAstNodes.put(variableBinding, variable); String key = variableBinding.getKey(); if (key != null) { this.bindingTables.bindingKeysToBindings.put(key, variableBinding); } return variableBinding; } return null; }
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) { /* do not consider a type starting passed the type end (if set) it must be belonging to an enclosing type */ if (this.fieldDeclaration.declarationSourceEnd != 0 && localDeclaration.declarationSourceStart > this.fieldDeclaration.declarationSourceEnd){ resetPendingModifiers(); if (this.parent == null) return this; // ignore return this.parent.add(localDeclaration, bracketBalanceValue); } /* method body should have been created */ Block block = new Block(0); block.sourceStart = ((Initializer)this.fieldDeclaration).sourceStart; RecoveredElement element = this.add(block, 1); if (this.initializerBody != null) { this.initializerBody.attachPendingModifiers( this.pendingAnnotations, this.pendingAnnotationCount, this.pendingModifiers, this.pendingModifersSourceStart); } resetPendingModifiers(); return element.add(localDeclaration, bracketBalanceValue); }
public void unhandledException(TypeBinding exceptionType, ASTNode location) { boolean insideDefaultConstructor = (this.referenceContext instanceof ConstructorDeclaration) && ((ConstructorDeclaration)this.referenceContext).isDefaultConstructor(); boolean insideImplicitConstructorCall = (location instanceof ExplicitConstructorCall) && (((ExplicitConstructorCall) location).accessMode == ExplicitConstructorCall.ImplicitSuper); int sourceEnd = location.sourceEnd; if (location instanceof LocalDeclaration) { sourceEnd = ((LocalDeclaration) location).declarationEnd; } this.handle( insideDefaultConstructor ? IProblem.UnhandledExceptionInDefaultConstructor : (insideImplicitConstructorCall ? IProblem.UndefinedConstructorInImplicitConstructorCall : IProblem.UnhandledException), new String[] {new String(exceptionType.readableName())}, new String[] {new String(exceptionType.shortReadableName())}, location.sourceStart, sourceEnd); }
public void unreachableCode(Statement statement) { int sourceStart = statement.sourceStart; int sourceEnd = statement.sourceEnd; if (statement instanceof LocalDeclaration) { LocalDeclaration declaration = (LocalDeclaration) statement; sourceStart = declaration.declarationSourceStart; sourceEnd = declaration.declarationSourceEnd; } else if (statement instanceof Expression) { int statemendEnd = ((Expression) statement).statementEnd; if (statemendEnd != -1) sourceEnd = statemendEnd; } this.handle( IProblem.CodeCannotBeReached, NoArgument, NoArgument, sourceStart, sourceEnd); }
public void acceptLocalVariable(LocalVariableBinding binding) { LocalDeclaration local = binding.declaration; IJavaElement parent = findLocalElement(local.sourceStart); // findLocalElement() cannot find local variable LocalVariable localVar = null; if(parent != null) { localVar = new LocalVariable( (JavaElement)parent, new String(local.name), local.declarationSourceStart, local.declarationSourceEnd, local.sourceStart, local.sourceEnd, Util.typeSignature(local.type), local.annotations, local.modifiers, local.getKind() == AbstractVariableDeclaration.PARAMETER); } if (localVar != null) { addElement(localVar); if(SelectionEngine.DEBUG){ System.out.print("SELECTION - accept local variable("); //$NON-NLS-1$ System.out.print(localVar.toString()); System.out.println(")"); //$NON-NLS-1$ } } }
private JLocal createLocal(LocalDeclaration x) { LocalVariableBinding b = x.binding; TypeBinding resolvedType = x.type.resolvedType; JType localType; if (resolvedType.constantPoolName() != null) { localType = typeMap.get(resolvedType); } else { // Special case, a statically unreachable local type. localType = JNullType.INSTANCE; } SourceInfo info = makeSourceInfo(x); JLocal newLocal = JProgram.createLocal(info, intern(x.name), localType, b.isFinal(), curMethod.body); curMethod.locals.put(b, newLocal); return newLocal; }
@Override public void visitLocal(EclipseNode localNode, LocalDeclaration local) { if (!EclipseHandlerUtil.typeMatches(val.class, localNode, local.type)) return; boolean variableOfForEach = false; if (localNode.directUp().get() instanceof ForeachStatement) { ForeachStatement fs = (ForeachStatement) localNode.directUp().get(); variableOfForEach = fs.elementVariable == local; } if (local.initialization == null && !variableOfForEach) { localNode.addError("'val' on a local variable requires an initializer expression"); return; } if (local.initialization instanceof ArrayInitializer) { localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })"); return; } if (localNode.directUp().get() instanceof ForStatement) { localNode.addError("'val' is not allowed in old-style for loops"); return; } }
private Collection<EclipseNode> buildArguments(Argument[] children) { List<EclipseNode> childNodes = new ArrayList<EclipseNode>(); if (children != null) for (LocalDeclaration local : children) { addIfNotNull(childNodes, buildLocal(local, Kind.ARGUMENT)); } return childNodes; }
private EclipseNode buildLocal(LocalDeclaration local, Kind kind) { if (setAndGetAsHandled(local)) return null; List<EclipseNode> childNodes = new ArrayList<EclipseNode>(); addIfNotNull(childNodes, buildStatement(local.initialization)); childNodes.addAll(buildAnnotations(local.annotations, true)); return putInMap(new EclipseNode(this, local, childNodes, kind)); }
@Override public void visitLocal(EclipseNode localNode, LocalDeclaration local) { if (!EclipseHandlerUtil.typeMatches(val.class, localNode, local.type)) return; handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val"); boolean variableOfForEach = false; if (localNode.directUp().get() instanceof ForeachStatement) { ForeachStatement fs = (ForeachStatement) localNode.directUp().get(); variableOfForEach = fs.elementVariable == local; } if (local.initialization == null && !variableOfForEach) { localNode.addError("'val' on a local variable requires an initializer expression"); return; } if (local.initialization instanceof ArrayInitializer) { localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })"); return; } if (localNode.directUp().get() instanceof ForStatement) { localNode.addError("'val' is not allowed in old-style for loops"); return; } if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) { localNode.addError("'val' is not allowed with lambda expressions."); } }
public LocalDeclaration createLocalDeclaration(ASTNode source, char[] dollarFieldName, TypeReference type, Expression initializer) { int pS = source.sourceStart, pE = source.sourceEnd; LocalDeclaration tempVar = new LocalDeclaration(dollarFieldName, pS, pE); setGeneratedBy(tempVar, source); tempVar.initialization = initializer; tempVar.type = type; tempVar.type.sourceStart = pS; tempVar.type.sourceEnd = pE; setGeneratedBy(tempVar.type, source); tempVar.modifiers = Modifier.FINAL; return tempVar; }
private void doAssignmentCheck0(EclipseNode node, Statement statement, char[] varName) { if (statement instanceof Assignment) doAssignmentCheck0(node, ((Assignment)statement).expression, varName); else if (statement instanceof LocalDeclaration) doAssignmentCheck0(node, ((LocalDeclaration)statement).initialization, varName); else if (statement instanceof CastExpression) doAssignmentCheck0(node, ((CastExpression)statement).expression, varName); else if (statement instanceof SingleNameReference) { if (Arrays.equals(((SingleNameReference)statement).token, varName)) { EclipseNode problemNode = node.getNodeFor(statement); if (problemNode != null) problemNode.addWarning( "You're assigning an auto-cleanup variable to something else. This is a bad idea."); } } }
/** {@inheritDoc} */ @Override public String getName() { final char[] n; if (node instanceof TypeDeclaration) n = ((TypeDeclaration)node).name; else if (node instanceof FieldDeclaration) n = ((FieldDeclaration)node).name; else if (node instanceof AbstractMethodDeclaration) n = ((AbstractMethodDeclaration)node).selector; else if (node instanceof LocalDeclaration) n = ((LocalDeclaration)node).name; else n = null; return n == null ? null : new String(n); }
/** {@inheritDoc} */ @Override protected boolean calculateIsStructurallySignificant(ASTNode parent) { if (node instanceof TypeDeclaration) return true; if (node instanceof AbstractMethodDeclaration) return true; if (node instanceof FieldDeclaration) return true; if (node instanceof LocalDeclaration) return true; if (node instanceof CompilationUnitDeclaration) return true; return false; }
@Override @Nullable public ResolvedNode resolve(@NonNull JavaContext context, @NonNull Node node) { Object nativeNode = getNativeNode(node); if (nativeNode == null) { return null; } if (nativeNode instanceof NameReference) { return resolve(((NameReference) nativeNode).binding); } else if (nativeNode instanceof TypeReference) { return resolve(((TypeReference) nativeNode).resolvedType); } else if (nativeNode instanceof MessageSend) { return resolve(((MessageSend) nativeNode).binding); } else if (nativeNode instanceof AllocationExpression) { return resolve(((AllocationExpression) nativeNode).binding); } else if (nativeNode instanceof TypeDeclaration) { return resolve(((TypeDeclaration) nativeNode).binding); } else if (nativeNode instanceof ExplicitConstructorCall) { return resolve(((ExplicitConstructorCall) nativeNode).binding); } else if (nativeNode instanceof Annotation) { return resolve(((Annotation) nativeNode).resolvedType); } else if (nativeNode instanceof AbstractMethodDeclaration) { return resolve(((AbstractMethodDeclaration) nativeNode).binding); } else if (nativeNode instanceof AbstractVariableDeclaration) { if (nativeNode instanceof LocalDeclaration) { return resolve(((LocalDeclaration) nativeNode).binding); } else if (nativeNode instanceof FieldDeclaration) { return resolve(((FieldDeclaration) nativeNode).binding); } } // TODO: Handle org.eclipse.jdt.internal.compiler.ast.SuperReference. It // doesn't contain an actual method binding; the parent node call should contain // it, but is missing a native node reference; investigate the ECJ bridge's super // handling. return null; }
public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) { if (decl != null && LocalDeclaration.class.equals(decl.getClass()) && expr.resolvedType != null) return expr.resolvedType; try { return expr.resolveType(scope); } catch (NullPointerException e) { return null; } }
private JavaElement getJavaElement(LocalVariableBinding binding) { LocalDeclaration local = binding.declaration; JavaElement parent = null; ReferenceContext referenceContext = binding.declaringScope.referenceContext(); if (referenceContext instanceof AbstractMethodDeclaration) { AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) referenceContext; parent = this.getJavaElementOfCompilationUnit(methodDeclaration, methodDeclaration.binding); } else if (referenceContext instanceof TypeDeclaration){ // Local variable is declared inside an initializer TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext; JavaElement type = this.getJavaElementOfCompilationUnit(typeDeclaration, typeDeclaration.binding); parent = Util.getUnresolvedJavaElement(local.sourceStart, local.sourceEnd, type); } if (parent == null) return null; return new LocalVariable( parent, new String(local.name), local.declarationSourceStart, local.declarationSourceEnd, local.sourceStart, local.sourceEnd, Util.typeSignature(local.type), binding.declaration.annotations, local.modifiers, local.getKind() == AbstractVariableDeclaration.PARAMETER); }
protected void consumeLocalVariableDeclarationStatement() { super.consumeLocalVariableDeclarationStatement(); // force to restart in recovery mode if the declaration contains the selection if (!this.diet) { LocalDeclaration localDeclaration = (LocalDeclaration) this.astStack[this.astPtr]; if ((this.selectionStart >= localDeclaration.sourceStart) && (this.selectionEnd <= localDeclaration.sourceEnd)) { this.restartRecovery = true; this.lastIgnoredToken = -1; } } }