Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To get rid of illegal reflective access.
- Loading branch information
Showing
7 changed files
with
413 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
sslr-core/src/main/java/com/sonar/sslr/impl/typed/Interceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* SonarSource Language Recognizer | ||
* Copyright (C) 2010-2019 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.sslr.impl.typed; | ||
|
||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
public class Interceptor { | ||
|
||
private Interceptor() { | ||
} | ||
|
||
public static Object create( | ||
Class<?> superClass, | ||
Class<?>[] constructorParameterTypes, | ||
Object[] constructorArguments, | ||
MethodInterceptor interceptor | ||
) { | ||
ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); | ||
final String className = "GeneratedBySSLR"; | ||
final String superClassName = Type.getInternalName(superClass); | ||
cv.visit( | ||
Opcodes.V1_8, | ||
Opcodes.ACC_PUBLIC, | ||
className, | ||
null, | ||
superClassName, | ||
null); | ||
|
||
cv.visitField( | ||
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, | ||
"methodInterceptor", | ||
Type.getDescriptor(MethodInterceptor.class), | ||
null, | ||
null); | ||
|
||
cv.visitField( | ||
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, | ||
"methods", | ||
Type.getDescriptor(Method[].class), | ||
null, | ||
null); | ||
|
||
String constructorDescriptor = Type.getMethodDescriptor( | ||
Type.getType(void.class), | ||
Arrays.stream(constructorParameterTypes) | ||
.map(Type::getType) | ||
.toArray(Type[]::new)); | ||
MethodVisitor mv = cv.visitMethod( | ||
Opcodes.ACC_PUBLIC, | ||
"<init>", | ||
constructorDescriptor, | ||
null, | ||
null); | ||
mv.visitVarInsn(Opcodes.ALOAD, 0); | ||
for (int i = 0; i < constructorParameterTypes.length; i++) { | ||
mv.visitVarInsn(Type.getType(constructorParameterTypes[i]).getOpcode(Opcodes.ILOAD), 1 + i); | ||
} | ||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, | ||
superClassName, | ||
"<init>", | ||
constructorDescriptor, | ||
false); | ||
mv.visitInsn(Opcodes.RETURN); | ||
mv.visitMaxs(0, 0); | ||
mv.visitEnd(); | ||
|
||
Method[] methods = superClass.getMethods(); | ||
for (int methodId = 0; methodId < methods.length; methodId++) { | ||
Method method = methods[methodId]; | ||
if (Object.class.equals(method.getDeclaringClass())) { | ||
continue; | ||
} | ||
if (method.getReturnType().isPrimitive()) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
mv = cv.visitMethod( | ||
Opcodes.ACC_PUBLIC, | ||
method.getName(), | ||
Type.getMethodDescriptor(method), | ||
null, | ||
null); | ||
|
||
mv.visitFieldInsn(Opcodes.GETSTATIC, | ||
className, | ||
"methodInterceptor", | ||
Type.getDescriptor(MethodInterceptor.class)); | ||
|
||
mv.visitFieldInsn(Opcodes.GETSTATIC, | ||
className, | ||
"methods", | ||
Type.getDescriptor(Method[].class)); | ||
mv.visitLdcInsn(methodId); | ||
mv.visitInsn(Opcodes.AALOAD); | ||
|
||
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, | ||
Type.getInternalName(MethodInterceptor.class), | ||
"intercept", | ||
Type.getMethodDescriptor( | ||
Type.getType(boolean.class), | ||
Type.getType(Method.class)), | ||
true); | ||
Label label = new Label(); | ||
mv.visitJumpInsn(Opcodes.IFEQ, label); | ||
mv.visitInsn(Opcodes.ACONST_NULL); | ||
mv.visitInsn(Opcodes.ARETURN); | ||
mv.visitLabel(label); | ||
mv.visitVarInsn(Opcodes.ALOAD, 0); | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
for (int i = 0; i < method.getParameterCount(); i++) { | ||
mv.visitVarInsn(Type.getType(parameterTypes[i]).getOpcode(Opcodes.ILOAD), 1 + i); | ||
} | ||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, | ||
superClassName, | ||
method.getName(), | ||
Type.getMethodDescriptor(method), | ||
false); | ||
mv.visitInsn(Opcodes.ARETURN); | ||
mv.visitMaxs(0, 0); | ||
mv.visitEnd(); | ||
} | ||
|
||
byte[] classBytes = cv.toByteArray(); | ||
|
||
Class<?> cls = new ClassLoader() { | ||
public Class<?> defineClass() { | ||
return defineClass(className, classBytes, 0, classBytes.length); | ||
} | ||
}.defineClass(); | ||
Object instance; | ||
try { | ||
instance = cls | ||
.getConstructor(constructorParameterTypes) | ||
.newInstance(constructorArguments); | ||
cls.getField("methods") | ||
.set(instance, methods); | ||
cls.getField("methodInterceptor") | ||
.set(instance, interceptor); | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return instance; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
sslr-core/src/main/java/com/sonar/sslr/impl/typed/MethodInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* SonarSource Language Recognizer | ||
* Copyright (C) 2010-2019 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.sslr.impl.typed; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public interface MethodInterceptor { | ||
|
||
/** | ||
* @return {@code true} if original method should not be invoked | ||
*/ | ||
boolean intercept(Method method); | ||
|
||
} |
Oops, something went wrong.