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.List;
 
 
 
Looks for methods that declare method level template parameter(s) that are not bound to any of the method's parameters, and thus is not adding any validation/type safety to the method, and is just confusing.
 
 public class UnboundMethodTemplateParameter extends PreorderVisitor implements Detector {
 
     private static final Pattern TEMPLATED_SIGNATURE = Pattern.compile("(\\<[^\\>]+\\>)(.+)");
     private static final Pattern TEMPLATE = Pattern.compile("\\<?([^:]+):[^;]*;");
     private final BugReporter bugReporter;
 
 
     public UnboundMethodTemplateParameter(BugReporter bugReporter) {
         this. = bugReporter;
     }

    
implements the visitor to accept the class for visiting

Parameters:
classContext the context object of the currently parsed class
 
     @Override
     public void visitClassContext(ClassContext classContext) {
         JavaClass cls = classContext.getJavaClass();
         cls.accept(this);
     }

    
implements the visitor to find methods that declare template parameters that are not bound to any parameter.

Parameters:
obj the context object of the currently parsed method
 
     @Override
     public void visitMethod(Method obj) {
         Attribute[] attributes = obj.getAttributes();
         for (Attribute a : attributes) {
             if ("Signature".equals(a.getName())) {
                 TemplateSignature ts = parseSignatureAttribute((Signaturea);
                 if (ts != null) {
                     for (String templateParm : ts.templateParameters) {
                         if (!ts.signature.contains("T" + templateParm + ";") && !ts.signature.contains("[T" + templateParm + ";")) {
                             .reportBug(new BugInstance(this..name(), )
                                         .addClass(this)
                                         .addMethod(this)
                                         .addString("Template Parameter: " + templateParm));
                             return;
                         }
                     }
                 }
                 return;
             }
         }
     }
 
     @Override
     public void report() {
     }

    
builds a template signature object based on the signature attribute of the method

Parameters:
signatureAttribute the signature attribute
Returns:
a template signature if there are templates defined, otherwise null
    private static TemplateSignature parseSignatureAttribute(Signature signatureAttribute) {
        Matcher m = .matcher(signatureAttribute.getSignature());
        if (m.matches()) {
            TemplateSignature ts = new TemplateSignature();
            ts.signature = m.group(2);
            String template = m.group(1);
            m = .matcher(template);
            List<Stringtemplates = new ArrayList<String>(4);
            while (m.find()) {
                templates.add(m.group(1));
            }
            ts.templateParameters = templates.toArray(new String[templates.size()]);
            return ts;
        }
        return null;
    }

    
a simple data only class for holding the template parameters and method signature
    static class TemplateSignature {
        String[] templateParameters;
        String signature;
        
        @Override
        public String toString() {
            return ToString.build(this);
        }
    }
New to GrepCode? Check out our FAQ X