|
Java example source code file (CallerSensitiveFinder.java)
The CallerSensitiveFinder.java Java example source code
/*
* Copyright (c) 2013, 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.
*/
import com.sun.tools.classfile.*;
import com.sun.tools.jdeps.ClassFileReader;
import static com.sun.tools.classfile.ConstantPool.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
/*
* @test
* @bug 8010117
* @summary Verify if CallerSensitive methods are annotated with
* sun.reflect.CallerSensitive annotation
* @build CallerSensitiveFinder
* @run main/othervm/timeout=900 -mx600m CallerSensitiveFinder
*/
public class CallerSensitiveFinder {
private static int numThreads = 3;
private static boolean verbose = false;
public static void main(String[] args) throws Exception {
List<Path> classes = new ArrayList<>();
String testclasses = System.getProperty("test.classes", ".");
int i = 0;
while (i < args.length) {
String arg = args[i++];
if (arg.equals("-v")) {
verbose = true;
} else {
Path p = Paths.get(testclasses, arg);
if (!p.toFile().exists()) {
throw new IllegalArgumentException(arg + " does not exist");
}
classes.add(p);
}
}
if (classes.isEmpty()) {
classes.addAll(PlatformClassPath.getJREClasses());
}
CallerSensitiveFinder csfinder = new CallerSensitiveFinder();
List<String> errors = csfinder.run(classes);
if (!errors.isEmpty()) {
throw new RuntimeException(errors.size() +
" caller-sensitive methods are missing @CallerSensitive annotation");
}
}
private final List<String> csMethodsMissingAnnotation = new ArrayList<>();
private final ReferenceFinder finder;
public CallerSensitiveFinder() {
this.finder = new ReferenceFinder(getFilter(), getVisitor());
}
private ReferenceFinder.Filter getFilter() {
final String classname = "sun/reflect/Reflection";
final String method = "getCallerClass";
return new ReferenceFinder.Filter() {
public boolean accept(ConstantPool cpool, CPRefInfo cpref) {
try {
CONSTANT_NameAndType_info nat = cpref.getNameAndTypeInfo();
return cpref.getClassName().equals(classname) && nat.getName().equals(method);
} catch (ConstantPoolException ex) {
throw new RuntimeException(ex);
}
}
};
}
private ReferenceFinder.Visitor getVisitor() {
return new ReferenceFinder.Visitor() {
public void visit(ClassFile cf, Method m, List<CPRefInfo> refs) {
try {
String name = String.format("%s#%s %s", cf.getName(),
m.getName(cf.constant_pool),
m.descriptor.getValue(cf.constant_pool));
if (!CallerSensitiveFinder.isCallerSensitive(m, cf.constant_pool)) {
csMethodsMissingAnnotation.add(name);
System.err.println("Missing @CallerSensitive: " + name);
} else {
if (verbose) {
System.out.format("@CS %s%n", name);
}
}
} catch (ConstantPoolException ex) {
throw new RuntimeException(ex);
}
}
};
}
public List<String> run(List
Other Java examples (source code examples)Here is a short list of links related to this Java CallerSensitiveFinder.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.