Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2015 Dave Brosius
   *
   * 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 com.mebigfatguy.fbcontrib.detect;
 
 import java.util.Map;
 import java.util.Set;
 
 
 
looks for interfaces that ignore 508 compliance, including not using JLabel.setLabelFor, Using null layouts,
 
 {
     private static final String SAW_TEXT_LABEL = "SAW_TEXT_LABEL";
     private static final String FROM_UIMANAGER = "FROM_UIMANAGER";
     private static final String APPENDED_STRING = "APPENDED_STRING";
 
 	private static JavaClass windowClass;
 	private static JavaClass componentClass;
 	private static JavaClass jcomponentClass;
 	private static JavaClass accessibleClass;
 	static {
 		try {
 			 = Repository.lookupClass("java/awt/Window");
 		} catch (ClassNotFoundException cnfe) {
 			 = null;
 			 = cnfe;
 		}
 		try {
 			 = Repository.lookupClass("java/awt/Component");
 		} catch (ClassNotFoundException cnfe) {
 			 = null;
 			 = cnfe;
 		}
 		try {
 			 = Repository.lookupClass("javax/swing/JComponent");
 		} catch (ClassNotFoundException cnfe) {
 			 = null;
 			 = cnfe;
 		}
 		try {
 			 = Repository.lookupClass("javax/accessibility/Accessible");
 		} catch (ClassNotFoundException cnfe) {
 			 = null;
 			 = cnfe;
 		}
 	}
 
 	private static final Map<StringIntegerdisplayTextMethods = new HashMap<StringInteger>();
 	static {
 		.put("javax/swing/JLabel#<init>(Ljava/lang/String;)".);
 		.put("javax/swing/JLabel#<init>(Ljava/lang/String;Ljavax/swing/Icon;I)".);
 		.put("javax/swing/JLabel#<init>(Ljava/lang/String;I)".);
 		.put("javax/swing/JButton#<init>(Ljava/lang/String;)".);
 		.put("javax/swing/JButton#<init>(Ljava/lang/String;Ljavax/swing/Icon;)".);
 		.put("javax/swing/JFrame#<init>(Ljava/lang/String;)".);
 		.put("javax/swing/JFrame#<init>(Ljava/lang/String;Ljava/awt/GraphicsConfiguration;)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Dialog;Ljava/lang/String;)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Dialog;Ljava/lang/String;Z)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Dialog;Ljava/lang/String;ZLjava/awt/GraphicsConfiguration;)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Frame;Ljava/lang/String;)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Frame;Ljava/lang/String;Z)".);
		.put("javax/swing/JDialog#<init>(Ljava/awt/Frame;Ljava/lang/String;ZLjava/awt/GraphicsConfiguration;)".);
		.put("java/awt/Dialog#setTitle(Ljava/lang/String;)".);
		.put("java/awt/Frame#setTitle(Ljava/lang/String;)".);
		.put("javax/swing/JMenu#<init>(Ljava/lang/String;)".);
		.put("javax/swing/JMenu#<init>(Ljava/lang/String;Z)".);
		.put("javax/swing/JMenuItem#<init>(Ljava/lang/String;)".);
		.put("javax/swing/JMenuItem#<init>(Ljava/lang/String;Ljavax/swing/Icon;)".);
		.put("javax/swing/JMenuItem#<init>(Ljava/lang/String;I)".);
	}
	private final BugReporter bugReporter;
	private OpcodeStack stack;
constructs a S508C detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
	public Section508Compliance(BugReporter bugReporter) {
		this. = bugReporter;
		if ( != null)
	}

implements the visitor to create and clear the stack

Parameters:
classContext the context object of the currently visited class
	public void visitClassContext(ClassContext classContext) {
		try {
			if (( != null) && ( != null)) {
				JavaClass cls = classContext.getJavaClass();
						.addClass(cls));
					}
				}
			}
			 = new OpcodeStack();
			super.visitClassContext(classContext);
			for (XField fa : ) {
				.addClass(this)
				.addField(fa));
			}
catch (ClassNotFoundException cnfe) {
finally {
			 = null;
			 = null;
			 = null;
		}
	}

looks for fields that are JLabels and stores them in a set

Parameters:
obj the field object of the current field
	public void visitField(Field obj) {
		String fieldSig = obj.getSignature();
		if ("Ljavax/swing/JLabel;".equals(fieldSig)) {
			FieldAnnotation fa = FieldAnnotation.fromVisitedField(this);
		}
	}

implements the visitor to reset the stack

Parameters:
obj the context object for the currently visited code block
	public void visitCode(Code obj) {
		super.visitCode(obj);
			.addClass(this)
			.addMethod(this);
			if (sla != null) {
				bug.addSourceLine(sla);
			}
		}
	}

implements the visitor to find 508 compliance concerns

