Java Code Examples for lombok.javac.JavacTreeMaker#Exec

The following examples show how to use lombok.javac.JavacTreeMaker#Exec . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacJavaUtilListSingularizer.java    From EasyMPermission with MIT License 6 votes vote down vote up
private List<JCStatement> createListCopy(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) {
	List<JCExpression> jceBlank = List.nil();
	Name thisName = builderType.toName("this");
	
	JCExpression argToUnmodifiable; {
		 // new java.util.ArrayList<Generics>(this.pluralName);
		List<JCExpression> constructorArgs = List.nil();
		JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
		constructorArgs = List.<JCExpression>of(thisDotPluralName);
		JCExpression targetTypeExpr = chainDots(builderType, "java", "util", "ArrayList");
		targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
		argToUnmodifiable = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
	}
	
	JCStatement unmodifiableStat; {
		// pluralname = Collections.unmodifiableInterfaceType(-newlist-);
		JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiableList"), List.of(argToUnmodifiable));
		unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
	}
	
	return List.of(unmodifiableStat);
}
 
Example 2
Source File: HandleEqualsAndHashCode.java    From EasyMPermission with MIT License 5 votes vote down vote up
public JCExpressionStatement createResultCalculation(JavacNode typeNode, JCExpression expr) {
	/* result = result * PRIME + (expr); */
	JavacTreeMaker maker = typeNode.getTreeMaker();
	Name resultName = typeNode.toName(RESULT_NAME);
	JCExpression mult = maker.Binary(CTC_MUL, maker.Ident(resultName), maker.Ident(typeNode.toName(PRIME_NAME)));
	JCExpression add = maker.Binary(CTC_PLUS, mult, expr);
	return maker.Exec(maker.Assign(maker.Ident(resultName), add));
}
 
Example 3
Source File: JavacGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
protected JCStatement createConstructBuilderVarIfNeeded(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, JCTree source) {
	List<JCExpression> jceBlank = List.nil();
	
	JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
	JCExpression thisDotField2 = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
	JCExpression cond = maker.Binary(CTC_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
	
	JCExpression create = maker.Apply(jceBlank, chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), getBuilderMethodName(data)), jceBlank);
	JCStatement thenPart = maker.Exec(maker.Assign(thisDotField2, create));
	
	return maker.If(cond, thenPart, null);
}
 
Example 4
Source File: JavacJavaUtilSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
protected JCStatement createConstructBuilderVarIfNeeded(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, JCTree source) {
	List<JCExpression> jceBlank = List.nil();

	Name v1Name = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
	Name v2Name = mapMode ? builderType.toName(data.getPluralName() + "$value") : null;
	JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
	JCExpression cond = maker.Binary(CTC_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
	thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
	JCExpression v1Type = chainDots(builderType, "java", "util", "ArrayList");
	v1Type = addTypeArgs(1, false, builderType, v1Type, data.getTypeArgs(), source);
	JCExpression constructArrayList = maker.NewClass(null, jceBlank, v1Type, jceBlank, null);
	JCStatement initV1 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
	JCStatement thenPart;
	if (mapMode) {
		thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v2Name);
		JCExpression v2Type = chainDots(builderType, "java", "util", "ArrayList");
		List<JCExpression> tArgs = data.getTypeArgs();
		if (tArgs != null && tArgs.tail != null) tArgs = tArgs.tail;
		else tArgs = List.nil();
		v2Type = addTypeArgs(1, false, builderType, v2Type, tArgs, source);
		constructArrayList = maker.NewClass(null, jceBlank, v2Type, jceBlank, null);
		JCStatement initV2 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
		thenPart = maker.Block(0, List.of(initV1, initV2));
	} else {
		thenPart = initV1;
	}
	return maker.If(cond, thenPart, null);
}
 
Example 5
Source File: HandleGetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCStatement callSet(JavacNode source, JCExpression receiver, JCExpression value) {
	JavacTreeMaker maker = source.getTreeMaker();
	return maker.Exec(maker.Apply(NIL_EXPRESSION, maker.Select(receiver, source.toName("set")), List.<JCExpression>of(value)));
}
 
