Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1423,33 +1423,6 @@
<failOnUnsupportedJava>false</failOnUnsupportedJava>
<excludes>
<exclude>jsr166e/**</exclude>
<!-- start excludes for valid system-out -->
<exclude>org/elasticsearch/common/logging/log4j/ConsoleAppender*</exclude>
<exclude>org/elasticsearch/common/http/client/HttpDownloadHelper*</exclude>
<exclude>org/elasticsearch/common/cli/Terminal*</exclude>
<exclude>org/elasticsearch/plugins/PluginManager$SysOut.class</exclude>
<exclude>org/elasticsearch/common/http/client/HttpDownloadHelper.class</exclude>
<exclude>org/elasticsearch/bootstrap/Bootstrap.class</exclude>
<exclude>org/elasticsearch/Version.class</exclude>
<exclude>org/elasticsearch/common/lucene/search/Queries$QueryWrapperFilterFactory.class</exclude>
<!-- end excludes for valid system-out -->
<!-- start excludes for Math.abs -->
<exclude>org/elasticsearch/common/util/MathUtils.class</exclude>
<exclude>org/elasticsearch/common/math/UnboxedMathUtils.class</exclude>
<exclude>org/elasticsearch/cluster/routing/OperationRouting.class</exclude>
<!-- end excludes for Math.abs -->
<!-- start exclude for FilteredQuery -->
<exclude>org/elasticsearch/common/lucene/search/XFilteredQuery.class</exclude>
<!-- end exclude for FilteredQuery -->
<!-- start exclude for Channels utility class -->
<exclude>org/elasticsearch/common/io/Channels.class</exclude>
<!-- end exclude for Channels -->
<!-- start exclude for Lucene utility class -->
<exclude>org/elasticsearch/common/lucene/Lucene$LenientParser.class</exclude>
<!-- end exclude for Lucene -->
<!-- start exclude for Future utility class -->
<exclude>org/elasticsearch/common/util/concurrent/FutureUtils.class</exclude>
<!-- end exclude for Future utility class -->
</excludes>
<bundledSignatures>
<!-- This will automatically choose the right signatures based on 'targetVersion': -->
Expand All @@ -1462,6 +1435,7 @@
<signaturesFile>dev-tools/forbidden/all-signatures.txt</signaturesFile>
</signaturesFiles>
<signatures>${forbidden.signatures}</signatures>
<suppressAnnotations><annotation>**.SuppressForbidden</annotation></suppressAnnotations>
</configuration>
<phase>compile</phase>
<goals>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -580,6 +581,7 @@ public String number() {
return sb.toString();
}

@SuppressForbidden(reason = "System.out.*")
public static void main(String[] args) {
System.out.println("Version: " + Version.CURRENT + ", Build: " + Build.CURRENT.hashShort() + "/" + Build.CURRENT.timestamp() + ", JVM: " + JvmInfo.jvmInfo().version());
}
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.Version;
import org.elasticsearch.common.PidFile;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.inject.CreationException;
import org.elasticsearch.common.inject.spi.Message;
Expand Down Expand Up @@ -90,6 +91,7 @@ public boolean handle(int code) {
}
}

@SuppressForbidden(reason = "Exception#printStackTrace()")
private static void setupLogging(Tuple<Settings, Environment> tuple) {
try {
tuple.v1().getClassLoader().loadClass("org.apache.log4j.Logger");
Expand All @@ -99,7 +101,7 @@ private static void setupLogging(Tuple<Settings, Environment> tuple) {
} catch (NoClassDefFoundError e) {
// no log4j
} catch (Exception e) {
System.err.println("Failed to configure logging...");
sysError("Failed to configure logging...", false);
e.printStackTrace();
}
}
Expand Down Expand Up @@ -154,8 +156,7 @@ public static void main(String[] args) {
PidFile.create(Paths.get(pidFile), true);
} catch (Exception e) {
String errorMessage = buildErrorMessage("pid", e);
System.err.println(errorMessage);
System.err.flush();
sysError(errorMessage, true);
System.exit(3);
}
}
Expand All @@ -171,8 +172,7 @@ public static void main(String[] args) {
setupLogging(tuple);
} catch (Exception e) {
String errorMessage = buildErrorMessage("Setup", e);
System.err.println(errorMessage);
System.err.flush();
sysError(errorMessage, true);
System.exit(3);
}

Expand All @@ -191,7 +191,7 @@ public static void main(String[] args) {
try {
if (!foreground) {
Loggers.disableConsoleLogging();
System.out.close();
closeSystOut();
}

// fail if using broken version
Expand All @@ -203,7 +203,7 @@ public static void main(String[] args) {
bootstrap.start();

if (!foreground) {
System.err.close();
closeSysError();
}

keepAliveLatch = new CountDownLatch(1);
Expand Down Expand Up @@ -234,8 +234,7 @@ public void run() {
}
String errorMessage = buildErrorMessage(stage, e);
if (foreground) {
System.err.println(errorMessage);
System.err.flush();
sysError(errorMessage, true);
Loggers.disableConsoleLogging();
}
logger.error("Exception", e);
Expand All @@ -244,6 +243,24 @@ public void run() {
}
}

@SuppressForbidden(reason = "System#out")
private static void closeSystOut() {
System.out.close();
}

@SuppressForbidden(reason = "System#err")
private static void closeSysError() {
System.err.close();
}

@SuppressForbidden(reason = "System#err")
private static void sysError(String line, boolean flush) {
System.err.println(line);
if (flush) {
System.err.flush();
}
}

private static String buildErrorMessage(String stage, Throwable e) {
StringBuilder errorMessage = new StringBuilder("{").append(Version.CURRENT).append("}: ");
errorMessage.append(stage).append(" Failed ...\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.math.MathUtils;
Expand Down Expand Up @@ -247,6 +248,7 @@ protected IndexShardRoutingTable shards(ClusterState clusterState, String index,
return indexShard;
}

@SuppressForbidden(reason = "Math#abs is trappy")
private int shardId(ClusterState clusterState, String index, String type, String id, @Nullable String routing) {
final IndexMetaData indexMetaData = indexMetaData(clusterState, index);
final Version createdVersion = indexMetaData.getCreationVersion();
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/elasticsearch/common/SuppressForbidden.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface SuppressForbidden {
String reason();
}
3 changes: 3 additions & 0 deletions src/main/java/org/elasticsearch/common/cli/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
package org.elasticsearch.common.cli;

import org.apache.commons.cli.CommandLine;
import org.elasticsearch.common.SuppressForbidden;

import java.io.*;
import java.util.Locale;

/**
*
*/
@SuppressForbidden(reason = "System#out")
public abstract class Terminal {

public static final String DEBUG_SYSTEM_PROPERTY = "es.cli.debug";
Expand Down Expand Up @@ -163,6 +165,7 @@ public void printStackTrace(Throwable t) {
}
}

