use of com.sun.tools.javac.tree.JCTree.JCExpression in project lombok by rzwitserloot.
the class HandleConstructor method createStaticConstructor.
public JCMethodDecl createStaticConstructor(String name, AccessLevel level, JavacNode typeNode, List<JavacNode> fields, JCTree source) {
JavacTreeMaker maker = typeNode.getTreeMaker();
JCClassDecl type = (JCClassDecl) typeNode.get();
JCModifiers mods = maker.Modifiers(Flags.STATIC | toJavacModifier(level));
JCExpression returnType, constructorType;
ListBuffer<JCTypeParameter> typeParams = new ListBuffer<JCTypeParameter>();
ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
ListBuffer<JCExpression> typeArgs1 = new ListBuffer<JCExpression>();
ListBuffer<JCExpression> typeArgs2 = new ListBuffer<JCExpression>();
ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
if (!type.typarams.isEmpty()) {
for (JCTypeParameter param : type.typarams) {
typeArgs1.append(maker.Ident(param.name));
typeArgs2.append(maker.Ident(param.name));
typeParams.append(maker.TypeParameter(param.name, param.bounds));
}
returnType = maker.TypeApply(maker.Ident(type.name), typeArgs1.toList());
constructorType = maker.TypeApply(maker.Ident(type.name), typeArgs2.toList());
} else {
returnType = maker.Ident(type.name);
constructorType = maker.Ident(type.name);
}
for (JavacNode fieldNode : fields) {
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
Name fieldName = removePrefixFromField(fieldNode);
JCExpression pType = cloneType(maker, field.vartype, source, typeNode.getContext());
List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, pType, null);
params.append(param);
args.append(maker.Ident(fieldName));
}
JCReturn returnStatement = maker.Return(maker.NewClass(null, List.<JCExpression>nil(), constructorType, args.toList(), null));
JCBlock body = maker.Block(0, List.<JCStatement>of(returnStatement));
return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName(name), returnType, typeParams.toList(), params.toList(), List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project lombok by rzwitserloot.
the class HandleBuilder method generateToBuilderMethod.
private JCMethodDecl generateToBuilderMethod(String toBuilderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams, java.util.List<BuilderFieldData> builderFields, boolean fluent, JCAnnotation ast) {
// return new ThingieBuilder<A, B>().setA(this.a).setB(this.b);
JavacTreeMaker maker = type.getTreeMaker();
ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
for (JCTypeParameter typeParam : typeParams) {
typeArgs.append(maker.Ident(typeParam.name));
}
JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null);
JCExpression invoke = call;
for (BuilderFieldData bfd : builderFields) {
Name setterName = fluent ? bfd.name : type.toName(HandlerUtil.buildAccessorName("set", bfd.name.toString()));
JCExpression arg;
if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) {
arg = maker.Select(maker.Ident(type.toName("this")), bfd.obtainVia == null ? bfd.rawName : type.toName(bfd.obtainVia.field()));
} else {
if (bfd.obtainVia.isStatic()) {
JCExpression c = maker.Select(maker.Ident(type.toName(type.getName())), type.toName(bfd.obtainVia.method()));
arg = maker.Apply(List.<JCExpression>nil(), c, List.<JCExpression>of(maker.Ident(type.toName("this"))));
} else {
JCExpression c = maker.Select(maker.Ident(type.toName("this")), type.toName(bfd.obtainVia.method()));
arg = maker.Apply(List.<JCExpression>nil(), c, List.<JCExpression>nil());
}
}
invoke = maker.Apply(List.<JCExpression>nil(), maker.Select(invoke, setterName), List.of(arg));
}
JCStatement statement = maker.Return(invoke);
JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName(toBuilderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project lombok by rzwitserloot.
the class JavacResolution method typeToJCTree.
private static JCExpression typeToJCTree(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
int dims = 0;
Type type0 = type;
while (type0 instanceof ArrayType) {
dims++;
type0 = ((ArrayType) type0).elemtype;
}
JCExpression result = typeToJCTree0(type0, ast, allowCompound, allowVoid);
while (dims > 0) {
result = ast.getTreeMaker().TypeArray(result);
dims--;
}
return result;
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project lombok by rzwitserloot.
the class JavacResolution method createJavaLangObject.
public static JCExpression createJavaLangObject(JavacAST ast) {
JavacTreeMaker maker = ast.getTreeMaker();
JCExpression out = maker.Ident(ast.toName("java"));
out = maker.Select(out, ast.toName("lang"));
out = maker.Select(out, ast.toName("Object"));
return out;
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class Attr method visitTypeUnion.
public void visitTypeUnion(JCTypeUnion tree) {
ListBuffer<Type> multicatchTypes = ListBuffer.lb();
// lazy, only if needed
ListBuffer<Type> all_multicatchTypes = null;
for (JCExpression typeTree : tree.alternatives) {
Type ctype = attribType(typeTree, env);
ctype = chk.checkType(typeTree.pos(), chk.checkClassType(typeTree.pos(), ctype), syms.throwableType);
if (!ctype.isErroneous()) {
//unrelated w.r.t. subtyping
if (chk.intersects(ctype, multicatchTypes.toList())) {
for (Type t : multicatchTypes) {
boolean sub = types.isSubtype(ctype, t);
boolean sup = types.isSubtype(t, ctype);
if (sub || sup) {
//assume 'a' <: 'b'
Type a = sub ? ctype : t;
Type b = sub ? t : ctype;
log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b);
}
}
}
multicatchTypes.append(ctype);
if (all_multicatchTypes != null)
all_multicatchTypes.append(ctype);
} else {
if (all_multicatchTypes == null) {
all_multicatchTypes = ListBuffer.lb();
all_multicatchTypes.appendList(multicatchTypes);
}
all_multicatchTypes.append(ctype);
}
}
Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, pkind, pt);
if (t.tag == CLASS) {
List<Type> alternatives = ((all_multicatchTypes == null) ? multicatchTypes : all_multicatchTypes).toList();
t = new UnionClassType((ClassType) t, alternatives);
}
tree.type = result = t;
}
Aggregations