Example 6
Source File: JavacJavaUtilSingularizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
protected List<JCStatement> createJavaUtilSimpleCreationAndFillStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType, JCTree source) {
	List<JCExpression> jceBlank = List.nil();
	Name thisName = builderType.toName("this");
	
	JCStatement createStat; {
		 // pluralName = new java.util.TargetType(initialCap);
		List<JCExpression> constructorArgs = List.nil();
		if (addInitialCapacityArg) {
			Name varName = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
			// this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
			// lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
			JCExpression lessThanCutoff = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, varName, nullGuard), maker.Literal(CTC_INT, 0x40000000));
			JCExpression integerMaxValue = genJavaLangTypeRef(builderType, "Integer", "MAX_VALUE");
			JCExpression sizeFormulaLeft = maker.Binary(CTC_PLUS, maker.Literal(CTC_INT, 1), getSize(maker, builderType, varName, nullGuard));
			JCExpression sizeFormulaRightLeft = maker.Binary(CTC_MINUS, getSize(maker, builderType, varName, nullGuard), maker.Literal(CTC_INT, 3));
			JCExpression sizeFormulaRight = maker.Binary(CTC_DIV, sizeFormulaRightLeft, maker.Literal(CTC_INT, 3));
			JCExpression sizeFormula = maker.Binary(CTC_PLUS, sizeFormulaLeft, sizeFormulaRight);
			constructorArgs = List.<JCExpression>of(maker.Conditional(lessThanCutoff, sizeFormula, integerMaxValue));
		}
		
		JCExpression targetTypeExpr = chainDots(builderType, "java", "util", targetType);
		targetTypeExpr = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
		JCExpression constructorCall = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
		if (defineVar) {
			JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
			localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs(), source);
			createStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, constructorCall);
		} else {
			createStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), constructorCall));
		}
	}
	
	JCStatement fillStat; {
		if (mapMode) {
			// for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
			Name ivar = builderType.toName("$i");
			Name keyVarName = builderType.toName(data.getPluralName() + "$key");
			JCExpression pluralnameDotPut = maker.Select(maker.Ident(data.getPluralName()), builderType.toName("put"));
			JCExpression arg1 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$key", "get"), List.<JCExpression>of(maker.Ident(ivar)));
			JCExpression arg2 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$value", "get"), List.<JCExpression>of(maker.Ident(ivar)));
			JCStatement putStatement = maker.Exec(maker.Apply(jceBlank, pluralnameDotPut, List.of(arg1, arg2)));
			JCStatement forInit = maker.VarDef(maker.Modifiers(0), ivar, maker.TypeIdent(CTC_INT), maker.Literal(CTC_INT, 0));
			JCExpression checkExpr = maker.Binary(CTC_LESS_THAN, maker.Ident(ivar), getSize(maker, builderType, keyVarName, nullGuard));
			JCExpression incrementExpr = maker.Unary(CTC_POSTINC, maker.Ident(ivar));
			fillStat = maker.ForLoop(List.of(forInit), checkExpr, List.of(maker.Exec(incrementExpr)), putStatement);
		} else {
			// pluralname.addAll(this.pluralname);
			JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
			fillStat = maker.Exec(maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("addAll")), List.of(thisDotPluralName)));
		}
		if (nullGuard) {
			JCExpression thisDotField = maker.Select(maker.Ident(thisName), mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName());
			JCExpression nullCheck = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
			fillStat = maker.If(nullCheck, fillStat, null);
		}
	}
	JCStatement unmodifiableStat; {
		// pluralname = Collections.unmodifiableInterfaceType(pluralname);
		JCExpression arg = maker.Ident(data.getPluralName());
		JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiable" + data.getTargetSimpleType()), List.of(arg));
		unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
	}
	
	return List.of(createStat, fillStat, unmodifiableStat);
}
 
