Skip to content

Instantly share code, notes, and snippets.

@asdf913
Created May 7, 2026 12:59
Show Gist options
  • Select an option

  • Save asdf913/29c294efe849dd1fd6505723f741d41b to your computer and use it in GitHub Desktop.

Select an option

Save asdf913/29c294efe849dd1fd6505723f741d41b to your computer and use it in GitHub Desktop.
Retrieve the allowed minimum and maximum port number defined in "checkPort()" in "java.net.InetSocketAddress" class
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.Arrays;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.FieldOrMethod;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.IFLT;
import org.apache.bcel.generic.IF_ICMPLE;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.LDC;
import org.apache.bcel.generic.MethodGen;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.apache.commons.lang3.reflect.FieldUtils;
import io.github.toolfactory.narcissus.Narcissus;
public class InetSocketAddressCheckPort {
public static void main(final String[] args) throws NoSuchMethodException, IOException {
//
System.out.println(Arrays.toString(getPorts()));
//
}
private static int[] getPorts() throws IOException, NoSuchMethodException {
//
int[] ints = null;
//
final Class<?> clz = InetSocketAddress.class;
//
try (final InputStream is = InetSocketAddressCheckPort.class
.getResourceAsStream(StringUtils.join("/", replace(Strings.CS, getName(clz), ".", "/"), ".class"))) {
//
final JavaClass javaClass = new ClassParser(is, null).parse();
//
final org.apache.bcel.classfile.Method method = getMethod(javaClass,
getDeclaredMethod(clz, "checkPort", Integer.TYPE));
//
final ConstantPoolGen cpg = new ConstantPoolGen(getConstantPool(javaClass));
//
final Instruction[] ins = getInstructions(getInstructionList(new MethodGen(method, getName(method), cpg)));
//
Instruction in = null;
//
Integer integer = null;
//
for (int i = 0; i < length(ins); i++) {
//
if ((in = ArrayUtils.get(ins, i)) instanceof IFLT) {
//
ints = ArrayUtils.add(ints, 0);
//
} else if (in instanceof IF_ICMPLE && i > 0 && (integer = cast(Integer.class,
getValue(cast(LDC.class, ArrayUtils.get(ins, i - 1)), cpg))) != null) {
//
ints = ArrayUtils.add(ints, integer.intValue());
//
} // if
//
} // for
//
} // try
//
return ints;
//
}
private static Object getValue(final LDC instance, final ConstantPoolGen cpg) {
return instance != null && cpg != null && cpg.getSize() > 0 ? instance.getValue(cpg) : null;
}
private static Instruction[] getInstructions(final InstructionList instance) {
return instance != null ? instance.getInstructions() : null;
}
private static InstructionList getInstructionList(final MethodGen instance) {
return instance != null ? instance.getInstructionList() : null;
}
private static String getName(final FieldOrMethod instance) {
//
try {
//
if (instance == null || FieldUtils.readField(instance, "constant_pool", true) == null) {
//
return null;
//
} // if
//
} catch (final IllegalAccessException e) {
//
return null;
//
} // try
//
return instance.getName();
//
}
private static ConstantPool getConstantPool(final JavaClass instance) {
return instance != null ? instance.getConstantPool() : null;
}
private static org.apache.bcel.classfile.Method getMethod(final JavaClass instance,
final java.lang.reflect.Method method) {
//
try {
//
if (instance == null || FieldUtils.readDeclaredField(instance, "methods", true) == null) {
//
return null;
//
} // if
//
} catch (final IllegalAccessException e) {
//
return null;
//
} // try
//
return instance.getMethod(method);
//
}
private static java.lang.reflect.Method getDeclaredMethod(final Class<?> instance, final String name,
final Class<?>... parameterTypes) throws NoSuchMethodException {
//
try {
//
if (instance == null || name == null
|| Narcissus.getField(name, getDeclaredField(getClass(name), "value")) == null) {
//
return null;
//
} // if
//
} catch (final NoSuchFieldException e) {
//
return null;
//
} // try
//
return instance.getDeclaredMethod(name, parameterTypes);
//
}
private static <T> T cast(final Class<T> clz, final Object instance) {
return clz != null && clz.isInstance(instance) ? clz.cast(instance) : null;
}
private static String replace(final Strings instance, final String text, final String searchString,
final String replacement) {
//
try {
//
if (instance == null || text == null
|| Narcissus.getField(text, getDeclaredField(getClass(text), "value")) == null) {
//
return null;
//
} // if
//
} catch (final NoSuchFieldException e) {
//
return null;
//
} // try
//
return instance.replace(text, searchString, replacement);
//
}
private static Field getDeclaredField(final Class<?> instance, final String name) throws NoSuchFieldException {
//
try {
//
final Class<?> clz = getClass(name);
//
if (instance == null || name == null
|| (clz != null && Narcissus.getField(name, clz.getDeclaredField("value")) == null)) {
//
return null;
//
} // if
//
} catch (final NoSuchFieldException e) {
//
return null;
//
} // try
//
return instance.getDeclaredField(name);
//
}
private static Class<?> getClass(final Object instance) {
return instance != null ? instance.getClass() : null;
}
private static String getName(final Class<?> instance) {
return instance != null ? instance.getName() : null;
}
private static int length(final Object[] instance) {
return instance != null ? instance.length : 0;
}
}
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.bcel</groupId>
<artifactId>bcel</artifactId>
<version>6.12.0</version>
</dependency>
<dependency>
<groupId>io.github.toolfactory</groupId>
<artifactId>narcissus</artifactId>
<version>1.0.11</version>
</dependency>
</dependencies>
@asdf913
Copy link
Copy Markdown
Author

asdf913 commented May 7, 2026

@asdf913
Copy link
Copy Markdown
Author

asdf913 commented May 7, 2026

Output

[0, 65535]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment