@Override public Void visitClassTypeAlias(ClassTypeAlias node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier className = node.getName(); ClassElementImpl element = new ClassElementImpl(className); element.setAbstract(node.getAbstractKeyword() != null); TypeVariableElement[] typeVariables = holder.getTypeVariables(); element.setTypeVariables(typeVariables); InterfaceTypeImpl interfaceType = new InterfaceTypeImpl(element); int typeVariableCount = typeVariables.length; Type[] typeArguments = new Type[typeVariableCount]; for (int i = 0; i < typeVariableCount; i++) { TypeVariableElementImpl typeVariable = (TypeVariableElementImpl) typeVariables[i]; TypeVariableTypeImpl typeArgument = new TypeVariableTypeImpl(typeVariable); typeVariable.setType(typeArgument); typeArguments[i] = typeArgument; } interfaceType.setTypeArguments(typeArguments); element.setType(interfaceType); currentHolder.addType(element); className.setElement(element); return null; }
@Override public Void visitClassTypeAlias(ClassTypeAlias node) { ElementHolder holder = new ElementHolder(); functionTypesToFix = new ArrayList<FunctionTypeImpl>(); visitChildren(holder, node); SimpleIdentifier className = node.getName(); ClassElementImpl element = new ClassElementImpl(className); element.setAbstract(node.getAbstractKeyword() != null); element.setTypedef(true); TypeParameterElement[] typeParameters = holder.getTypeParameters(); element.setTypeParameters(typeParameters); Type[] typeArguments = createTypeParameterTypes(typeParameters); InterfaceTypeImpl interfaceType = new InterfaceTypeImpl(element); interfaceType.setTypeArguments(typeArguments); element.setType(interfaceType); // set default constructor element.setConstructors(createDefaultConstructors(interfaceType)); for (FunctionTypeImpl functionType : functionTypesToFix) { functionType.setTypeArguments(typeArguments); } functionTypesToFix = null; currentHolder.addType(element); className.setStaticElement(element); holder.validate(); return null; }
@Override public Void visitTypeParameter(TypeParameter node) { SimpleIdentifier parameterName = node.getName(); TypeVariableElementImpl element = new TypeVariableElementImpl(parameterName); currentHolder.addTypeVariable(element); parameterName.setElement(element); return null; }
@Override public Void visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { SimpleIdentifier parameterName = node.getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setParameterKind(node.getKind()); currentHolder.addParameter(parameter); parameterName.setElement(parameter); return null; }
@Override public Void visitSwitchCase(SwitchCase node) { for (Label label : node.getLabels()) { SimpleIdentifier labelName = label.getLabel(); LabelElementImpl element = new LabelElementImpl(labelName, false, true); currentHolder.addLabel(element); labelName.setStaticElement(element); } return super.visitSwitchCase(node); }
@Override public Void visitVariableDeclaration(VariableDeclaration node) { VariableElementImpl element; if (inFieldContext) { SimpleIdentifier fieldName = node.getName(); element = new FieldElementImpl(fieldName); currentHolder.addField((FieldElementImpl) element); fieldName.setElement(element); } else { SimpleIdentifier variableName = node.getName(); element = new VariableElementImpl(variableName); currentHolder.addVariable(element); variableName.setElement(element); } Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword(); boolean isFinal = matches(keyword, Keyword.FINAL); element.setConst(matches(keyword, Keyword.CONST)); element.setFinal(isFinal); if (node.getInitializer() != null) { ElementHolder holder = new ElementHolder(); boolean wasInFieldContext = inFieldContext; inFieldContext = false; try { visitChildren(holder, node.getInitializer()); } finally { inFieldContext = wasInFieldContext; } FunctionElementImpl initializer = new FunctionElementImpl(); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getVariables()); initializer.setSynthetic(true); element.setInitializer(initializer); } if (inFieldContext) { FieldElementImpl field = (FieldElementImpl) element; PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(field); getter.setGetter(true); field.setGetter(getter); if (!isFinal) { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(field); setter.setSetter(true); field.setSetter(setter); } field.setStatic( matches(((FieldDeclaration) node.getParent().getParent()).getKeyword(), Keyword.STATIC)); } return null; }
@Override public Void visitSwitchDefault(SwitchDefault node) { for (Label label : node.getLabels()) { SimpleIdentifier labelName = label.getLabel(); LabelElementImpl element = new LabelElementImpl(labelName, false, true); currentHolder.addLabel(element); labelName.setElement(element); } return null; }
@Override public Void visitTypeParameter(TypeParameter node) { SimpleIdentifier parameterName = node.getName(); TypeParameterElementImpl typeParameter = new TypeParameterElementImpl(parameterName); TypeParameterTypeImpl typeParameterType = new TypeParameterTypeImpl(typeParameter); typeParameter.setType(typeParameterType); currentHolder.addTypeParameter(typeParameter); parameterName.setStaticElement(typeParameter); return super.visitTypeParameter(node); }
@Override public Void visitSimpleFormalParameter(SimpleFormalParameter node) { SimpleIdentifier parameterName = node.getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setParameterKind(node.getKind()); currentHolder.addParameter(parameter); parameterName.setElement(parameter); return null; }
@Override public Void visitLabeledStatement(LabeledStatement node) { boolean onSwitchStatement = node.getStatement() instanceof SwitchStatement; for (Label label : node.getLabels()) { SimpleIdentifier labelName = label.getLabel(); LabelElementImpl element = new LabelElementImpl(labelName, onSwitchStatement, false); currentHolder.addLabel(element); labelName.setElement(element); } return null; }
@Override public Void visitSimpleFormalParameter(SimpleFormalParameter node) { if (!(node.getParent() instanceof DefaultFormalParameter)) { SimpleIdentifier parameterName = node.getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setParameterKind(node.getKind()); setParameterVisibleRange(node, parameter); currentHolder.addParameter(parameter); parameterName.setStaticElement(parameter); } return super.visitSimpleFormalParameter(node); }
@Override public Void visitFunctionDeclaration(FunctionDeclaration node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier functionName = node.getName(); FunctionElementImpl element = new FunctionElementImpl(functionName); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); element.setParameters(holder.getParameters()); currentHolder.addFunction(element); functionName.setElement(element); return null; }
@Override public Void visitDeclaredIdentifier(DeclaredIdentifier node) { SimpleIdentifier variableName = node.getIdentifier(); Token keyword = node.getKeyword(); LocalVariableElementImpl element = new LocalVariableElementImpl(variableName); ForEachStatement statement = (ForEachStatement) node.getParent(); int declarationEnd = node.getOffset() + node.getLength(); int statementEnd = statement.getOffset() + statement.getLength(); element.setVisibleRange(declarationEnd, statementEnd - declarationEnd - 1); element.setConst(matches(keyword, Keyword.CONST)); element.setFinal(matches(keyword, Keyword.FINAL)); currentHolder.addLocalVariable(element); variableName.setStaticElement(element); return super.visitDeclaredIdentifier(node); }
@Override public Void visitCatchClause(CatchClause node) { SimpleIdentifier exceptionParameter = node.getExceptionParameter(); if (exceptionParameter != null) { LocalVariableElementImpl exception = new LocalVariableElementImpl(exceptionParameter); currentHolder.addLocalVariable(exception); exceptionParameter.setStaticElement(exception); SimpleIdentifier stackTraceParameter = node.getStackTraceParameter(); if (stackTraceParameter != null) { LocalVariableElementImpl stackTrace = new LocalVariableElementImpl(stackTraceParameter); currentHolder.addLocalVariable(stackTrace); stackTraceParameter.setStaticElement(stackTrace); } } return super.visitCatchClause(node); }
@Override public Void visitFunctionTypeAlias(FunctionTypeAlias node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier aliasName = node.getName(); ParameterElement[] parameters = holder.getParameters(); TypeAliasElementImpl element = new TypeAliasElementImpl(aliasName); element.setParameters(parameters); element.setTypeVariables(holder.getTypeVariables()); ArrayList<Type> normalParameterTypes = new ArrayList<Type>(); ArrayList<Type> optionalParameterTypes = new ArrayList<Type>(); LinkedHashMap<String, Type> namedParameterTypes = new LinkedHashMap<String, Type>(); for (ParameterElement parameter : parameters) { switch (parameter.getParameterKind()) { case REQUIRED: normalParameterTypes.add(parameter.getType()); break; case POSITIONAL: optionalParameterTypes.add(parameter.getType()); break; case NAMED: namedParameterTypes.put(parameter.getName(), parameter.getType()); break; } } TypeName returnType = node.getReturnType(); FunctionTypeImpl functionType = new FunctionTypeImpl(element); functionType.setNormalParameterTypes( normalParameterTypes.toArray(new Type[normalParameterTypes.size()])); functionType.setOptionalParameterTypes( optionalParameterTypes.toArray(new Type[optionalParameterTypes.size()])); functionType.setNamedParameterTypes(namedParameterTypes); if (returnType != null) { functionType.setReturnType(returnType.getType()); } element.setType(functionType); currentHolder.addTypeAlias(element); aliasName.setElement(element); return null; }
@Override public Void visitCatchClause(CatchClause node) { SimpleIdentifier exceptionParameter = node.getExceptionParameter(); if (exceptionParameter != null) { VariableElementImpl exception = new VariableElementImpl(exceptionParameter); currentHolder.addVariable(exception); exceptionParameter.setElement(exception); SimpleIdentifier stackTraceParameter = node.getStackTraceParameter(); if (stackTraceParameter != null) { VariableElementImpl stackTrace = new VariableElementImpl(stackTraceParameter); currentHolder.addVariable(stackTrace); stackTraceParameter.setElement(stackTrace); } } node.visitChildren(this); return null; }
@Override public Void visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { if (!(node.getParent() instanceof DefaultFormalParameter)) { SimpleIdentifier parameterName = node.getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setParameterKind(node.getKind()); setParameterVisibleRange(node, parameter); currentHolder.addParameter(parameter); parameterName.setStaticElement(parameter); } // // The children of this parameter include any parameters defined on the type of this parameter. // ElementHolder holder = new ElementHolder(); visitChildren(holder, node); ((ParameterElementImpl) node.getElement()).setParameters(holder.getParameters()); holder.validate(); return null; }
@Override public Void visitClassDeclaration(ClassDeclaration node) { ElementHolder holder = new ElementHolder(); isValidMixin = true; functionTypesToFix = new ArrayList<FunctionTypeImpl>(); visitChildren(holder, node); SimpleIdentifier className = node.getName(); ClassElementImpl element = new ClassElementImpl(className); TypeParameterElement[] typeParameters = holder.getTypeParameters(); Type[] typeArguments = createTypeParameterTypes(typeParameters); InterfaceTypeImpl interfaceType = new InterfaceTypeImpl(element); interfaceType.setTypeArguments(typeArguments); element.setType(interfaceType); ConstructorElement[] constructors = holder.getConstructors(); if (constructors.length == 0) { // // Create the default constructor. // constructors = createDefaultConstructors(interfaceType); } element.setAbstract(node.getAbstractKeyword() != null); element.setAccessors(holder.getAccessors()); element.setConstructors(constructors); element.setFields(holder.getFields()); element.setMethods(holder.getMethods()); element.setTypeParameters(typeParameters); element.setValidMixin(isValidMixin); int functionTypeCount = functionTypesToFix.size(); for (int i = 0; i < functionTypeCount; i++) { functionTypesToFix.get(i).setTypeArguments(typeArguments); } functionTypesToFix = null; currentHolder.addType(element); className.setStaticElement(element); holder.validate(); return null; }
@Override public Void visitDefaultFormalParameter(DefaultFormalParameter node) { ElementHolder holder = new ElementHolder(); SimpleIdentifier parameterName = node.getParameter().getIdentifier(); ParameterElementImpl parameter; if (node.getParameter() instanceof FieldFormalParameter) { parameter = new DefaultFieldFormalParameterElementImpl(parameterName); } else { parameter = new DefaultParameterElementImpl(parameterName); } parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setParameterKind(node.getKind()); // set initializer, default value range Expression defaultValue = node.getDefaultValue(); if (defaultValue != null) { visit(holder, defaultValue); FunctionElementImpl initializer = new FunctionElementImpl(defaultValue.getBeginToken().getOffset()); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getLocalVariables()); initializer.setParameters(holder.getParameters()); initializer.setSynthetic(true); parameter.setInitializer(initializer); parameter.setDefaultValueRange(defaultValue.getOffset(), defaultValue.getLength()); } // visible range setParameterVisibleRange(node, parameter); currentHolder.addParameter(parameter); parameterName.setStaticElement(parameter); node.getParameter().accept(this); holder.validate(); return null; }
@Override public Void visitFunctionTypeAlias(FunctionTypeAlias node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier aliasName = node.getName(); ParameterElement[] parameters = holder.getParameters(); TypeParameterElement[] typeParameters = holder.getTypeParameters(); FunctionTypeAliasElementImpl element = new FunctionTypeAliasElementImpl(aliasName); element.setParameters(parameters); element.setTypeParameters(typeParameters); FunctionTypeImpl type = new FunctionTypeImpl(element); type.setTypeArguments(createTypeParameterTypes(typeParameters)); element.setType(type); currentHolder.addTypeAlias(element); aliasName.setStaticElement(element); holder.validate(); return null; }
@Override public Void visitDefaultFormalParameter(DefaultFormalParameter node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node.getDefaultValue()); FunctionElementImpl initializer = new FunctionElementImpl(); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getVariables()); initializer.setParameters(holder.getParameters()); SimpleIdentifier parameterName = node.getParameter().getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setInitializer(initializer); parameter.setParameterKind(node.getKind()); currentHolder.addParameter(parameter); parameterName.setElement(parameter); return null; }
@Override public Void visitConstructorDeclaration(ConstructorDeclaration node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier constructorName = node.getName(); ConstructorElementImpl element = new ConstructorElementImpl(constructorName); if (node.getFactoryKeyword() != null) { element.setFactory(true); } element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); element.setParameters(holder.getParameters()); currentHolder.addConstructor(element); node.setElement(element); if (constructorName != null) { constructorName.setElement(element); } return null; }
/** Uses {@link AssistContext} form {@link DartEditor} to prepare {@link #fDartElement}. */ private void prepareElement() { fDartElement = null; context = fEditor.getAssistContext(); if (context == null) { return; } ASTNode selectedNode = context.getCoveredNode(); if (!(selectedNode instanceof SimpleIdentifier)) { return; } nameNode = (SimpleIdentifier) selectedNode; fDartElement = nameNode.getElement(); fDartElement = getCanonicalElement(fDartElement); }
@Override public Void visitConstructorDeclaration(ConstructorDeclaration node) { isValidMixin = false; ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } SimpleIdentifier constructorName = node.getName(); ConstructorElementImpl element = new ConstructorElementImpl(constructorName); if (node.getFactoryKeyword() != null) { element.setFactory(true); } element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); element.setConst(node.getConstKeyword() != null); currentHolder.addConstructor(element); node.setElement(element); if (constructorName == null) { Identifier returnType = node.getReturnType(); if (returnType != null) { element.setNameOffset(returnType.getOffset()); } } else { constructorName.setStaticElement(element); } holder.validate(); return null; }
public void start() { if (getActiveLinkedMode() != null) { // for safety; should already be handled in RenameDartElementAction fgActiveLinkedMode.startFullDialog(); return; } ISourceViewer viewer = fEditor.getViewer(); IDocument document = viewer.getDocument(); fOriginalSelection = viewer.getSelectedRange(); int offset = fOriginalSelection.x; try { fLinkedPositionGroup = new LinkedPositionGroup(); prepareElement(); if (fDartElement == null) { return; } if (viewer instanceof ITextViewerExtension6) { IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager(); if (undoManager instanceof IUndoManagerExtension) { IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager; IUndoContext undoContext = undoManagerExtension.getUndoContext(); IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); fStartingUndoOperation = operationHistory.getUndoOperation(undoContext); } } fOriginalName = nameNode.getName(); final int pos = nameNode.getOffset(); final List<ASTNode> sameNodes = Lists.newArrayList(); nameNode .getRoot() .accept( new RecursiveASTVisitor<Void>() { @Override public Void visitSimpleIdentifier(SimpleIdentifier node) { Element element = node.getElement(); element = getCanonicalElement(element); if (Objects.equal(element, fDartElement)) { sameNodes.add(node); } return super.visitSimpleIdentifier(node); } }); // TODO: copied from LinkedNamesAssistProposal#apply(..): // sort for iteration order, starting with the node @ offset Collections.sort( sameNodes, new Comparator<ASTNode>() { @Override public int compare(ASTNode o1, ASTNode o2) { return rank(o1) - rank(o2); } /** * Returns the absolute rank of an <code>ASTNode</code>. Nodes preceding <code>pos * </code> are ranked last. * * @param node the node to compute the rank for * @return the rank of the node with respect to the invocation offset */ private int rank(ASTNode node) { int relativeRank = node.getOffset() + node.getLength() - pos; if (relativeRank < 0) { return Integer.MAX_VALUE + relativeRank; } else { return relativeRank; } } }); for (int i = 0; i < sameNodes.size(); i++) { ASTNode elem = sameNodes.get(i); LinkedPosition linkedPosition = new LinkedPosition(document, elem.getOffset(), elem.getLength(), i); if (i == 0) { fNamePosition = linkedPosition; } fLinkedPositionGroup.addPosition(linkedPosition); } fLinkedModeModel = new LinkedModeModel(); fLinkedModeModel.addGroup(fLinkedPositionGroup); fLinkedModeModel.forceInstall(); fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor)); fLinkedModeModel.addLinkingListener(new EditorSynchronizer()); LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer); ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE); ui.setExitPolicy(new ExitPolicy(document)); ui.enter(); viewer.setSelectedRange( fOriginalSelection.x, fOriginalSelection.y); // by default, full word is selected; restore original selection if (viewer instanceof IEditingSupportRegistry) { IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer; registry.register(fFocusEditingSupport); } openSecondaryPopup(); // startAnimation(); fgActiveLinkedMode = this; } catch (BadLocationException e) { DartToolsPlugin.log(e); } }
@Override public Void visitSimpleIdentifier(SimpleIdentifier node) { writer.print(node.getToken().getLexeme()); return null; }
@Override public R visitSimpleIdentifier(SimpleIdentifier node) { node.visitChildren(this); return null; }
@Override public Void visitFunctionDeclaration(FunctionDeclaration node) { FunctionExpression expression = node.getFunctionExpression(); if (expression != null) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, expression); } finally { inFunction = wasInFunction; } Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier functionName = node.getName(); FunctionElementImpl element = new FunctionElementImpl(functionName); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); if (inFunction) { Block enclosingBlock = node.getAncestor(Block.class); if (enclosingBlock != null) { int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); element.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); } } currentHolder.addFunction(element); expression.setElement(element); functionName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); if (propertyNameNode == null) { // TODO(brianwilkerson) Report this internal error. return null; } String propertyName = propertyNameNode.getName(); TopLevelVariableElementImpl variable = (TopLevelVariableElementImpl) currentHolder.getTopLevelVariable(propertyName); if (variable == null) { variable = new TopLevelVariableElementImpl(node.getName().getName()); variable.setFinal(true); variable.setSynthetic(true); currentHolder.addTopLevelVariable(variable); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(variable); getter.setGetter(true); getter.setStatic(true); variable.setGetter(getter); currentHolder.addAccessor(getter); expression.setElement(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(variable); setter.setSetter(true); setter.setStatic(true); variable.setSetter(setter); variable.setFinal(false); currentHolder.addAccessor(setter); expression.setElement(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); } return null; }
@Override public Void visitMethodDeclaration(MethodDeclaration node) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } boolean isStatic = node.isStatic(); Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier methodName = node.getName(); String nameOfMethod = methodName.getName(); if (nameOfMethod.equals(TokenType.MINUS.getLexeme()) && node.getParameters().getParameters().size() == 0) { nameOfMethod = "unary-"; } MethodElementImpl element = new MethodElementImpl(nameOfMethod, methodName.getOffset()); element.setAbstract(node.isAbstract()); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); element.setStatic(isStatic); currentHolder.addMethod(element); methodName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); String propertyName = propertyNameNode.getName(); FieldElementImpl field = (FieldElementImpl) currentHolder.getField(propertyName); if (field == null) { field = new FieldElementImpl(node.getName().getName()); field.setFinal(true); field.setStatic(isStatic); field.setSynthetic(true); currentHolder.addField(field); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(field); getter.setAbstract( node.getBody() instanceof EmptyFunctionBody && node.getExternalKeyword() == null); getter.setGetter(true); getter.setStatic(isStatic); field.setGetter(getter); currentHolder.addAccessor(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(field); setter.setAbstract( node.getBody() instanceof EmptyFunctionBody && !matches(node.getExternalKeyword(), Keyword.EXTERNAL)); setter.setSetter(true); setter.setStatic(isStatic); field.setSetter(setter); field.setFinal(false); currentHolder.addAccessor(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); return null; }
@Override public Void visitVariableDeclaration(VariableDeclaration node) { Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword(); boolean isConst = matches(keyword, Keyword.CONST); boolean isFinal = matches(keyword, Keyword.FINAL); boolean hasInitializer = node.getInitializer() != null; VariableElementImpl element; if (inFieldContext) { SimpleIdentifier fieldName = node.getName(); FieldElementImpl field; if (isConst && hasInitializer) { field = new ConstFieldElementImpl(fieldName); } else { field = new FieldElementImpl(fieldName); } element = field; currentHolder.addField(field); fieldName.setStaticElement(field); } else if (inFunction) { SimpleIdentifier variableName = node.getName(); LocalVariableElementImpl variable; if (isConst && hasInitializer) { variable = new ConstLocalVariableElementImpl(variableName); } else { variable = new LocalVariableElementImpl(variableName); } element = variable; Block enclosingBlock = node.getAncestor(Block.class); int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); // TODO(brianwilkerson) This isn't right for variables declared in a for loop. variable.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); currentHolder.addLocalVariable(variable); variableName.setStaticElement(element); } else { SimpleIdentifier variableName = node.getName(); TopLevelVariableElementImpl variable; if (isConst && hasInitializer) { variable = new ConstTopLevelVariableElementImpl(variableName); } else { variable = new TopLevelVariableElementImpl(variableName); } element = variable; currentHolder.addTopLevelVariable(variable); variableName.setStaticElement(element); } element.setConst(isConst); element.setFinal(isFinal); if (hasInitializer) { ElementHolder holder = new ElementHolder(); boolean wasInFieldContext = inFieldContext; inFieldContext = false; try { visit(holder, node.getInitializer()); } finally { inFieldContext = wasInFieldContext; } FunctionElementImpl initializer = new FunctionElementImpl(node.getInitializer().getBeginToken().getOffset()); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getLocalVariables()); initializer.setSynthetic(true); element.setInitializer(initializer); holder.validate(); } if (element instanceof PropertyInducingElementImpl) { PropertyInducingElementImpl variable = (PropertyInducingElementImpl) element; if (inFieldContext) { ((FieldElementImpl) variable) .setStatic( matches( ((FieldDeclaration) node.getParent().getParent()).getStaticKeyword(), Keyword.STATIC)); } PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(variable); getter.setGetter(true); getter.setStatic(variable.isStatic()); currentHolder.addAccessor(getter); variable.setGetter(getter); if (!isFinal) { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(variable); setter.setSetter(true); setter.setStatic(variable.isStatic()); ParameterElementImpl parameter = new ParameterElementImpl("_" + variable.getName(), variable.getNameOffset()); parameter.setSynthetic(true); parameter.setParameterKind(ParameterKind.REQUIRED); setter.setParameters(new ParameterElement[] {parameter}); currentHolder.addAccessor(setter); variable.setSetter(setter); } } return null; }