lombok.experimental.FieldDefaults Java Examples

The following examples show how to use lombok.experimental.FieldDefaults. 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: HandleFieldDefaults.java    From EasyMPermission with MIT License 6 votes vote down vote up
public void handle(AnnotationValues<FieldDefaults> annotation, Annotation ast, EclipseNode annotationNode) {
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_DEFAULTS_FLAG_USAGE, "@FieldDefaults");
	
	EclipseNode node = annotationNode.up();
	FieldDefaults instance = annotation.getInstance();
	AccessLevel level = instance.level();
	boolean makeFinal = instance.makeFinal();
	
	if (level == AccessLevel.NONE && !makeFinal) {
		annotationNode.addError("This does nothing; provide either level or makeFinal or both.");
		return;
	}
	
	if (level == AccessLevel.PACKAGE) {
		annotationNode.addError("Setting 'level' to PACKAGE does nothing. To force fields as package private, use the @PackagePrivate annotation on the field.");
	}
	
	if (!makeFinal && annotation.isExplicit("makeFinal")) {
		annotationNode.addError("Setting 'makeFinal' to false does nothing. To force fields to be non-final, use the @NonFinal annotation on the field.");
	}
	
	if (node == null) return;
	
	generateFieldDefaultsForType(node, annotationNode, level, makeFinal, false);
}
 
Example #2
Source File: HandleFieldDefaults.java    From EasyMPermission with MIT License 5 votes vote down vote up
public boolean generateFieldDefaultsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean makeFinal, boolean checkForTypeLevelFieldDefaults) {
	if (checkForTypeLevelFieldDefaults) {
		if (hasAnnotation(FieldDefaults.class, typeNode)) {
			//The annotation will make it happen, so we can skip it.
			return true;
		}
	}
	
	JCClassDecl typeDecl = null;
	if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
	long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
	boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
	
	if (typeDecl == null || notAClass) {
		errorNode.addError("@FieldDefaults is only supported on a class or an enum.");
		return false;
	}
	
	for (JavacNode field : typeNode.down()) {
		if (field.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		
		setFieldDefaultsForField(field, errorNode.get(), level, makeFinal);
	}
	
	return true;
}
 
Example #3
Source File: HandleFieldDefaults.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void handle(AnnotationValues<FieldDefaults> annotation, JCAnnotation ast, JavacNode annotationNode) {
	handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_DEFAULTS_FLAG_USAGE, "@FieldDefaults");
	
	deleteAnnotationIfNeccessary(annotationNode, FieldDefaults.class);
	deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
	JavacNode node = annotationNode.up();
	FieldDefaults instance = annotation.getInstance();
	AccessLevel level = instance.level();
	boolean makeFinal = instance.makeFinal();
	
	if (level == AccessLevel.NONE && !makeFinal) {
		annotationNode.addError("This does nothing; provide either level or makeFinal or both.");
		return;
	}
	
	if (level == AccessLevel.PACKAGE) {
		annotationNode.addError("Setting 'level' to PACKAGE does nothing. To force fields as package private, use the @PackagePrivate annotation on the field.");
	}
	
	if (!makeFinal && annotation.isExplicit("makeFinal")) {
		annotationNode.addError("Setting 'makeFinal' to false does nothing. To force fields to be non-final, use the @NonFinal annotation on the field.");
	}
	
	if (node == null) return;
	
	generateFieldDefaultsForType(node, annotationNode, level, makeFinal, false);
}