use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.
the class BytecodeAnalysisIndex method collectKeys.
@NotNull
private static Map<Bytes, Void> collectKeys(byte[] content) throws NoSuchAlgorithmException {
HashMap<Bytes, Void> map = new HashMap<>();
MessageDigest md = BytecodeAnalysisConverter.getMessageDigest();
new ClassReader(content).accept(new KeyedMethodVisitor() {
@Nullable
@Override
MethodVisitor visitMethod(MethodNode node, Key key) {
map.put(ClassDataIndexer.compressKey(md, key), null);
return null;
}
}, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return map;
}
use of org.jetbrains.org.objectweb.asm.MethodVisitor in project android by JetBrains.
the class ResourceClassGenerator method generateIntArrayFromCache.
private static void generateIntArrayFromCache(@NotNull ClassWriter cw, String className, Map<String, List<Integer>> styleableCache) {
// Generate the field declarations.
for (String name : styleableCache.keySet()) {
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, name, "[I", null, null);
}
// Generate class initializer block to initialize the arrays declared above.
MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
for (Map.Entry<String, List<Integer>> entry : styleableCache.entrySet()) {
List<Integer> values = entry.getValue();
if (!values.isEmpty()) {
generateArrayInitialization(mv, className, entry.getKey(), values);
}
}
mv.visitInsn(RETURN);
mv.visitMaxs(4, 0);
mv.visitEnd();
}
use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.
the class StringPropertyCodeGenerator method generateLoadTextMethod.
private void generateLoadTextMethod(final AsmCodeGenerator.FormClassVisitor visitor, final String methodName, final String componentClass, final String setMnemonicMethodName) {
MethodVisitor mv = visitor.visitNewMethod(ACC_PRIVATE | ACC_SYNTHETIC, methodName, "(L" + componentClass + ";Ljava/lang/String;)V", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 3);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 4);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 5);
mv.visitInsn(ICONST_M1);
mv.visitVarInsn(ISTORE, 6);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 7);
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ILOAD, 7);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
Label l1 = new Label();
mv.visitJumpInsn(IF_ICMPGE, l1);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ILOAD, 7);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
mv.visitIntInsn(BIPUSH, 38);
Label l2 = new Label();
mv.visitJumpInsn(IF_ICMPNE, l2);
mv.visitIincInsn(7, 1);
mv.visitVarInsn(ILOAD, 7);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
Label l3 = new Label();
mv.visitJumpInsn(IF_ICMPNE, l3);
mv.visitJumpInsn(GOTO, l1);
mv.visitLabel(l3);
mv.visitVarInsn(ILOAD, 4);
mv.visitJumpInsn(IFNE, l2);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ILOAD, 7);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
mv.visitIntInsn(BIPUSH, 38);
mv.visitJumpInsn(IF_ICMPEQ, l2);
mv.visitInsn(ICONST_1);
mv.visitVarInsn(ISTORE, 4);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ILOAD, 7);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
mv.visitVarInsn(ISTORE, 5);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "length", "()I", false);
mv.visitVarInsn(ISTORE, 6);
mv.visitLabel(l2);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ILOAD, 7);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(C)Ljava/lang/StringBuffer;", false);
mv.visitInsn(POP);
mv.visitIincInsn(7, 1);
mv.visitJumpInsn(GOTO, l0);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, "setText", "(Ljava/lang/String;)V", false);
mv.visitVarInsn(ILOAD, 4);
Label l4 = new Label();
mv.visitJumpInsn(IFEQ, l4);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ILOAD, 5);
mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, setMnemonicMethodName, "(C)V", false);
if (myHaveSetDisplayedMnemonicIndex) {
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ILOAD, 6);
mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, "setDisplayedMnemonicIndex", "(I)V", false);
}
mv.visitLabel(l4);
mv.visitInsn(RETURN);
mv.visitMaxs(3, 8);
mv.visitEnd();
}
use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.
the class LocalVariablesUtil method collectVariablesFromBytecode.
@NotNull
private static List<DecompiledLocalVariable> collectVariablesFromBytecode(VirtualMachineProxyImpl vm, Location location, MultiMap<Integer, String> namesMap) {
if (!vm.canGetBytecodes()) {
return Collections.emptyList();
}
try {
LOG.assertTrue(location != null);
final com.sun.jdi.Method method = location.method();
final Location methodLocation = method.location();
if (methodLocation == null || methodLocation.codeIndex() < 0) {
// native or abstract method
return Collections.emptyList();
}
long codeIndex = location.codeIndex();
if (codeIndex > 0) {
final byte[] bytecodes = method.bytecodes();
if (bytecodes != null && bytecodes.length > 0) {
final int firstLocalVariableSlot = getFirstLocalsSlot(method);
final HashMap<Integer, DecompiledLocalVariable> usedVars = new HashMap<>();
MethodBytecodeUtil.visit(method, codeIndex, new MethodVisitor(Opcodes.API_VERSION) {
@Override
public void visitVarInsn(int opcode, int slot) {
if (slot >= firstLocalVariableSlot) {
DecompiledLocalVariable variable = usedVars.get(slot);
String typeSignature = MethodBytecodeUtil.getVarInstructionType(opcode).getDescriptor();
if (variable == null || !typeSignature.equals(variable.getSignature())) {
variable = new DecompiledLocalVariable(slot, false, typeSignature, namesMap.get(slot));
usedVars.put(slot, variable);
}
}
}
}, false);
if (usedVars.isEmpty()) {
return Collections.emptyList();
}
List<DecompiledLocalVariable> vars = new ArrayList<>(usedVars.values());
vars.sort(Comparator.comparingInt(DecompiledLocalVariable::getSlot));
return vars;
}
}
} catch (UnsupportedOperationException ignored) {
} catch (Exception e) {
LOG.error(e);
}
return Collections.emptyList();
}
use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.
the class PreviewNestedFormLoader method generateStubClass.
private void generateStubClass(final LwRootContainer rootContainer, final String generatedClassName) throws IOException, CodeGenerationException {
@NonNls ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, generatedClassName, null, "java/lang/Object", ArrayUtil.EMPTY_STRING_ARRAY);
cw.visitField(Opcodes.ACC_PUBLIC, PreviewFormAction.PREVIEW_BINDING_FIELD, "Ljavax/swing/JComponent;", null, null);
@NonNls MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
ByteArrayInputStream bais = new ByteArrayInputStream(cw.toByteArray());
AsmCodeGenerator acg = new AsmCodeGenerator(rootContainer, myFinder, this, true, new PsiClassWriter(myModule));
byte[] data = acg.patchClass(bais);
FormErrorInfo[] errors = acg.getErrors();
if (errors.length > 0) {
throw new CodeGenerationException(errors[0].getComponentId(), errors[0].getErrorMessage());
}
FileUtil.writeToFile(new File(myTempPath, generatedClassName + ".class"), data);
}
Aggregations