static ImmutableList<Parameter> createListFromVarSymbols(List<VarSymbol> varSymbols) { return Streams.mapWithIndex( varSymbols.stream(), (s, i) -> new AutoValue_Parameter( s.getSimpleName().toString(), s.asType(), (int) i, s.getSimpleName().toString(), Kind.IDENTIFIER, false)) .collect(toImmutableList()); }
return NO_MATCH; if (symbol.getKind() != ElementKind.PARAMETER) { return NO_MATCH; return NO_MATCH; if (type.getTypeArguments().isEmpty()) { return NO_MATCH; wildBound(getOnlyElement(type.getTypeArguments())), state.getTypeFromString(Path.class.getName()), state)) {
private void saveConstValue(VariableTree tree, Scope scope) { VarSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return; } if ((sym.flags() & (Flags.EFFECTIVELY_FINAL | Flags.FINAL)) == 0) { return; } // heuristic: long string constants are generally more interesting than short ones, or // than non-string constants (e.g. `""`, `0`, or `false`). String constValue = ASTHelpers.constValue(tree.getInitializer(), String.class); if (constValue == null || constValue.length() <= 1) { return; } scope.put(state.getSourceForNode(tree.getInitializer()), sym); } }.scan(tree, new Scope(null));
return; if (classdecl.implementing != null && !classdecl.implementing.isEmpty() && !getScope().enumScope) { List<JCExpression> implementing = new ArrayList<>(classdecl.implementing); if (!var.sym.isStatic() && var.init != null) { getScope().fieldsWithInitializers.add((JCVariableDecl) def); || (def instanceof JCVariableDecl && ((JCVariableDecl) def).sym.isStatic()))) { if (getScope().enumScope && ((JCVariableDecl) def).sym.getKind() != ElementKind.ENUM_CONSTANT) { getScope().isComplexEnum = true; continue; print(", "); print("private " + v.getSimpleName() + ": any"); || (def instanceof JCVariableDecl && ((JCVariableDecl) def).sym.isStatic())) { if (def instanceof JCVariableDecl && context.hasAnnotationType(((JCVariableDecl) def).sym, ANNOTATION_STRING_TYPE, JSweetConfig.ANNOTATION_ERASED)) { for (JCTree tree : classdecl.defs) { if (tree instanceof JCVariableDecl && ((JCVariableDecl) tree).sym.getKind() == ElementKind.ENUM_CONSTANT) { JCVariableDecl varDecl = (JCVariableDecl) tree;
Symtab symtab = state.getSymtab(); List<? extends ExpressionTree> args = invocation.getArguments(); switch (symbol.getSimpleName().toString()) { case "expect": Type type = ASTHelpers.getType(getOnlyElement(invocation.getArguments())); Type matcherType = state.getTypes().asSuper(type, state.getSymbolFromString("org.hamcrest.Matcher")); if (!matcherType.getTypeArguments().isEmpty()) { Type matchType = getOnlyElement(matcherType.getTypeArguments()); if (isSubtype(matchType, symtab.throwableType, state)) { exceptionClassName = SuggestedFixes.qualifyType(state, fix, matchType); case "expectMessage": if (isSubtype( getOnlyElement(symbol.getParameters()).asType(), symtab.stringType, state)) {
/** Definition for this$n field. * @param pos The source code position of the definition. * @param owner The class in which the definition goes. */ JCVariableDecl outerThisDef(int pos, Symbol owner) { long flags = FINAL | SYNTHETIC; if (owner.kind == TYP && target.usePrivateSyntheticFields()) flags |= PRIVATE; Type target = types.erasure(owner.enclClass().type.getEnclosingType()); VarSymbol outerThis = new VarSymbol( flags, outerThisName(target, owner), target, owner); outerThisStack = outerThisStack.prepend(outerThis); JCVariableDecl vd = make.at(pos).VarDef(outerThis, null); vd.vartype = access(vd.vartype); return vd; }
public List<VarSymbol> params() { owner.complete(); if (params == null) { List<Name> names = savedParameterNames; savedParameterNames = null; if (names == null) { names = List.nil(); int i = 0; for (Type t : type.getParameterTypes()) names = names.prepend(name.table.fromString("arg" + i++)); names = names.reverse(); } ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>(); for (Type t : type.getParameterTypes()) { buf.append(new VarSymbol(PARAMETER, names.head, t, this)); names = names.tail; } params = buf.toList(); } return params; }
private void fillArtificalParamNames(final ClassSymbol clazz) { for (Symbol s : clazz.getEnclosedElements()) { if (s instanceof MethodSymbol) { MethodSymbol ms = (MethodSymbol) s; if (ms.getParameters().isEmpty()) { continue; } Set<String> usedNames = new HashSet<String>(); for (VarSymbol vs : ms.getParameters()) { String name = JavaSourceAccessor.getINSTANCE().generateReadableParameterName(vs.asType().toString(), usedNames); vs.setName(clazz.name.table.fromString(name)); } } } }
/** Create a variable definition from a variable symbol and an initializer * expression. */ public JCVariableDecl VarDef(VarSymbol v, JCExpression init) { return (JCVariableDecl) new JCVariableDecl( Modifiers(v.flags(), Annotations(v.getAnnotationMirrors())), v.name, Type(v.type), init, v).setPos(pos).setType(v.type); }
/** * Works for method parameters defined either in source or in class files * * @param symbol the method symbol * @param paramInd index of the parameter * @return all declaration and type-use annotations for the parameter */ public static Stream<? extends AnnotationMirror> getAllAnnotationsForParameter( Symbol.MethodSymbol symbol, int paramInd) { Symbol.VarSymbol varSymbol = symbol.getParameters().get(paramInd); return Stream.concat( varSymbol.getAnnotationMirrors().stream(), symbol .getRawTypeAttributes() .stream() .filter( t -> t.position.type.equals(TargetType.METHOD_FORMAL_PARAMETER) && t.position.parameter_index == paramInd)); }
private static Symbol getFinalizer(VisitorState state, ClassSymbol enclosing) { Type finalizerType = state.getTypeFromString("com.google.common.labs.base.Finalizer"); Optional<VarSymbol> finalizerField = state.getTypes().closure(enclosing.asType()).stream() .flatMap(s -> getFields(s.asElement())) .filter(s -> ASTHelpers.isSameType(finalizerType, s.asType(), state)) .findFirst(); if (finalizerField.isPresent()) { return finalizerField.get(); } return ASTHelpers.resolveExistingMethod( state, enclosing.enclClass(), state.getName("finalize"), /* argTypes= */ ImmutableList.of(), /* tyargTypes= */ ImmutableList.of()); }
private VarSymbol makeOuterThisVarSymbol(Symbol owner, long flags) { if (owner.kind == TYP && target.usePrivateSyntheticFields()) flags |= PRIVATE; Type target = types.erasure(owner.enclClass().type.getEnclosingType()); VarSymbol outerThis = new VarSymbol(flags, outerThisName(target, owner), target, owner); outerThisStack = outerThisStack.prepend(outerThis); return outerThis; }
return NO_MATCH; if (tree.getArguments().size() != sym.getParameters().size()) { String qualified = SuggestedFixes.qualifyType( state, fix, types.elemtype(getLast(sym.getParameters()).asType())); Tree toFix = !trueIsArray ? cond.getTrueExpression() : cond.getFalseExpression(); fix.prefixWith(toFix, String.format("new %s[] {", qualified)).postfixWith(toFix, "}");
@Override public Description matchVariable(VariableTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } VarSymbol symbol = ASTHelpers.getSymbol(tree); if (symbol.type.isPrimitive() && tree.getInitializer() != null) { return doUnboxingCheck(state, tree.getInitializer()); } if (!symbol.getKind().equals(ElementKind.FIELD)) { return Description.NO_MATCH; } ExpressionTree initializer = tree.getInitializer(); if (initializer != null) { if (!symbol.type.isPrimitive() && !skipDueToFieldAnnotation(symbol)) { if (mayBeNullExpr(state, initializer)) { return createErrorDescriptionForNullAssignment( MessageTypes.ASSIGN_FIELD_NULLABLE, tree, "assigning @Nullable expression to @NonNull field", initializer, state.getPath()); } } } return Description.NO_MATCH; }
boolean hasInvisible = false; if (m.params != null) for (VarSymbol s : m.params) { for (Attribute.Compound a : s.getAnnotationMirrors()) { switch (getRetention(a.type.tsym)) { case SOURCE: break; if (hasVisible) { int attrIndex = writeAttr(names.RuntimeVisibleParameterAnnotations); databuf.appendByte(m.params.length()); for (VarSymbol s : m.params) { ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); for (Attribute.Compound a : s.getAnnotationMirrors()) if (getRetention(a.type.tsym) == RetentionPolicy.RUNTIME) buf.append(a); databuf.appendByte(m.params.length()); for (VarSymbol s : m.params) { ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); for (Attribute.Compound a : s.getAnnotationMirrors()) if (getRetention(a.type.tsym) == RetentionPolicy.CLASS) buf.append(a);
/** * Suggest replacing {@code input} with a reference to a method parameter in the nearest enclosing * method declaration with a similar or the same name as the {@code input} expression. * * @param input a MEMBER_SELECT or IDENTIFIER expression which should be replaced. * @param validParameterPredicate Predicate used to decide which method parameters are appropriate * candidates for replacement (e.g.: is the appropriate type) */ public static ImmutableList<Fix> fixesByReplacingExpressionWithMethodParameter( ExpressionTree input, Predicate<JCVariableDecl> validParameterPredicate, VisitorState state) { Preconditions.checkState(input.getKind() == IDENTIFIER || input.getKind() == MEMBER_SELECT); // find a method parameter matching the input predicate and similar name and suggest it // as the new argument Multimap<Integer, JCVariableDecl> potentialReplacements = ASTHelpers.findEnclosingNode(state.getPath(), JCMethodDecl.class).getParameters().stream() .filter(validParameterPredicate) .collect(collectByEditDistanceTo(simpleNameOfIdentifierOrMemberAccess(input))); return buildValidReplacements( potentialReplacements, var -> SuggestedFix.replace(input, var.sym.toString())); }