List of usage examples for org.objectweb.asm Opcodes ASM5
int ASM5
To view the source code for org.objectweb.asm Opcodes ASM5.
Click Source Link
From source file:org.eclipse.xtext.common.types.access.binary.asm.JvmAnnotationReferenceBuilder.java
License:Open Source License
public JvmAnnotationReferenceBuilder(InternalEList<JvmAnnotationReference> target, String desc, Proxies proxies) {/* w ww .jav a 2s .c o m*/ super(Opcodes.ASM5); this.proxies = proxies; this.target = target; this.annotationType = BinarySignatures.createTypeSignature(desc); result = TypesFactory.eINSTANCE.createJvmAnnotationReference(); result.setAnnotation(proxies.createAnnotationProxy(annotationType)); values = (InternalEList<JvmAnnotationValue>) result.getExplicitValues(); }
From source file:org.eclipse.xtext.common.types.access.binary.asm.JvmAnnotationValueBuilder.java
License:Open Source License
protected JvmAnnotationValueBuilder(Proxies proxies) { super(Opcodes.ASM5); this.proxies = proxies; }
From source file:org.eclipse.xtext.common.types.access.binary.asm.JvmDeclaredTypeBuilder.java
License:Open Source License
protected JvmDeclaredTypeBuilder(BinaryClass binaryClass, ClassFileBytesAccess bytesAccess, ClassLoader classLoader, Map<String, JvmTypeParameter> typeParameters, Proxies proxies) { super(Opcodes.ASM5); this.proxies = proxies; this.binaryClass = binaryClass; this.bytesAccess = bytesAccess; this.classLoader = classLoader; this.typeParameters = typeParameters; }
From source file:org.eclipse.xtext.common.types.access.binary.asm.JvmExecutableBuilder.java
License:Open Source License
public JvmExecutableBuilder(final JvmDeclaredType declarator, final int initialOffset, Map<String, JvmTypeParameter> typeParameters, Proxies proxies, final int access, final String name, final String desc, final String signature, final String[] exceptions) { super(Opcodes.ASM5); this.proxies = proxies; this.declarator = declarator; final StringBuilder fqName; if (isConstructor(name)) { result = TypesFactory.eINSTANCE.createJvmConstructor(); String declaratorIdentifier = declarator.getIdentifier(); String simpleName = declarator.getSimpleName(); fqName = new StringBuilder(declaratorIdentifier.length() + 1 + simpleName.length() + desc.length()); fqName.append(declaratorIdentifier); fqName.append('.'); fqName.append(simpleName);/*from ww w . j a v a2s. com*/ result.setSimpleName(simpleName); this.offset = initialOffset; } else { JvmOperation operation = TypesFactory.eINSTANCE.createJvmOperation(); result = operation; String declaratorIdentifier = declarator.getIdentifier(); fqName = new StringBuilder(declaratorIdentifier.length() + 1 + name.length() + desc.length()); fqName.append(declaratorIdentifier); fqName.append('.'); fqName.append(name); result.setSimpleName(name); this.offset = 0; operation.setAbstract((access & ACC_ABSTRACT) != 0); operation.setFinal((access & ACC_FINAL) != 0); operation.setStatic((access & ACC_STATIC) != 0); operation.setSynchronized((access & ACC_SYNCHRONIZED) != 0); operation.setStrictFloatingPoint((access & ACC_STRICT) != 0); operation.setNative((access & ACC_NATIVE) != 0); if (declarator.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE && ((JvmGenericType) declarator).isInterface()) { if (!operation.isAbstract() && !operation.isStatic()) { operation.setDefault(true); } } } proxies.setVisibility(access, result); BinaryMethodSignature binarySignature = BinarySignatures .createMethodSignature(signature != null ? signature : desc); typeParameters = proxies.createTypeParameters(binarySignature, result, typeParameters); if (result.eClass() == TypesPackage.Literals.JVM_OPERATION) { returnType = desc; ((JvmOperation) result) .setReturnType(proxies.createTypeReference(binarySignature.getReturnType(), typeParameters)); } fqName.append('('); List<BinaryGenericTypeSignature> parameterTypes = binarySignature.getParameterTypes(); int myOffset = signature == null ? offset : 0; if (parameterTypes.size() > myOffset) { // char[][] argumentNames = method.getArgumentNames(); // boolean hasArgumentNames = argumentNames != null && argumentNames.length != 0; InternalEList<JvmFormalParameter> parameters = (InternalEList<JvmFormalParameter>) result .getParameters(); for (int i = myOffset; i < parameterTypes.size(); i++) { if (i != myOffset) fqName.append(','); BinaryTypeSignature parameterType = parameterTypes.get(i); fqName.append(parameterType.getTypeErasure().toIdentifier()); parameters.addUnique( createFormalParameter(parameterType, "arg" + (i - myOffset), result, typeParameters)); } } fqName.append(')'); result.internalSetIdentifier(fqName.toString()); proxies.setVisibility(access, result); result.setVarArgs((access & ACC_VARARGS) != 0); result.setDeprecated((access & ACC_DEPRECATED) != 0); List<BinaryGenericTypeSignature> exceptionSignatures = binarySignature.getExceptionTypes(); if (exceptionSignatures.size() == 0) { if (exceptions != null) { InternalEList<JvmTypeReference> resultExceptions = (InternalEList<JvmTypeReference>) result .getExceptions(); for (int i = 0, max = exceptions.length; i < max; i++) { resultExceptions.addUnique(proxies.createTypeReference( BinarySignatures.createObjectTypeSignature(exceptions[i]), typeParameters)); } } } else { InternalEList<JvmTypeReference> resultExceptions = (InternalEList<JvmTypeReference>) result .getExceptions(); for (int i = 0, max = exceptionSignatures.size(); i < max; i++) { resultExceptions.addUnique(proxies.createTypeReference(exceptionSignatures.get(i), typeParameters)); } } }
From source file:org.eclipse.xtext.common.types.access.binary.asm.JvmFieldBuilder.java
License:Open Source License
public JvmFieldBuilder(JvmDeclaredType declarator, Map<String, JvmTypeParameter> typeParameters, Proxies proxies, final int access, final String name, final String desc, final String signature, final Object value) { super(Opcodes.ASM5); this.proxies = proxies; this.declarator = declarator; if ((access & ACC_ENUM) != 0) { result = TypesFactory.eINSTANCE.createJvmEnumerationLiteral(); } else {/*from w w w . j a v a 2 s . c o m*/ result = TypesFactory.eINSTANCE.createJvmField(); if (value != null) { result.setConstant(true); if (desc.length() == 1) { switch (desc.charAt(0)) { case 'Z': result.setConstantValue(((Integer) value).intValue() != 0); break; case 'C': result.setConstantValue((char) ((Integer) value).intValue()); break; case 'B': result.setConstantValue(((Number) value).byteValue()); break; case 'S': result.setConstantValue(((Number) value).shortValue()); break; default: result.setConstantValue(value); } } else { result.setConstantValue(value); } } else { result.setConstant(false); } } result.internalSetIdentifier(declarator.getIdentifier() + "." + name); result.setSimpleName(name); result.setFinal((access & ACC_FINAL) != 0); result.setStatic((access & ACC_STATIC) != 0); result.setTransient((access & ACC_TRANSIENT) != 0); result.setVolatile((access & ACC_VOLATILE) != 0); result.setDeprecated((access & ACC_DEPRECATED) != 0); proxies.setVisibility(access, result); result.setType(proxies.createTypeReference( BinarySignatures.createTypeSignature(signature != null ? signature : desc), typeParameters)); }
From source file:org.elasticsearch.painless.MethodWriter.java
License:Apache License
public MethodWriter(int access, Method method, ClassVisitor cw, BitSet statements, CompilerSettings settings) { super(Opcodes.ASM5, cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null), access, method.getName(), method.getDescriptor()); this.statements = statements; this.settings = settings; }
From source file:org.elasticsearch.painless.PainlessScriptEngine.java
License:Apache License
/** * Generates a stateful factory class that will return script instances. Acts as a middle man between * the {@link ScriptContext#factoryClazz} and the {@link ScriptContext#instanceClazz} when used so that * the stateless factory can be used for caching and the stateful factory can act as a cache for new * script instances. Uses the newInstance method from a {@link ScriptContext#statefulFactoryClazz} to * define the factory method to create new instances of the {@link ScriptContext#instanceClazz}. * @param loader The {@link ClassLoader} that is used to define the factory class and script class. * @param context The {@link ScriptContext}'s semantics are used to define the factory class. * @param <T> The factory class.//from w w w. ja v a2 s . c o m * @return A factory class that will return script instances. */ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context, MainMethodReserved reserved) { int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS; int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL; String interfaceBase = Type.getType(context.statefulFactoryClazz).getInternalName(); String className = interfaceBase + "$StatefulFactory"; String classInterfaces[] = new String[] { interfaceBase }; ClassWriter writer = new ClassWriter(classFrames); writer.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, OBJECT_TYPE.getInternalName(), classInterfaces); Method newFactory = null; for (Method method : context.factoryClazz.getMethods()) { if ("newFactory".equals(method.getName())) { newFactory = method; break; } } for (int count = 0; count < newFactory.getParameterTypes().length; ++count) { writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "$arg" + count, Type.getType(newFactory.getParameterTypes()[count]).getDescriptor(), null, null).visitEnd(); } org.objectweb.asm.commons.Method base = new org.objectweb.asm.commons.Method("<init>", MethodType.methodType(void.class).toMethodDescriptorString()); org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method("<init>", MethodType.methodType(void.class, newFactory.getParameterTypes()).toMethodDescriptorString()); GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ASM5, init, writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null)); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(OBJECT_TYPE, base); for (int count = 0; count < newFactory.getParameterTypes().length; ++count) { constructor.loadThis(); constructor.loadArg(count); constructor.putField(Type.getType(className), "$arg" + count, Type.getType(newFactory.getParameterTypes()[count])); } constructor.returnValue(); constructor.endMethod(); Method newInstance = null; for (Method method : context.statefulFactoryClazz.getMethods()) { if ("newInstance".equals(method.getName())) { newInstance = method; break; } } org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(newInstance.getName(), MethodType.methodType(newInstance.getReturnType(), newInstance.getParameterTypes()) .toMethodDescriptorString()); List<Class<?>> parameters = new ArrayList<>(Arrays.asList(newFactory.getParameterTypes())); parameters.addAll(Arrays.asList(newInstance.getParameterTypes())); org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method("<init>", MethodType .methodType(void.class, parameters.toArray(new Class<?>[] {})).toMethodDescriptorString()); GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, instance, writer.visitMethod( Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, instance.getName(), instance.getDescriptor(), null, null)); adapter.visitCode(); adapter.newInstance(WriterConstants.CLASS_TYPE); adapter.dup(); for (int count = 0; count < newFactory.getParameterTypes().length; ++count) { adapter.loadThis(); adapter.getField(Type.getType(className), "$arg" + count, Type.getType(newFactory.getParameterTypes()[count])); } adapter.loadArgs(); adapter.invokeConstructor(WriterConstants.CLASS_TYPE, constru); adapter.returnValue(); adapter.endMethod(); writeNeedsMethods(context.statefulFactoryClazz, writer, reserved); writer.visitEnd(); loader.defineFactory(className.replace('/', '.'), writer.toByteArray()); return Type.getType(className); }
From source file:org.elasticsearch.painless.PainlessScriptEngine.java
License:Apache License
/** * Generates a factory class that will return script instances or stateful factories. * Uses the newInstance method from a {@link ScriptContext#factoryClazz} to define the factory method * to create new instances of the {@link ScriptContext#instanceClazz} or uses the newFactory method * to create new factories of the {@link ScriptContext#statefulFactoryClazz}. * @param loader The {@link ClassLoader} that is used to define the factory class and script class. * @param context The {@link ScriptContext}'s semantics are used to define the factory class. * @param classType The type to be instaniated in the newFactory or newInstance method. Depends * on whether a {@link ScriptContext#statefulFactoryClazz} is specified. * @param <T> The factory class.// w w w .j a v a 2s .com * @return A factory class that will return script instances. */ private <T> T generateFactory(Loader loader, ScriptContext<T> context, MainMethodReserved reserved, Type classType) { int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS; int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL; String interfaceBase = Type.getType(context.factoryClazz).getInternalName(); String className = interfaceBase + "$Factory"; String classInterfaces[] = new String[] { interfaceBase }; ClassWriter writer = new ClassWriter(classFrames); writer.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, OBJECT_TYPE.getInternalName(), classInterfaces); org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method("<init>", MethodType.methodType(void.class).toMethodDescriptorString()); GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ASM5, init, writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null)); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(OBJECT_TYPE, init); constructor.returnValue(); constructor.endMethod(); Method reflect = null; for (Method method : context.factoryClazz.getMethods()) { if ("newInstance".equals(method.getName())) { reflect = method; break; } else if ("newFactory".equals(method.getName())) { reflect = method; break; } } org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(reflect.getName(), MethodType.methodType(reflect.getReturnType(), reflect.getParameterTypes()) .toMethodDescriptorString()); org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method("<init>", MethodType.methodType(void.class, reflect.getParameterTypes()).toMethodDescriptorString()); GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, instance, writer.visitMethod( Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, instance.getName(), instance.getDescriptor(), null, null)); adapter.visitCode(); adapter.newInstance(classType); adapter.dup(); adapter.loadArgs(); adapter.invokeConstructor(classType, constru); adapter.returnValue(); adapter.endMethod(); writeNeedsMethods(context.factoryClazz, writer, reserved); writer.visitEnd(); Class<?> factory = loader.defineFactory(className.replace('/', '.'), writer.toByteArray()); try { return context.factoryClazz.cast(factory.getConstructor().newInstance()); } catch (Exception exception) { // Catch everything to let the user know this is something caused internally. throw new IllegalStateException( "An internal error occurred attempting to define the factory class [" + className + "].", exception); } }
From source file:org.elasticsearch.painless.PainlessScriptEngine.java
License:Apache License
private void writeNeedsMethods(Class<?> clazz, ClassWriter writer, MainMethodReserved reserved) { for (Method method : clazz.getMethods()) { if (method.getName().startsWith("needs") && method.getReturnType().equals(boolean.class) && method.getParameterTypes().length == 0) { String name = method.getName(); name = name.substring(5);//from ww w . j a v a2s . c o m name = Character.toLowerCase(name.charAt(0)) + name.substring(1); org.objectweb.asm.commons.Method needs = new org.objectweb.asm.commons.Method(method.getName(), MethodType.methodType(boolean.class).toMethodDescriptorString()); GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, needs, writer.visitMethod(Opcodes.ACC_PUBLIC, needs.getName(), needs.getDescriptor(), null, null)); adapter.visitCode(); adapter.push(reserved.getUsedVariables().contains(name)); adapter.returnValue(); adapter.endMethod(); } } }
From source file:org.evosuite.graphs.cfg.CFGClassAdapter.java
License:Open Source License
/** * Constructor/* w w w. j a v a 2s. c o m*/ * * @param visitor * a {@link org.objectweb.asm.ClassVisitor} object. * @param className * a {@link java.lang.String} object. */ public CFGClassAdapter(ClassLoader classLoader, ClassVisitor visitor, String className) { super(Opcodes.ASM5, visitor); this.className = className; this.classLoader = classLoader; }