Java Code Examples for lombok.javac.JavacNode#addWarning()

The following examples show how to use lombok.javac.JavacNode#addWarning() . 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: HandleLog.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode, String loggerTopic) {
	deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass());

	JavacNode typeNode = annotationNode.up();
	switch (typeNode.getKind()) {
	case TYPE:
		String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME);
		if (logFieldName == null) logFieldName = "log";
		
		boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC));
		
		if ((((JCClassDecl)typeNode.get()).mods.flags & Flags.INTERFACE) != 0) {
			annotationNode.addError("@Log is legal only on classes and enums.");
			return;
		}
		if (fieldExists(logFieldName, typeNode) != MemberExistsResult.NOT_EXISTS) {
			annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
			return;
		}

		JCFieldAccess loggingType = selfType(typeNode);
		createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic, loggerTopic);
		break;
	default:
		annotationNode.addError("@Log is legal only on types.");
		break;
	}
}
 
Example 2
Source File: HandleConstructor.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, String staticName, SkipIfConstructorExists skipIfConstructorExists, Boolean suppressConstructorProperties, JavacNode source) {
	boolean staticConstrRequired = staticName != null && !staticName.equals("");
	
	if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
	if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
		for (JavacNode child : typeNode.down()) {
			if (child.getKind() == Kind.ANNOTATION) {
				boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) ||
						annotationTypeMatches(AllArgsConstructor.class, child) ||
						annotationTypeMatches(RequiredArgsConstructor.class, child);
				
				if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
					skipGeneration = annotationTypeMatches(Builder.class, child);
				}
				
				if (skipGeneration) {
					if (staticConstrRequired) {
						// @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
						// will take care of it. However, @Data also wants a specific static name; this will be ignored; the appropriate way to do this is to use
						// the 'staticName' parameter of the @XArgsConstructor you've stuck on your type.
						// We should warn that we're ignoring @Data's 'staticConstructor' param.
						source.addWarning("Ignoring static constructor name: explicit @XxxArgsConstructor annotation present; its `staticName` parameter will be used.");
					}
					return;
				}
			}
		}
	}
	
	JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, suppressConstructorProperties, source);
	injectMethod(typeNode, constr);
	if (staticConstrRequired) {
		JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, fields, source.get());
		injectMethod(typeNode, staticConstr);
	}
}
 
Example 3
Source File: HandleGetter.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) {
	handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_FLAG_USAGE, "@Getter");
	
	Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
	deleteAnnotationIfNeccessary(annotationNode, Getter.class);
	deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
	JavacNode node = annotationNode.up();
	Getter annotationInstance = annotation.getInstance();
	AccessLevel level = annotationInstance.value();
	boolean lazy = annotationInstance.lazy();
	if (lazy) handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_LAZY_FLAG_USAGE, "@Getter(lazy=true)");
	
	if (level == AccessLevel.NONE) {
		if (lazy) annotationNode.addWarning("'lazy' does not work with AccessLevel.NONE.");
		return;
	}
	
	if (node == null) return;
	
	List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Getter(onMethod=", annotationNode);
	
	switch (node.getKind()) {
	case FIELD:
		createGetterForFields(level, fields, annotationNode, true, lazy, onMethod);
		break;
	case TYPE:
		if (!onMethod.isEmpty()) {
			annotationNode.addError("'onMethod' is not supported for @Getter on a type.");
		}
		if (lazy) annotationNode.addError("'lazy' is not supported for @Getter on a type.");
		generateGetterForType(node, annotationNode, level, false);
		break;
	}
}
 
Example 4
Source File: HandleSneakyThrows.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateEmptyBlockWarning(JavacNode methodNode, JavacNode annotation, boolean hasConstructorCall) {
	if (hasConstructorCall) {
		annotation.addWarning("Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor.");
	} else {
		annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored.");
	}
}
 
Example 5
Source File: HandleCleanup.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void doAssignmentCheck0(JavacNode node, JCTree statement, Name name) {
	if (statement instanceof JCAssign) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name);
	if (statement instanceof JCExpressionStatement) doAssignmentCheck0(node,
			((JCExpressionStatement)statement).expr, name);
	if (statement instanceof JCVariableDecl) doAssignmentCheck0(node, ((JCVariableDecl)statement).init, name);
	if (statement instanceof JCTypeCast) doAssignmentCheck0(node, ((JCTypeCast)statement).expr, name);
	if (statement instanceof JCIdent) {
		if (((JCIdent)statement).name.contentEquals(name)) {
			JavacNode problemNode = node.getNodeFor(statement);
			if (problemNode != null) problemNode.addWarning(
			"You're assigning an auto-cleanup variable to something else. This is a bad idea.");
		}
	}
}
 
Example 6
Source File: HandleGetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createGetterForField(AccessLevel level,
		JavacNode fieldNode, JavacNode source, boolean whineIfExists, boolean lazy, List<JCAnnotation> onMethod) {
	if (fieldNode.getKind() != Kind.FIELD) {
		source.addError("@Getter is only supported on a class or a field.");
		return;
	}
	
	JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
	
	if (lazy) {
		if ((fieldDecl.mods.flags & Flags.PRIVATE) == 0 || (fieldDecl.mods.flags & Flags.FINAL) == 0) {
			source.addError("'lazy' requires the field to be private and final.");
			return;
		}
		if (fieldDecl.init == null) {
			source.addError("'lazy' requires field initialization.");
			return;
		}
	}
	
	String methodName = toGetterName(fieldNode);
	
	if (methodName == null) {
		source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	for (String altName : toAllGetterNames(fieldNode)) {
		switch (methodExists(altName, fieldNode, false, 0)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
				source.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
	
	injectMethod(fieldNode.up(), createGetter(access, fieldNode, fieldNode.getTreeMaker(), source.get(), lazy, onMethod));
}
 
Example 7
Source File: HandleSetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode sourceNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
	if (fieldNode.getKind() != Kind.FIELD) {
		fieldNode.addError("@Setter is only supported on a class or a field.");
		return;
	}
	
	JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
	String methodName = toSetterName(fieldNode);
	
	if (methodName == null) {
		fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
		fieldNode.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
		return;
	}
	
	for (String altName : toAllSetterNames(fieldNode)) {
		switch (methodExists(altName, fieldNode, false, 1)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
				fieldNode.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
	
	JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
	injectMethod(fieldNode.up(), createdSetter);
}
 
Example 8
Source File: HandleWither.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
	if (fieldNode.getKind() != Kind.FIELD) {
		fieldNode.addError("@Wither is only supported on a class or a field.");
		return;
	}
	
	JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
	String methodName = toWitherName(fieldNode);
	
	if (methodName == null) {
		fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	if ((fieldDecl.mods.flags & Flags.STATIC) != 0) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
		return;
	}
	
	if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
		return;
	}
	
	if (fieldDecl.name.toString().startsWith("$")) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
		return;
	}
	
	for (String altName : toAllWitherNames(fieldNode)) {
		switch (methodExists(altName, fieldNode, false, 1)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
				fieldNode.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	long access = toJavacModifier(level);
	
	JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source, onMethod, onParam);
	injectMethod(fieldNode.up(), createdWither);
}