/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2005, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.ba;

import edu.umd.cs.findbugs.annotations.CheckForNull;

/**
 * @author pugh
 */
public class NullnessAnnotationDatabase extends AnnotationDatabase<NullnessAnnotation> {
	
	public NullnessAnnotationDatabase() {
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.concurrent.package-info", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.concurrent.CopyOnWriteArrayList", NullnessAnnotation.UNKNOWN_NULLNESS);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.concurrent.CopyOnWriteArraySet", NullnessAnnotation.UNKNOWN_NULLNESS);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.concurrent.Exchanger", NullnessAnnotation.UNKNOWN_NULLNESS);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.concurrent.FutureTask", NullnessAnnotation.UNKNOWN_NULLNESS);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.PriorityQueue", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.SortedMap", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.SortedSet", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.EnumMap", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.PARAMETER, "java.util.EnumSet", NullnessAnnotation.NONNULL);
		addDefaultAnnotation(AnnotationDatabase.METHOD, "java.lang.String", NullnessAnnotation.NONNULL);
		// addMethodAnnotation("java.util.Queue", "poll", "()Ljava/lang/Object;", false, NullnessAnnotation.CHECK_FOR_NULL);
		addMethodAnnotation("java.io.BufferedReader", "readLine", "()Ljava/lang/String;", false, NullnessAnnotation.CHECK_FOR_NULL);
	}
	public boolean parameterMustBeNonNull(XMethod m, int param) {
		if (!anyAnnotations(NullnessAnnotation.NONNULL)) return false;
		XMethodParameter xmp = new XMethodParameter(m,param);
		NullnessAnnotation resolvedAnnotation = getResolvedAnnotation(xmp, true);
		// System.out.println("QQQ parameter " + param + " of " + m + " is " + resolvedAnnotation);
		return resolvedAnnotation == NullnessAnnotation.NONNULL;
	}
	
	@CheckForNull @Override
	public NullnessAnnotation getResolvedAnnotation(final Object o, boolean getMinimal) {
		
		if (o instanceof XMethodParameter) {
			XMethodParameter mp = (XMethodParameter) o;
			XMethod m = mp.getMethod();
			if (m.getName().startsWith("access$")) return null;
			if (mp.getParameterNumber() == 0 && m.getName().equals("equals") 
					&& m.getSignature().equals("(Ljava/lang/Object;)Z") && !m.isStatic())
					return NullnessAnnotation.CHECK_FOR_NULL;
			else if (mp.getParameterNumber() == 0 && m.getName().equals("compareTo") 
					&& m.getSignature().endsWith(";)Z") && !m.isStatic())
					return NullnessAnnotation.NONNULL;
		}
		else if (o instanceof XMethod) {
			XMethod m = (XMethod) o;
			if (m.getName().startsWith("access$")) return null;

		}
		return super.getResolvedAnnotation(o, getMinimal);
	}
}

@@ -1704,32 +1704,572 @@
 NOWN_NULLNESS);%0A
+%09%09addMethodParameterAnnotation(%22java.util.concurrent.ThreadPoolExecutor%22, %22addIfUnderCorePoolSize%22, %22(Ljava/lang/Runnable;)Z%22, false, 0, NullnessAnnotation.CHECK_FOR_NULL);%0A%09%09addMethodParameterAnnotation(%22java.util.concurrent.ThreadPoolExecutor%22, %22addThread%22, %22(Ljava/lang/Runnable;)Ljava/lang/Thread;%22, false, 0, NullnessAnnotation.CHECK_FOR_NULL);%0A%09%09addMethodParameterAnnotation(%22java.util.concurrent.ThreadPoolExecutor%22, %22afterExecute%22, %22(Ljava/lang/Runnable;Ljava/lang/Throwable;)V%22, false, 0, NullnessAnnotation.CHECK_FOR_NULL);%0A%09%09%0A%09%09%0A
 %09%09addDefaultAnno

mark some parameters as being checkfornull rather than nonnull