diff options
author | michaelbai@google.com <michaelbai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-30 20:22:24 +0000 |
---|---|---|
committer | michaelbai@google.com <michaelbai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-30 20:22:24 +0000 |
commit | 5877addc7e5c5add0c5fc7f738ee458497f258f7 (patch) | |
tree | 93aa4172f1bfcf2bf35d14a80d2260806b1aa760 /tools/android | |
parent | 20540b0bbcf1c1719d5e81a749729de386f157c8 (diff) | |
download | chromium_src-5877addc7e5c5add0c5fc7f738ee458497f258f7.zip chromium_src-5877addc7e5c5add0c5fc7f738ee458497f258f7.tar.gz chromium_src-5877addc7e5c5add0c5fc7f738ee458497f258f7.tar.bz2 |
Upstream findbugs plugin for Android
FindBugs plugin to detect synchronized method and synchronized this.
BUG=143971
Review URL: https://codereview.chromium.org/11263050
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164990 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/android')
-rw-r--r-- | tools/android/OWNERS | 1 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/README | 15 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/build.xml | 48 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/findbugs.xml | 18 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/findbugs_plugin.gyp | 17 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/lib/chromiumPlugin.jar | bin | 0 -> 3913 bytes | |||
-rw-r--r-- | tools/android/findbugs_plugin/messages.xml | 56 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedMethodDetector.java | 38 | ||||
-rw-r--r-- | tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedThisDetector.java | 73 |
9 files changed, 266 insertions, 0 deletions
diff --git a/tools/android/OWNERS b/tools/android/OWNERS index 56f9f0f..a715389 100644 --- a/tools/android/OWNERS +++ b/tools/android/OWNERS @@ -1,6 +1,7 @@ set noparent bulach@chromium.org jnd@chromium.org +michaelbai@chromium.org tonyg@chromium.org wangxianzhu@chromium.org yfriedman@chromium.org diff --git a/tools/android/findbugs_plugin/README b/tools/android/findbugs_plugin/README new file mode 100644 index 0000000..3ba3f53 --- /dev/null +++ b/tools/android/findbugs_plugin/README @@ -0,0 +1,15 @@ +This is the FindBugs plugin for chrome on android. + +Currently it detects: +- synchronized method +- synchronized 'this' + +We don't want the synchronized method and synchronized 'this' to be +used, the exception is the synchronized method defined in Android +API. + +The plugin jar file was prebuilt and checked in, to rebuild the +plugin, you need ant, and run below command, the new jar file will +be in lib directory. + +ant install diff --git a/tools/android/findbugs_plugin/build.xml b/tools/android/findbugs_plugin/build.xml new file mode 100644 index 0000000..09ee13c --- /dev/null +++ b/tools/android/findbugs_plugin/build.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2012 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can be + found in the LICENSE file. +--> + +<project name="findbugs_plugin" basedir="."> + + <description> + Build findbugs_plugin for Chromium Android + </description> + <property name="src.dir" location="src" /> + <property name="lib.dir" location="../../../third_party/findbugs/lib" /> + <property name="bin.dir" location="lib" /> + <property name="intermediate.dir" location="intermediate" /> + <property name="jar.name" value="chromiumPlugin.jar" /> + + <path id="classpath.id"> + <fileset dir="${lib.dir}"> + <include name="**/*.jar" /> + </fileset> + </path> + + <target name="makedir"> + <mkdir dir="${intermediate.dir}" /> + <mkdir dir="${bin.dir}" /> + </target> + + <target name="findbugs_plugin_classes" depends="makedir"> + <javac srcdir="${src.dir}" destdir="${intermediate.dir}" + classpathref="classpath.id" includeantruntime="false" /> + </target> + + <target name="copy_xml_files" depends="makedir"> + <copy file="messages.xml" todir="${intermediate.dir}" /> + <copy file="findbugs.xml" todir="${intermediate.dir}" /> + </target> + + <target name="findbugs_plugin_jar" depends="findbugs_plugin_classes, copy_xml_files"> + <jar destfile="${bin.dir}/${jar.name}" basedir="${intermediate.dir}"> + </jar> + </target> + + <target name="install" depends="findbugs_plugin_jar"> + <delete dir="${intermediate.dir}" /> + </target> +</project> diff --git a/tools/android/findbugs_plugin/findbugs.xml b/tools/android/findbugs_plugin/findbugs.xml new file mode 100644 index 0000000..43b1f34 --- /dev/null +++ b/tools/android/findbugs_plugin/findbugs.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2012 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can be + found in the LICENSE file. +--> + +<FindbugsPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="findbugsplugin.xsd" + pluginid="SynchronizedThisDetector" + provider="chromium" + website="http://code.google.com/p/chromium/wiki/UseFindBugsForAndroid"> + <Detector class="org.chromium.tools.findbugs.plugin.SynchronizedThisDetector" reports="CHROMIUM_SYNCHRONIZED_THIS" /> + <BugPattern type="CHROMIUM_SYNCHRONIZED_THIS" abbrev="CST" category="CORRECTNESS"/> + + <Detector class="org.chromium.tools.findbugs.plugin.SynchronizedMethodDetector" reports="CHROMIUM_SYNCHRONIZED_METHOD" /> + <BugPattern type="CHROMIUM_SYNCHRONIZED_METHOD" abbrev="CSM" category="CORRECTNESS"/> +</FindbugsPlugin> diff --git a/tools/android/findbugs_plugin/findbugs_plugin.gyp b/tools/android/findbugs_plugin/findbugs_plugin.gyp new file mode 100644 index 0000000..c966e76 --- /dev/null +++ b/tools/android/findbugs_plugin/findbugs_plugin.gyp @@ -0,0 +1,17 @@ +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'findbugs_plugin_test', + 'type': 'none', + 'variables': { + 'package_name': 'findbugs_plugin', + 'java_in_dir': 'test/java/', + }, + 'includes': [ '../../../build/java.gypi' ], + } + ] +} diff --git a/tools/android/findbugs_plugin/lib/chromiumPlugin.jar b/tools/android/findbugs_plugin/lib/chromiumPlugin.jar Binary files differnew file mode 100644 index 0000000..6ccf61b --- /dev/null +++ b/tools/android/findbugs_plugin/lib/chromiumPlugin.jar diff --git a/tools/android/findbugs_plugin/messages.xml b/tools/android/findbugs_plugin/messages.xml new file mode 100644 index 0000000..aea983b --- /dev/null +++ b/tools/android/findbugs_plugin/messages.xml @@ -0,0 +1,56 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Copyright (c) 2012 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can be + found in the LICENSE file. +--> + +<MessageCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="messagecollection.xsd"> + + <Plugin> + <ShortDescription>Chromium FindBugs Plugin </ShortDescription> + <Details>Adds style checks enforced in the chromium project.</Details> + </Plugin> + + <Detector class="org.chromium.tools.findbugs.plugin.SynchronizedThisDetector"> + <Details> + <![CDATA[ + Shouldn't use synchronized(this). + ]]> + </Details> + + </Detector> + + <BugPattern type="CHROMIUM_SYNCHRONIZED_THIS"> + <ShortDescription>Shouldn't use synchronized(this)</ShortDescription> + <LongDescription>Shouldn't use synchronized(this), please narrow down the synchronization scope.</LongDescription> + <Details> +<![CDATA[ +<p>Shouldn't use synchronized(this), please narrow down the synchronization scope.</p> +]]> + </Details> + </BugPattern> + + <Detector class="org.chromium.tools.findbugs.plugin.SynchronizedMethodDetector"> + <Details> + <![CDATA[ + Shouldn't use synchronized method. + ]]> + </Details> + + </Detector> + + <BugPattern type="CHROMIUM_SYNCHRONIZED_METHOD"> + <ShortDescription>Shouldn't use synchronized method</ShortDescription> + <LongDescription>Shouldn't use synchronized method, please narrow down the synchronization scope.</LongDescription> + <Details> +<![CDATA[ +<p>Shouldn't use synchronized method, please narrow down the synchronization scope.</p> +]]> + </Details> + </BugPattern> + + <BugCode abbrev="CHROMIUM">CHROMIUM</BugCode> +</MessageCollection> diff --git a/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedMethodDetector.java b/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedMethodDetector.java new file mode 100644 index 0000000..7a879f6 --- /dev/null +++ b/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedMethodDetector.java @@ -0,0 +1,38 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.tools.findbugs.plugin; + +import edu.umd.cs.findbugs.BugInstance; +import edu.umd.cs.findbugs.BugReporter; +import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; + +import org.apache.bcel.classfile.Code; + +/** + * This class detects the synchronized method. + */ +public class SynchronizedMethodDetector extends OpcodeStackDetector { + + private BugReporter mBugReporter; + + public SynchronizedMethodDetector(BugReporter bugReporter) { + this.mBugReporter = bugReporter; + } + + @Override + public void visit(Code code) { + if (getMethod().isSynchronized()) { + mBugReporter.reportBug(new BugInstance(this, "CHROMIUM_SYNCHRONIZED_METHOD", + NORMAL_PRIORITY) + .addClassAndMethod(this) + .addSourceLine(this)); + } + super.visit(code); + } + + @Override + public void sawOpcode(int arg0) { + } +} diff --git a/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedThisDetector.java b/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedThisDetector.java new file mode 100644 index 0000000..330431b --- /dev/null +++ b/tools/android/findbugs_plugin/src/org/chromium/tools/findbugs/plugin/SynchronizedThisDetector.java @@ -0,0 +1,73 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.tools.findbugs.plugin; + +import org.apache.bcel.classfile.Code; + +import edu.umd.cs.findbugs.BugInstance; +import edu.umd.cs.findbugs.BugReporter; +import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; + +/** + * This class detects the synchronized(this). + * + * The pattern of byte code of synchronized(this) is + * aload_0 # Load the 'this' pointer on top of stack + * dup # Duplicate the 'this' pointer + * astore_x # Store this for late use, it might be astore. + * monitorenter + */ +public class SynchronizedThisDetector extends OpcodeStackDetector { + private final int PATTERN[] = {ALOAD_0, DUP, 0xff, 0xff, MONITORENTER}; + + private int mStep = 0; + private BugReporter mBugReporter; + + public SynchronizedThisDetector(BugReporter bugReporter) { + mBugReporter = bugReporter; + } + + @Override + public void visit(Code code) { + mStep = 0; + super.visit(code); + } + + @Override + public void sawOpcode(int seen) { + if (PATTERN[mStep] == seen) { + mStep++; + if (mStep == PATTERN.length) { + mBugReporter.reportBug(new BugInstance(this, "CHROMIUM_SYNCHRONIZED_THIS", + NORMAL_PRIORITY) + .addClassAndMethod(this) + .addSourceLine(this)); + mStep = 0; + return; + } + } else if (mStep == 2) { + // This could be astore_x + switch (seen) { + case ASTORE_0: + case ASTORE_1: + case ASTORE_2: + case ASTORE_3: + mStep += 2; + break; + case ASTORE: + mStep++; + break; + default: + mStep = 0; + break; + } + } else if (mStep == 3) { + // Could be any byte following the ASTORE. + mStep++; + } else { + mStep = 0; + } + } +} |