Example 7
Source File: HandleRuntimePermission.java    From EasyMPermission with MIT License 4 votes vote down vote up
private JCMethodDecl createOnRequestPermissionMethod(JavacNode typeNode, List<PermissionAnnotatedItem> permissionList) {
    JavacTreeMaker treeMaker = typeNode.getTreeMaker();

    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();

    JCTree.JCExpression returnType = treeMaker.Type(Javac.createVoidType(treeMaker, CTC_VOID));

    List<JCVariableDecl> paramList = List.of(
        Variable(typeNode)
                .modifiers(Flags.PARAMETER)
                .name("requestCode")
                .type(treeMaker.TypeIdent(CTC_INT))
                .value(null)
                .build(),
        Variable(typeNode)
                .modifiers(Flags.PARAMETER)
                .name("permissions")
                .type(treeMaker.TypeArray(genJavaLangTypeRef(typeNode, "String")))
                .value(null)
                .build(),
        Variable(typeNode)
                .modifiers(Flags.PARAMETER)
                .name("grantResults")
                .type(treeMaker.TypeArray(treeMaker.TypeIdent(CTC_INT)))
                .value(null)
                .build()
    );

    for (PermissionAnnotatedItem item : permissionList) {

        JCReturn jcReturn = treeMaker.Return(null);

        JCBinary cmpRequestCode = treeMaker.Binary(CTC_EQUAL,
                treeMaker.Ident(typeNode.toName("requestCode")),
                treeMaker.Literal(CTC_INT, item.getRequestCode()));

        JCBinary cmpGrantResult = treeMaker.Binary(CTC_NOT_EQUAL,
                treeMaker.Indexed(treeMaker.Ident(typeNode.toName("grantResults")), treeMaker.Literal(CTC_INT, 0)),
                JavacHandlerUtil.chainDots(typeNode, "android", "content", "pm", "PackageManager", "PERMISSION_GRANTED"));

        JCExpression toastMethod =
                JavacHandlerUtil.chainDots(typeNode, "android", "widget", "Toast", "makeText");
        List<JCExpression> toastArgs = List.<JCTree.JCExpression>of(
                treeMaker.Ident(typeNode.toName("this")),
                treeMaker.Literal(MESSAGE_PERMISSIONS_WERE_NOT_GRANTED),
                treeMaker.Literal(CTC_INT, 1));
        JCMethodInvocation printlnInvocation =
                treeMaker.Apply(List.<JCTree.JCExpression>nil(), toastMethod, toastArgs);
        JCExpressionStatement thenPart = treeMaker.Exec(
                treeMaker.Apply(List.<JCTree.JCExpression>nil(), treeMaker.Select(printlnInvocation, typeNode.toName("show")), List.<JCExpression>nil()));

        toastArgs = List.<JCTree.JCExpression>of(
                treeMaker.Ident(typeNode.toName("this")),
                treeMaker.Literal(MESSAGE_PERMISSIONS_HAVE_BEEN_GRANTED),
                treeMaker.Literal(CTC_INT, 1));
        printlnInvocation =
                treeMaker.Apply(List.<JCTree.JCExpression>nil(), toastMethod, toastArgs);
        JCExpressionStatement elsePart = treeMaker.Exec(
                treeMaker.Apply(List.<JCTree.JCExpression>nil(), treeMaker.Select(printlnInvocation, typeNode.toName("show")), List.<JCExpression>nil()));

        JCIf jcIfGrantResult = If(typeNode)
                .condition(cmpGrantResult)
                .withThen(Block(typeNode).last(thenPart).build())
                .withElse(Block(typeNode).last(elsePart).build())
                .build();

        JCIf jcIfRequestCode = If(typeNode)
                .condition(cmpRequestCode)
                .onlyThen(Block(typeNode).add(jcIfGrantResult).last(jcReturn).build())
                .build();

        statements.append(jcIfRequestCode);
    }

    JCBlock body = Block(typeNode)
            .last(statements.toList())
            .build();

    return Method(typeNode)
            .modifiers(Flags.PUBLIC)
            .returnType(returnType)
            .name("onRequestPermissionsResult")
            .paramType()
            .parameters(paramList)
            .thrown()
            .body(body)
            .build();
}