use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class ImmutableAnalysis method areFieldsImmutable.
/**
* Check a single class' fields for immutability.
*
* @param immutableTyParams the in-scope immutable type parameters
* @param classType the type to check the fields of
*/
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
ClassSymbol classSym = (ClassSymbol) classType.tsym;
if (classSym.members() == null) {
return Violation.absent();
}
Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {
@Override
public boolean accepts(Symbol symbol) {
return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
}
};
Map<Symbol, Tree> declarations = new HashMap<>();
if (tree.isPresent()) {
for (Tree member : tree.get().getMembers()) {
Symbol sym = ASTHelpers.getSymbol(member);
if (sym != null) {
declarations.put(sym, member);
}
}
}
// javac gives us members in reverse declaration order
// handling them in declaration order leads to marginally better diagnostics
List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
for (Symbol member : members) {
Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
if (info.isPresent()) {
return info;
}
}
return Violation.absent();
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class UTemplater method createTemplate.
/**
* Returns a template based on a method. One-line methods starting with a {@code return} statement
* are guessed to be expression templates, and all other methods are guessed to be block
* templates.
*/
public static Template<?> createTemplate(Context context, MethodTree decl) {
MethodSymbol declSym = ASTHelpers.getSymbol(decl);
ImmutableClassToInstanceMap<Annotation> annotations = UTemplater.annotationMap(declSym);
ImmutableMap<String, VarSymbol> freeExpressionVars = freeExpressionVariables(decl);
Context subContext = new SubContext(context);
final UTemplater templater = new UTemplater(freeExpressionVars, subContext);
ImmutableMap<String, UType> expressionVarTypes = ImmutableMap.copyOf(Maps.transformValues(freeExpressionVars, new Function<VarSymbol, UType>() {
@Override
public UType apply(VarSymbol sym) {
return templater.template(sym.type);
}
}));
UType genericType = templater.template(declSym.type);
List<UTypeVar> typeParameters;
UMethodType methodType;
if (genericType instanceof UForAll) {
UForAll forAllType = (UForAll) genericType;
typeParameters = forAllType.getTypeVars();
methodType = (UMethodType) forAllType.getQuantifiedType();
} else if (genericType instanceof UMethodType) {
typeParameters = ImmutableList.of();
methodType = (UMethodType) genericType;
} else {
throw new IllegalArgumentException("Expected genericType to be either a ForAll or a UMethodType, but was " + genericType);
}
List<? extends StatementTree> bodyStatements = decl.getBody().getStatements();
if (bodyStatements.size() == 1 && Iterables.getOnlyElement(bodyStatements).getKind() == Kind.RETURN && context.get(REQUIRE_BLOCK_KEY) == null) {
ExpressionTree expression = ((ReturnTree) Iterables.getOnlyElement(bodyStatements)).getExpression();
return ExpressionTemplate.create(annotations, typeParameters, expressionVarTypes, templater.template(expression), methodType.getReturnType());
} else {
List<UStatement> templateStatements = new ArrayList<>();
for (StatementTree statement : bodyStatements) {
templateStatements.add(templater.template(statement));
}
return BlockTemplate.create(annotations, typeParameters, expressionVarTypes, templateStatements);
}
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.
the class TreeConverter method convertVariableExpression.
private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
Type newType = convertType(var.asType(), getPosition(node), isVarargs);
VariableDeclarationFragment fragment = new VariableDeclarationFragment();
fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.
the class TreeConverter method convertVariableDeclaration.
private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
if (var.getKind() == ElementKind.FIELD) {
FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
return newNode;
}
if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
}
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
TreeUtil.moveList(init.getArguments(), newNode.getArguments());
if (init.getAnonymousClassDeclaration() != null) {
newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
}
return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
}
return convertSingleVariable(node);
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.
the class TreeConverter method convertSingleVariable.
private TreeNode convertSingleVariable(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
SourcePosition pos = getPosition(node);
boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
Type newType = convertType(var.asType(), pos, isVarargs);
return new SingleVariableDeclaration().setType(newType).setIsVarargs(isVarargs).setAnnotations(convertAnnotations(node.getModifiers())).setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
}
Aggregations