Skip to content

Commit

Permalink
Fixed issue #140
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed May 17, 2021
1 parent 2846202 commit 1d0e426
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2010-2021 Osman Shoukry
*
* Licensed 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 com.openpojo.validation.utils;


import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
* this class dumps a class that looks like this:
*
* public class AClassWithSytheticStringField {
* private static "synthetic" String syntheticStaticString;
* }
*/
public class AClassWithSyntheticStaticFieldDumper implements Opcodes {
public static byte[] dump(String className) {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;

cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);

{
fv = cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_SYNTHETIC, "syntheticStaticString", "Ljava/lang/String;", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();

return cw.toByteArray();
}
}

0 comments on commit 1d0e426

Please sign in to comment.