Parameters:
seen the opcode of the currently parsed instruction
	public void sawOpcode(int seen) {
		boolean sawTextLabel = false;
		boolean sawUIManager = false;
		boolean sawAppend = false;
		try {
	        .precomputation(this);
	        
			if ((seen == ) || ((seen >= ) && (seen <= ))) {
				if (.getStackDepth() > 0) {
					if ("Ljavax/swing/JLabel;".equals(item.getSignature())
						int reg = RegisterUtils.getAStoreReg(thisseen);
						.put(Integer.valueOf(reg), SourceLineAnnotation.fromVisitedInstruction(this));
					}
				}
else if (seen == ) {
				if (.getStackDepth() > 0) {
					}
				}
else if (seen == ) {
				String methodName = getNameConstantOperand();
				if ("javax/swing/JLabel".equals(className)
						&&  ..equals(methodName)) {
					String signature = getSigConstantOperand();
					if (signature.indexOf("Ljava/lang/String;") >= 0) {
						sawTextLabel = true;
					}
				}
else if (seen == ) {
				String methodName = getNameConstantOperand();
				if ("javax/swing/JLabel".equals(className)) {
					if ("setLabelFor".equals(methodName)) {
						if (.getStackDepth() > 1) {
							XField field = item.getXField();
							if (field != null) {
else {
								int reg = item.getRegisterNumber();
								if (reg >= 0) {
									.remove(Integer.valueOf(reg));
								}
							}
						}
					}
else if ("java/lang/StringBuffer".equals(className) || "java/lang/StringBuilder".equals(className)) {
				    if ("append".equals(methodName)) {
				        if (.getStackDepth() > 0) {
				            OpcodeStack.Item item = .getStackItem(0);
				            Object con = item.getConstant();
				            if (con instanceof String) {
				                String literal = (String)con;
				                sawAppend = !literal.startsWith("<");
				            } else {
				                sawAppend = true;
				            }
				        }
				    } else if ("toString".equals(methodName)) {
				        if (.getStackDepth() > 0) {
				            OpcodeStack.Item item = .getStackItem(0);
				            if (.equals(item.getUserValue())) {
				                sawAppend = true;
				            }
				        }
				    }
				}
				processSetSizeOps(methodName);
                processNullLayouts(classNamemethodName);
				processSetColorOps(methodName);
else if (seen == ) {
			    if ("javax/swing/UIManager".equals(getClassConstantOperand())) {
			        sawUIManager = true;
			    }
			}
			if ((seen == ) || (seen == ) || (seen == )) {
			}
catch (ClassNotFoundException cnfe) {
finally {
			TernaryPatcher.pre(seen);
			.sawOpcode(thisseen);
			TernaryPatcher.post(seen);
			if (sawTextLabel) {
				if (.getStackDepth() > 0) {
				}
else if (sawUIManager) {
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
                    item.setUserValue();
                }
else if (sawAppend) {
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
                    item.setUserValue();
                }
			}
		}
	}

looks for calls to set a readable string that is generated from a static constant, as these strings are not translatable. also looks for setting readable strings that are appended together. This is likely not to be internationalizable.
	private void processFaultyGuiStrings() {
        StringBuilder methodInfo = new StringBuilder();
        methodInfo.append(getClassConstantOperand());
        methodInfo.append("#");
        methodInfo.append(getNameConstantOperand());
        String signature = getSigConstantOperand();
        signature = signature.substring(0, signature.indexOf(')') + 1);
        methodInfo.append(signature);
        Integer parmIndex = .get(methodInfo.toString());
        if (parmIndex != null) {
            if (.getStackDepth() > parmIndex.intValue()) {
                OpcodeStack.Item item = .getStackItem(parmIndex.intValue());
                if (item.getConstant() != null) {
                    .reportBug(new BugInstance(this..name(), )
                                .addClass(this)
                                .addMethod(this)
                                .addSourceLine(this));
                } else if (.equals(item.getUserValue())) {
                    .reportBug(new BugInstance(this..name(), )
                    .addClass(this)
                    .addMethod(this)
                    .addSourceLine(this));
                }
            }
        }
	}

looks for containers where a null layout is installed

Parameters:
className class that a method call is made on
methodName name of the method that is called
	private void processNullLayouts(String classNameString methodName) {
        if ("java/awt/Container".equals(className)) {
            if ("setLayout".equals(methodName)) {
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
                    if (item.isNull()) {
                        .reportBug(new BugInstance(this..name(), )
                        .addClass(this)
                        .addMethod(this)
                        .addSourceLine(this));
                    }
                }
            }
        }
	}

looks for calls to set the color of components where the color isn't from UIManager

Parameters:
methodName the method that is called
Throws:
java.lang.ClassNotFoundException if the gui component class can't be found
	private void processSetColorOps(String methodNamethrows ClassNotFoundException {
        if ("setBackground".equals(methodName)
                ||  "setForeground".equals(methodName)) {
            int argCount = Type.getArgumentTypes(getSigConstantOperand()).length;
            if (.getStackDepth() > argCount) {
                OpcodeStack.Item item = .getStackItem(0);
                if (!.equals(item.getUserValue())) {
                    item = .getStackItem(argCount);
                    JavaClass cls = item.getJavaClass();
                    if ((( != null) && cls.instanceOf())
                            ||  (( != null) && cls.instanceOf())) {
                        .reportBug(new BugInstance(this..name(), )
                        .addClass(this)
                        .addMethod(this)
                        .addSourceLine(this));
                    }
                }
            }
        }
	}

looks for calls to setSize on components, rather than letting the layout manager set them

Parameters:
methodName the method that was called on a component
Throws:
java.lang.ClassNotFoundException if the gui class wasn't found
	private void processSetSizeOps(String methodNamethrows ClassNotFoundException {
	    if ("setSize".equals(methodName)) {
            int argCount = Type.getArgumentTypes(getSigConstantOperand()).length;
            if (( != null) && (.getStackDepth() > argCount)) {
                OpcodeStack.Item item = .getStackItem(argCount);
                JavaClass cls = item.getJavaClass();
                if ((cls != null) && cls.instanceOf()) {
                    .reportBug(new BugInstance(this..name(), )
                    .addClass(this)
                    .addMethod(this)
                    .addSourceLine(this));
                }
            }
        }
	}
New to GrepCode? Check out our FAQ X