private void generateSetter( final TYPE_TYPE type, final FIELD_TYPE field, final AccessLevel level, final String propertyNameFieldName) { String fieldName = field.name(); boolean isBoolean = field.isOfType("boolean"); String setterName = TransformationsUtil.toSetterName(fieldName, isBoolean); if (type.hasMethod(setterName)) return; String oldValueName = OLD_VALUE_VARIABLE_NAME; List<lombok.ast.Annotation> nonNulls = field.annotations(TransformationsUtil.NON_NULL_PATTERN); MethodDecl methodDecl = MethodDecl(Type("void"), setterName) .withAccessLevel(level) .withArgument(Arg(field.type(), fieldName).withAnnotations(nonNulls)); if (!nonNulls.isEmpty() && !field.isPrimitive()) { methodDecl.withStatement( If(Equal(Name(fieldName), Null())) .Then(Throw(New(Type(NullPointerException.class)).withArgument(String(fieldName))))); } methodDecl .withStatement( LocalDecl(field.type(), oldValueName) .makeFinal() .withInitialization(Field(fieldName))) // .withStatement(Assign(Field(fieldName), Name(fieldName))) // .withStatement( Call(Call(PROPERTY_CHANGE_SUPPORT_METHOD_NAME), FIRE_PROPERTY_CHANGE_METHOD_NAME) // .withArgument(Name(propertyNameFieldName)) .withArgument(Name(oldValueName)) .withArgument(Field(fieldName))); type.injectMethod(methodDecl); }
@Override public JCTree visitMethodDecl(final lombok.ast.MethodDecl node, final Void p) { final JCModifiers mods = setGeneratedBy( M(node) .Modifiers( flagsFor(node.getModifiers()), build(node.getAnnotations(), JCAnnotation.class)), source); final JCExpression restype = build(node.getReturnType()); final List<JCTypeParameter> typarams = build(node.getTypeParameters()); final List<JCVariableDecl> params = build(node.getArguments()); final List<JCExpression> thrown = build(node.getThrownExceptions()); JCBlock body = null; if (!node.noBody() && ((mods.flags & Flags.ABSTRACT) == 0)) { body = setGeneratedBy(M(node).Block(0, build(node.getStatements(), JCStatement.class)), source); } final JCMethodDecl method = setGeneratedBy( M(node) .MethodDef( mods, name(node.getName()), restype, typarams, params, thrown, body, null), source); return method; }
private void generateSetter( final TYPE_TYPE type, final FIELD_TYPE field, final AccessLevel level, final boolean vetoable, final boolean throwVetoException, final String propertyNameFieldName) { String fieldName = field.filteredName(); boolean isBoolean = field.isOfType("boolean"); String setterName = toSetterName(field.getAnnotationValue(Accessors.class), field.name(), isBoolean); if (type.hasMethod(setterName, field.type())) return; String oldValueName = OLD_VALUE_VARIABLE_NAME; List<lombok.ast.Annotation> nonNulls = field.annotations(TransformationsUtil.NON_NULL_PATTERN); MethodDecl methodDecl = MethodDecl(Type("void"), setterName) .withAccessLevel(level) .withArgument(Arg(field.type(), fieldName).withAnnotations(nonNulls)); if (!nonNulls.isEmpty() && !field.isPrimitive()) { methodDecl.withStatement( If(Equal(Name(fieldName), Null())) .Then(Throw(New(Type(NullPointerException.class)).withArgument(String(fieldName))))); } methodDecl.withStatement( LocalDecl(field.type(), oldValueName).makeFinal().withInitialization(Field(fieldName))); if (vetoable) { if (throwVetoException) { methodDecl.withThrownException(Type(PropertyVetoException.class)); methodDecl.withStatement( Call(FIRE_VETOABLE_CHANGE_METHOD_NAME) // .withArgument(Name(propertyNameFieldName)) .withArgument(Name(oldValueName)) .withArgument(Name(fieldName))); } else { methodDecl.withStatement( Try(Block() .withStatement( Call(FIRE_VETOABLE_CHANGE_METHOD_NAME) // .withArgument(Name(propertyNameFieldName)) .withArgument(Name(oldValueName)) .withArgument(Name(fieldName)))) // .Catch( Arg(Type(PropertyVetoException.class), E_VALUE_VARIABLE_NAME), Block().withStatement(Return()))); } } methodDecl .withStatement(Assign(Field(fieldName), Name(fieldName))) // .withStatement( Call(FIRE_PROPERTY_CHANGE_METHOD_NAME) // .withArgument(Name(propertyNameFieldName)) .withArgument(Name(oldValueName)) .withArgument(Name(fieldName))); type.editor().injectMethod(methodDecl); }
public void delegateMethodsTo( TYPE_TYPE type, MethodDescriptor[] methodDescriptors, Expression<?> receiver, Map<String, String> methodMapper) { for (MethodDescriptor methodDesc : methodDescriptors) { List<TypeRef> argTypes = new ArrayList<>(); List<Argument> methodArgs = new ArrayList<>(); List<Expression<?>> callArgs = new ArrayList<>(); int argCounter = 0; for (MethodDescriptor.Type argType : methodDesc.arguments) { String argName = "arg" + argCounter++; TypeRef argTypeRef = asTypeRef(argType); argTypes.add(argTypeRef); Argument arg = Arg(argTypeRef, argName); if (argType.annotations.length > 0) { List<Annotation> annotations = new ArrayList<>(); for (int i = 0; i < argType.annotations.length; i++) { annotations.add(Annotation(asTypeRef(argType.annotations[i]))); } arg.withAnnotations(annotations); } methodArgs.add(arg); callArgs.add(Name(argName)); } if (type.hasMethodIncludingSupertypes( methodDesc.methodName, argTypes.toArray(new TypeRef[argTypes.size()]))) { continue; } String delegateMethodName = methodMapper.get(methodDesc.methodName); if (delegateMethodName == null) { delegateMethodName = methodDesc.methodName; } Call methodCall = Call(receiver, delegateMethodName).withArguments(callArgs); MethodDecl methodDecl = MethodDecl(asTypeRef(methodDesc.returnType), methodDesc.methodName) .makePublic() .withArguments(methodArgs); if (VOID.equals(methodDesc.returnType.type)) { methodDecl.withStatement(methodCall); } else { methodDecl.withStatement(Return(methodCall)); } if (methodDesc.typeParameters.length > 0) { List<TypeParam> types = new ArrayList<>(); for (int i = 0; i < methodDesc.typeParameters.length; i++) { types.add(asTypeParam(methodDesc.typeParameters[i])); } methodDecl.withTypeParameters(types); } if (methodDesc.exceptions.length > 0) { List<TypeRef> exceptions = new ArrayList<>(); for (int i = 0; i < methodDesc.exceptions.length; i++) { exceptions.add(asTypeRef(methodDesc.exceptions[i])); } methodDecl.withThrownExceptions(exceptions); } if (methodDesc.annotations.length > 0) { List<Annotation> annotations = new ArrayList<>(); for (int i = 0; i < methodDesc.annotations.length; i++) { annotations.add(Annotation(asTypeRef(methodDesc.annotations[i]))); } methodDecl.withAnnotations(annotations); } type.editor().injectMethod(methodDecl); } }