8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks
Generalizing the crules infrastructure, adding a new analyzer to check String concatenation in Assert calls. Reviewed-by: jjg, vromero
This commit is contained in:
parent
5284120119
commit
66ea31ef63
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
# Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
#
|
||||
# This code is free software; you can redistribute it and/or modify it
|
||||
@ -168,6 +168,8 @@ sjavac.includes = \
|
||||
sjavac.tests = \
|
||||
tools/sjavac
|
||||
|
||||
crules.tests = ../make/test/crules
|
||||
|
||||
#
|
||||
|
||||
# The following files require the latest JDK to be available.
|
||||
|
@ -760,7 +760,8 @@
|
||||
**** crules targets.
|
||||
-->
|
||||
|
||||
<target name="build-crules" depends="-def-compilecrules,-def-build-jar-with-services">
|
||||
<target name="build-crules"
|
||||
depends="-def-compilecrules,-def-build-jar-with-services,build-bootstrap-javac,-create-import-jdk-stubs">
|
||||
<compilecrules/>
|
||||
<build-jar-with-services
|
||||
name="crules"
|
||||
@ -770,13 +771,19 @@
|
||||
jarmainclass=""
|
||||
jarclasspath="crules.jar"
|
||||
service.type="com.sun.source.util.Plugin"
|
||||
service.provider="crules.MutableFieldsAnalyzer"/>
|
||||
service.provider="crules.CodingRulesAnalyzerPlugin"/>
|
||||
<build-tool name="crules"/>
|
||||
</target>
|
||||
|
||||
<target name="jtreg-crules" depends="build-javac,build-crules,-def-jtreg">
|
||||
<jtreg-tool name="crules"
|
||||
tests="${crules.tests}"
|
||||
extra.jvmargs="-Xbootclasspath/a:${build.toolclasses.dir}/crules.jar" />
|
||||
</target>
|
||||
|
||||
<target name="check-coding-rules" depends="build-bootstrap-javac,-create-import-jdk-stubs,build-crules">
|
||||
<build-classes includes="${javac.includes}"
|
||||
plugin.options="-J-Xbootclasspath/a:${build.toolclasses.dir}/crules.jar -Xplugin:mutable_fields_analyzer" />
|
||||
plugin.options="-J-Xbootclasspath/a:${build.toolclasses.dir}/crules.jar -Xplugin:coding_rules" />
|
||||
</target>
|
||||
|
||||
<!--
|
||||
@ -1150,6 +1157,7 @@
|
||||
<attribute name="options" default="${other.jtreg.options}"/>
|
||||
<attribute name="keywords" default="-keywords:!ignore"/>
|
||||
<attribute name="jpda.jvmargs" default=""/>
|
||||
<attribute name="extra.jvmargs" default=""/>
|
||||
<sequential>
|
||||
<property name="coverage.options" value=""/> <!-- default -->
|
||||
<property name="coverage.classpath" value=""/> <!-- default -->
|
||||
@ -1163,7 +1171,7 @@
|
||||
samevm="@{samevm}" verbose="@{verbose}"
|
||||
failonerror="false" resultproperty="jtreg.@{name}.result"
|
||||
javacoptions="-g"
|
||||
vmoptions="${coverage.options} -Xbootclasspath/p:${coverage.classpath}${path.separator}${build.classes.dir} @{jpda.jvmargs}">
|
||||
vmoptions="${coverage.options} -Xbootclasspath/p:${coverage.classpath}${path.separator}${build.classes.dir} @{jpda.jvmargs} @{extra.jvmargs}">
|
||||
<arg line="@{keywords}"/>
|
||||
<arg line="@{options}"/>
|
||||
<arg line="@{tests}"/>
|
||||
|
@ -0,0 +1,11 @@
|
||||
/**@test /nodynamiccopyright/
|
||||
* @compile/fail/ref=Test.out -Xplugin:coding_rules -XDrawDiagnostics Test.java
|
||||
*/
|
||||
|
||||
import com.sun.tools.javac.util.Assert;
|
||||
|
||||
public class Test {
|
||||
public void check(String value) {
|
||||
Assert.check(value.trim().length() > 0, "value=" + value);
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
Test.java:9:21: compiler.err.proc.messager: compiler.misc.crules.should.not.use.string.concatenation
|
||||
1 error
|
@ -0,0 +1,9 @@
|
||||
/**@test /nodynamiccopyright/
|
||||
* @compile/fail/ref=Test.out -Xplugin:coding_rules -XDrawDiagnostics Test.java
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac;
|
||||
|
||||
public class Test {
|
||||
public static String mutable;
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
Test.java:8:26: compiler.err.proc.messager: compiler.misc.crules.err.var.must.be.final: public static String mutable
|
||||
1 error
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -27,64 +27,43 @@ import java.text.MessageFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.Plugin;
|
||||
import com.sun.source.util.TaskEvent;
|
||||
import com.sun.source.util.TaskListener;
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.api.BasicJavacTask;
|
||||
import com.sun.tools.javac.code.Symtab;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.TreeScanner;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.JCDiagnostic;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
|
||||
import com.sun.tools.javac.util.Log;
|
||||
import com.sun.tools.javac.util.Options;
|
||||
import com.sun.tools.javac.util.RawDiagnosticFormatter;
|
||||
|
||||
import static com.sun.source.util.TaskEvent.Kind;
|
||||
|
||||
public abstract class AbstractCodingRulesAnalyzer implements Plugin {
|
||||
public abstract class AbstractCodingRulesAnalyzer {
|
||||
|
||||
protected Log log;
|
||||
protected Trees trees;
|
||||
private final Log log;
|
||||
private final boolean rawDiagnostics;
|
||||
private final JCDiagnostic.Factory diags;
|
||||
private final Options options;
|
||||
protected final Messages messages;
|
||||
protected final Symtab syms;
|
||||
protected TreeScanner treeVisitor;
|
||||
protected Kind eventKind;
|
||||
protected Messages messages;
|
||||
|
||||
public void init(JavacTask task, String... args) {
|
||||
public AbstractCodingRulesAnalyzer(JavacTask task) {
|
||||
BasicJavacTask impl = (BasicJavacTask)task;
|
||||
Context context = impl.getContext();
|
||||
log = Log.instance(context);
|
||||
trees = Trees.instance(task);
|
||||
options = Options.instance(context);
|
||||
rawDiagnostics = options.isSet("rawDiagnostics");
|
||||
diags = JCDiagnostic.Factory.instance(context);
|
||||
messages = new Messages();
|
||||
task.addTaskListener(new PostAnalyzeTaskListener());
|
||||
syms = Symtab.instance(context);
|
||||
}
|
||||
|
||||
public class PostAnalyzeTaskListener implements TaskListener {
|
||||
|
||||
@Override
|
||||
public void started(TaskEvent taskEvent) {}
|
||||
|
||||
@Override
|
||||
public void finished(TaskEvent taskEvent) {
|
||||
if (taskEvent.getKind().equals(eventKind)) {
|
||||
TypeElement typeElem = taskEvent.getTypeElement();
|
||||
Tree tree = trees.getTree(typeElem);
|
||||
if (tree != null) {
|
||||
JavaFileObject prevSource = log.currentSourceFile();
|
||||
try {
|
||||
log.useSource(taskEvent.getCompilationUnit().getSourceFile());
|
||||
treeVisitor.scan((JCTree)tree);
|
||||
} finally {
|
||||
log.useSource(prevSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Messages {
|
||||
protected class Messages {
|
||||
ResourceBundle bundle;
|
||||
|
||||
Messages() {
|
||||
@ -93,7 +72,14 @@ public abstract class AbstractCodingRulesAnalyzer implements Plugin {
|
||||
}
|
||||
|
||||
public void error(JCTree tree, String code, Object... args) {
|
||||
String msg = (code == null) ? (String) args[0] : localize(code, args);
|
||||
String msg;
|
||||
if (rawDiagnostics) {
|
||||
RawDiagnosticFormatter f = new RawDiagnosticFormatter(options);
|
||||
msg = f.formatMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(),
|
||||
tree.pos(), code, args), null);
|
||||
} else {
|
||||
msg = (code == null) ? (String) args[0] : localize(code, args);
|
||||
}
|
||||
log.error(tree, "proc.messager", msg.toString());
|
||||
}
|
||||
|
||||
|
64
langtools/make/tools/crules/AssertCheckAnalyzer.java
Normal file
64
langtools/make/tools/crules/AssertCheckAnalyzer.java
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package crules;
|
||||
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.TaskEvent.Kind;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
|
||||
import com.sun.tools.javac.tree.JCTree.Tag;
|
||||
import com.sun.tools.javac.tree.TreeInfo;
|
||||
import com.sun.tools.javac.tree.TreeScanner;
|
||||
import com.sun.tools.javac.util.Assert;
|
||||
|
||||
public class AssertCheckAnalyzer extends AbstractCodingRulesAnalyzer {
|
||||
|
||||
public AssertCheckAnalyzer(JavacTask task) {
|
||||
super(task);
|
||||
treeVisitor = new AssertCheckVisitor();
|
||||
eventKind = Kind.ANALYZE;
|
||||
}
|
||||
|
||||
class AssertCheckVisitor extends TreeScanner {
|
||||
|
||||
@Override
|
||||
public void visitApply(JCMethodInvocation tree) {
|
||||
Symbol method = TreeInfo.symbolFor(tree);
|
||||
if (method != null &&
|
||||
method.owner.getQualifiedName().contentEquals(Assert.class.getName()) &&
|
||||
!method.name.contentEquals("error")) {
|
||||
JCExpression lastParam = tree.args.last();
|
||||
if (lastParam != null &&
|
||||
lastParam.type.tsym == syms.stringType.tsym &&
|
||||
lastParam.hasTag(Tag.PLUS)) {
|
||||
messages.error(tree, "crules.should.not.use.string.concatenation");
|
||||
}
|
||||
}
|
||||
|
||||
super.visitApply(tree);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
107
langtools/make/tools/crules/CodingRulesAnalyzerPlugin.java
Normal file
107
langtools/make/tools/crules/CodingRulesAnalyzerPlugin.java
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package crules;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.Plugin;
|
||||
import com.sun.source.util.TaskEvent;
|
||||
import com.sun.source.util.TaskEvent.Kind;
|
||||
import com.sun.source.util.TaskListener;
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.api.BasicJavacTask;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.Log;
|
||||
|
||||
public class CodingRulesAnalyzerPlugin implements Plugin {
|
||||
|
||||
protected Log log;
|
||||
protected Trees trees;
|
||||
|
||||
public void init(JavacTask task, String... args) {
|
||||
BasicJavacTask impl = (BasicJavacTask)task;
|
||||
Context context = impl.getContext();
|
||||
log = Log.instance(context);
|
||||
trees = Trees.instance(task);
|
||||
task.addTaskListener(new PostAnalyzeTaskListener(
|
||||
new MutableFieldsAnalyzer(task),
|
||||
new AssertCheckAnalyzer(task)
|
||||
));
|
||||
}
|
||||
|
||||
public class PostAnalyzeTaskListener implements TaskListener {
|
||||
private final Map<Kind, List<AbstractCodingRulesAnalyzer>> analyzers = new HashMap<>();
|
||||
|
||||
public PostAnalyzeTaskListener(AbstractCodingRulesAnalyzer... analyzers) {
|
||||
for (AbstractCodingRulesAnalyzer analyzer : analyzers) {
|
||||
List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(analyzer.eventKind);
|
||||
|
||||
if (currentAnalyzers == null) {
|
||||
this.analyzers.put(analyzer.eventKind, currentAnalyzers = new ArrayList<>());
|
||||
}
|
||||
|
||||
currentAnalyzers.add(analyzer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void started(TaskEvent taskEvent) {}
|
||||
|
||||
@Override
|
||||
public void finished(TaskEvent taskEvent) {
|
||||
List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(taskEvent.getKind());
|
||||
|
||||
if (currentAnalyzers != null) {
|
||||
TypeElement typeElem = taskEvent.getTypeElement();
|
||||
Tree tree = trees.getTree(typeElem);
|
||||
if (tree != null) {
|
||||
JavaFileObject prevSource = log.currentSourceFile();
|
||||
try {
|
||||
log.useSource(taskEvent.getCompilationUnit().getSourceFile());
|
||||
for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) {
|
||||
analyzer.treeVisitor.scan((JCTree)tree);
|
||||
}
|
||||
} finally {
|
||||
log.useSource(prevSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "coding_rules";
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -28,24 +28,25 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.TaskEvent.Kind;
|
||||
import com.sun.tools.javac.code.Kinds;
|
||||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
import com.sun.tools.javac.tree.TreeScanner;
|
||||
|
||||
import static com.sun.source.util.TaskEvent.Kind;
|
||||
import static com.sun.tools.javac.code.Flags.*;
|
||||
import static com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
import static com.sun.tools.javac.code.Flags.ENUM;
|
||||
import static com.sun.tools.javac.code.Flags.FINAL;
|
||||
import static com.sun.tools.javac.code.Flags.STATIC;
|
||||
import static com.sun.tools.javac.code.Flags.SYNTHETIC;
|
||||
|
||||
public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {
|
||||
|
||||
public MutableFieldsAnalyzer() {
|
||||
public MutableFieldsAnalyzer(JavacTask task) {
|
||||
super(task);
|
||||
treeVisitor = new MutableFieldsVisitor();
|
||||
eventKind = Kind.ANALYZE;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "mutable_fields_analyzer";
|
||||
}
|
||||
|
||||
private boolean ignoreField(String className, String field) {
|
||||
List<String> currentFieldsToIgnore =
|
||||
classFieldsToIgnoreMap.get(className);
|
||||
@ -89,7 +90,7 @@ public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {
|
||||
private static final String packageToCheck = "com.sun.tools.javac";
|
||||
|
||||
private static final Map<String, List<String>> classFieldsToIgnoreMap =
|
||||
new HashMap<String, List<String>>();
|
||||
new HashMap<>();
|
||||
|
||||
static {
|
||||
classFieldsToIgnoreMap.
|
||||
|
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
#
|
||||
# This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,3 +26,5 @@
|
||||
# 0: symbol
|
||||
crules.err.var.must.be.final=\
|
||||
Static variable {0} must be final
|
||||
crules.should.not.use.string.concatenation=\
|
||||
Should not use string concatenation.
|
||||
|
@ -66,7 +66,7 @@ public class Flags {
|
||||
flags &= ~flag.value;
|
||||
}
|
||||
}
|
||||
Assert.check(flags == 0, "Flags parameter contains unknown flags " + flags);
|
||||
Assert.check(flags == 0);
|
||||
return flagSet;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -67,22 +67,19 @@ public class TypeAnnotationPosition {
|
||||
private TypePathEntry(TypePathEntryKind tag) {
|
||||
Assert.check(tag == TypePathEntryKind.ARRAY ||
|
||||
tag == TypePathEntryKind.INNER_TYPE ||
|
||||
tag == TypePathEntryKind.WILDCARD,
|
||||
"Invalid TypePathEntryKind: " + tag);
|
||||
tag == TypePathEntryKind.WILDCARD);
|
||||
this.tag = tag;
|
||||
this.arg = 0;
|
||||
}
|
||||
|
||||
public TypePathEntry(TypePathEntryKind tag, int arg) {
|
||||
Assert.check(tag == TypePathEntryKind.TYPE_ARGUMENT,
|
||||
"Invalid TypePathEntryKind: " + tag);
|
||||
Assert.check(tag == TypePathEntryKind.TYPE_ARGUMENT);
|
||||
this.tag = tag;
|
||||
this.arg = arg;
|
||||
}
|
||||
|
||||
public static TypePathEntry fromBinary(int tag, int arg) {
|
||||
Assert.check(arg == 0 || tag == TypePathEntryKind.TYPE_ARGUMENT.tag,
|
||||
"Invalid TypePathEntry tag/arg: " + tag + "/" + arg);
|
||||
Assert.check(arg == 0 || tag == TypePathEntryKind.TYPE_ARGUMENT.tag);
|
||||
switch (tag) {
|
||||
case 0:
|
||||
return ARRAY;
|
||||
@ -351,7 +348,7 @@ public class TypeAnnotationPosition {
|
||||
Iterator<Integer> iter = list.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Integer fst = iter.next();
|
||||
Assert.check(iter.hasNext(), "Could not decode type path: " + list);
|
||||
Assert.check(iter.hasNext());
|
||||
Integer snd = iter.next();
|
||||
loc = loc.append(TypePathEntry.fromBinary(fst, snd));
|
||||
}
|
||||
|
@ -2765,7 +2765,7 @@ public class Check {
|
||||
}
|
||||
|
||||
public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
|
||||
Assert.checkNonNull(a.type, "annotation tree hasn't been attributed yet: " + a);
|
||||
Assert.checkNonNull(a.type);
|
||||
validateAnnotationTree(a);
|
||||
|
||||
if (a.hasTag(TYPE_ANNOTATION) &&
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -25,7 +25,6 @@
|
||||
|
||||
package com.sun.tools.javac.util;
|
||||
|
||||
|
||||
/**
|
||||
* Simple facility for unconditional assertions.
|
||||
* The methods in this class are described in terms of equivalent assert
|
||||
@ -87,7 +86,7 @@ public class Assert {
|
||||
}
|
||||
|
||||
/** Equivalent to
|
||||
* assert cond : value;
|
||||
* assert cond : msg;
|
||||
*/
|
||||
public static void check(boolean cond, String msg) {
|
||||
if (!cond)
|
||||
@ -103,7 +102,7 @@ public class Assert {
|
||||
}
|
||||
|
||||
/** Equivalent to
|
||||
* assert (o == null) : value;
|
||||
* assert (o == null) : msg;
|
||||
*/
|
||||
public static void checkNull(Object o, String msg) {
|
||||
if (o != null)
|
||||
@ -111,7 +110,7 @@ public class Assert {
|
||||
}
|
||||
|
||||
/** Equivalent to
|
||||
* assert (o != null) : value;
|
||||
* assert (o != null) : msg;
|
||||
*/
|
||||
public static <T> T checkNonNull(T t, String msg) {
|
||||
if (t == null)
|
||||
|
@ -183,7 +183,7 @@ public class Bits {
|
||||
*/
|
||||
public void incl(int x) {
|
||||
Assert.check(currentState != BitsState.UNKNOWN);
|
||||
Assert.check(x >= 0, "Value of x " + x);
|
||||
Assert.check(x >= 0);
|
||||
sizeTo((x >>> wordshift) + 1);
|
||||
bits[x >>> wordshift] = bits[x >>> wordshift] |
|
||||
(1 << (x & wordmask));
|
||||
|
111
langtools/test/tools/all/RunCodingRules.java
Normal file
111
langtools/test/tools/all/RunCodingRules.java
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8043643
|
||||
* @summary Run the langtools coding rules over the langtools source code.
|
||||
*/
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.DiagnosticListener;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import com.sun.tools.javac.util.Assert;
|
||||
|
||||
public class RunCodingRules {
|
||||
public static void main(String... args) throws Exception {
|
||||
new RunCodingRules().run();
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
File testSrc = new File(System.getProperty("test.src", "."));
|
||||
File targetDir = new File(System.getProperty("test.classes", "."));
|
||||
File sourceDir = null;
|
||||
File crulesDir = null;
|
||||
for (File d = testSrc; d != null; d = d.getParentFile()) {
|
||||
if (new File(d, "TEST.ROOT").exists()) {
|
||||
d = d.getParentFile();
|
||||
File f = new File(d, "src/share/classes");
|
||||
if (f.exists()) {
|
||||
sourceDir = f;
|
||||
f = new File(d, "make/tools");
|
||||
if (f.exists())
|
||||
crulesDir = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sourceDir == null || crulesDir == null) {
|
||||
System.err.println("Warning: sources not found, test skipped.");
|
||||
return ;
|
||||
}
|
||||
|
||||
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
|
||||
StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null);
|
||||
DiagnosticListener<JavaFileObject> noErrors = diagnostic -> {
|
||||
Assert.check(diagnostic.getKind() != Diagnostic.Kind.ERROR, diagnostic.toString());
|
||||
};
|
||||
|
||||
List<File> crulesFiles = Files.walk(crulesDir.toPath())
|
||||
.map(entry -> entry.toFile())
|
||||
.filter(entry -> entry.getName().endsWith(".java"))
|
||||
.filter(entry -> entry.getParentFile().getName().equals("crules"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
File crulesTarget = new File(targetDir, "crules");
|
||||
crulesTarget.mkdirs();
|
||||
List<String> crulesOptions = Arrays.asList("-d", crulesTarget.getAbsolutePath());
|
||||
javaCompiler.getTask(null, fm, noErrors, crulesOptions, null,
|
||||
fm.getJavaFileObjectsFromFiles(crulesFiles)).call();
|
||||
File registration = new File(crulesTarget, "META-INF/services/com.sun.source.util.Plugin");
|
||||
registration.getParentFile().mkdirs();
|
||||
try (Writer metaInfServices = new FileWriter(registration)) {
|
||||
metaInfServices.write("crules.CodingRulesAnalyzerPlugin\n");
|
||||
}
|
||||
|
||||
List<File> sources = Files.walk(sourceDir.toPath())
|
||||
.map(entry -> entry.toFile())
|
||||
.filter(entry -> entry.getName().endsWith(".java"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
File sourceTarget = new File(targetDir, "classes");
|
||||
sourceTarget.mkdirs();
|
||||
String processorPath = crulesTarget.getAbsolutePath() + File.pathSeparator +
|
||||
crulesDir.getAbsolutePath();
|
||||
List<String> options = Arrays.asList("-d", sourceTarget.getAbsolutePath(),
|
||||
"-processorpath", processorPath, "-Xplugin:coding_rules");
|
||||
javaCompiler.getTask(null, fm, noErrors, options, null,
|
||||
fm.getJavaFileObjectsFromFiles(sources)).call();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user