@SuppressForbidden(reason = "System#out")
private static class SystemTerminal extends Terminal {

private final PrintWriter printWriter = new PrintWriter(System.out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.ElasticsearchTimeoutException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.unit.TimeValue;

import java.io.*;
Expand Down Expand Up @@ -134,6 +135,7 @@ public void endDownload() {
/**
* verbose progress system prints to some output stream
*/
@SuppressForbidden(reason = "System#out")
public static class VerboseProgress implements DownloadProgress {
private int dots = 0;
// CheckStyle:VisibilityModifier OFF - bc
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/common/io/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.common.io;

import org.elasticsearch.common.SuppressForbidden;
import org.jboss.netty.buffer.ChannelBuffer;

import java.io.EOFException;
Expand All @@ -28,6 +29,7 @@
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.WritableByteChannel;

@SuppressForbidden(reason = "Channel#read")
public final class Channels {

private Channels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.log4j.Layout;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.helpers.LogLog;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.logging.Loggers;

import java.io.IOException;
Expand Down Expand Up @@ -138,6 +139,7 @@ void targetWarn(String val) {
* Prepares the appender for use.
*/
@Override
@SuppressForbidden(reason = "System#out")
public void activateOptions() {
if (follow) {
if (target.equals(SYSTEM_ERR)) {
Expand Down Expand Up @@ -172,6 +174,7 @@ final void closeWriter() {
* An implementation of OutputStream that redirects to the
* current System.err.
*/
@SuppressForbidden(reason = "System#err")
private static class SystemErrStream extends OutputStream {
public SystemErrStream() {
}
Expand All @@ -194,6 +197,7 @@ public void write(final byte[] b) throws IOException {
}

@Override

public void write(final byte[] b, final int off, final int len)
throws IOException {
if (!Loggers.consoleLoggingEnabled()) {
Expand All @@ -215,6 +219,7 @@ public void write(final int b) throws IOException {
* An implementation of OutputStream that redirects to the
* current System.out.
*/
@SuppressForbidden(reason = "System#err")
private static class SystemOutStream extends OutputStream {
public SystemOutStream() {
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/common/lucene/Lucene.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.ESLogger;
Expand Down Expand Up @@ -659,6 +660,7 @@ public static Version parseVersionLenient(String toParse, Version defaultValue)
return LenientParser.parse(toParse, defaultValue);
}

@SuppressForbidden(reason = "Version#parseLeniently() used in a central place")
private static final class LenientParser {
public static Version parse(String toParse, Version defaultValue) {
if (Strings.hasLength(toParse)) {
Expand Down
21 changes: 6 additions & 15 deletions src/main/java/org/elasticsearch/common/lucene/search/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.lucene.search.*;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.search.child.CustomQueryWrappingFilter;

Expand Down Expand Up @@ -148,22 +149,12 @@ public static int calculateMinShouldMatch(int optionalClauseCount, String spec)
* If a filter has an anti per segment execution / caching nature then @{@link CustomQueryWrappingFilter} is returned
* otherwise the standard {@link org.apache.lucene.search.QueryWrapperFilter} is returned.
*/
@SuppressForbidden(reason = "QueryWrapperFilter cachability")
public static Filter wrap(Query query, QueryParseContext context) {
return FACTORY.wrap(query, context);
}

private static final QueryWrapperFilterFactory FACTORY = new QueryWrapperFilterFactory();
// NOTE: This is a separate class since we added QueryWrapperFilter as a forbidden API
// that way we can exclude only the inner class without excluding the entire Queries class
// and potentially miss a forbidden API usage!
private static final class QueryWrapperFilterFactory {

public Filter wrap(Query query, QueryParseContext context) {
if (context.requireCustomQueryWrappingFilter() || CustomQueryWrappingFilter.shouldUseCustomQueryWrappingFilter(query)) {
return new CustomQueryWrappingFilter(query);
} else {
return new QueryWrapperFilter(query);
}
if (context.requireCustomQueryWrappingFilter() || CustomQueryWrappingFilter.shouldUseCustomQueryWrappingFilter(query)) {
return new CustomQueryWrappingFilter(query);
} else {
return new QueryWrapperFilter(query);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.common.math;

import org.elasticsearch.common.SuppressForbidden;

import java.util.concurrent.ThreadLocalRandom;

/**
Expand Down Expand Up @@ -456,18 +458,22 @@ public static double randomLong(Long l) {
return ThreadLocalRandom.current().nextLong(l);
}

@SuppressForbidden(reason = "Math#abs is trappy")
public static int abs(Integer a) {
return Math.abs(a);
}

@SuppressForbidden(reason = "Math#abs is trappy")
public static long abs(Long a) {
return Math.abs(a);
}

@SuppressForbidden(reason = "Math#abs is trappy")
public static float abs(Float a) {
return Math.abs(a);
}

@SuppressForbidden(reason = "Math#abs is trappy")
public static double abs(Double a) {
return Math.abs(a);
}
Expand Down
Loading