void createJSMethods(ClassHolder cls) { for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { MethodReference methodRef = method.getReference(); if (method.getAnnotations().get(JSBody.class.getName()) == null) { continue; } requireJSBody(diagnostics, method); if (!repository.methodMap.containsKey(method.getReference())) { continue; } MethodReference proxyRef = repository.methodMap.get(methodRef); MethodHolder proxyMethod = new MethodHolder(proxyRef.getDescriptor()); proxyMethod.getModifiers().add(ElementModifier.NATIVE); proxyMethod.getModifiers().add(ElementModifier.STATIC); boolean inline = repository.inlineMethods.contains(methodRef); AnnotationHolder generatorAnnot = new AnnotationHolder(inline ? DynamicInjector.class.getName() : DynamicGenerator.class.getName()); proxyMethod.getAnnotations().add(generatorAnnot); cls.addMethod(proxyMethod); Set<MethodReference> callbacks = repository.callbackMethods.get(proxyRef); if (callbacks != null) { for (MethodReference callback : callbacks) { generateCallbackCaller(cls, callback); } } } }
private ClassHolder readClass(InputStream stream, String name) throws IOException { DataInput input = new DataInputStream(stream); ClassHolder cls = new ClassHolder(name); cls.setLevel(accessLevels[input.readByte()]); cls.getModifiers().addAll(unpackModifiers(input.readInt())); int parentIndex = input.readInt(); cls.setParent(parentIndex >= 0 ? symbolTable.at(parentIndex) : null); int ownerIndex = input.readInt(); cls.setOwnerName(ownerIndex >= 0 ? symbolTable.at(ownerIndex) : null); int ifaceCount = input.readByte(); for (int i = 0; i < ifaceCount; ++i) { cls.getInterfaces().add(symbolTable.at(input.readInt())); } readAnnotations(input, cls.getAnnotations()); int fieldCount = input.readShort(); for (int i = 0; i < fieldCount; ++i) { cls.addField(readField(input)); } int methodCount = input.readShort(); for (int i = 0; i < methodCount; ++i) { cls.addMethod(readMethod(stream)); } return cls; }
private void substitute(ClassHolder cls, ClassHierarchy hierarchy) { ClassReader subst = hierarchy.getClassSource().get(TeaVMLoggerFactorySubstitution.class.getName()); for (FieldHolder field : cls.getFields().toArray(new FieldHolder[0])) { cls.removeField(field); } for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { cls.removeMethod(method); } for (FieldReader field : subst.getFields()) { cls.addField(ModelUtils.copyField(field)); } for (MethodReader method : subst.getMethods()) { cls.addMethod(ModelUtils.copyMethod(method)); } } }
for (MethodNode methodNode : node.methods) { MethodHolder method = parseMethod(methodNode, fullFileName); cls.addMethod(method); method.updateReference(referenceCache);
private void createAnnotationClass(DependencyAgent agent, String className) { String readerClassName = className + "$$__annotations__$$"; if (agent.getClassSource().get(readerClassName) != null) { return; } ClassHolder cls = new ClassHolder(className + "$$__annotations__$$"); cls.setLevel(AccessLevel.PUBLIC); cls.setOwnerName("java.lang.Object"); cls.getInterfaces().add(PlatformAnnotationProvider.class.getName()); MethodHolder ctor = new MethodHolder("<init>", ValueType.VOID); ctor.setLevel(AccessLevel.PUBLIC); ProgramEmitter pe = ProgramEmitter.create(ctor, agent.getClassHierarchy()); ValueEmitter thisVar = pe.var(0, cls); thisVar.invokeSpecial(Object.class, "<init>").exit(); ClassReader annotatedClass = agent.getClassSource().get(className); cls.addMethod(ctor); cls.addMethod(addReader(agent, annotatedClass)); agent.submitClass(cls); }
public static ClassHolder copyClass(ClassReader original, ClassHolder target, boolean withPrograms) { target.setLevel(original.getLevel()); target.getModifiers().addAll(original.readModifiers()); target.setParent(original.getParent()); target.getInterfaces().addAll(original.getInterfaces()); for (MethodReader method : original.getMethods()) { target.addMethod(copyMethod(method, withPrograms)); } for (FieldReader field : original.getFields()) { target.addField(copyField(field)); } target.setOwnerName(original.getOwnerName()); copyAnnotations(original.getAnnotations(), target.getAnnotations()); return target; }
method.setProgram(ProgramUtils.copy(program)); new UnreachableBasicBlockEliminator().optimize(program); cls.addMethod(method); } else { MethodDependency dep = getMethod(methodRef);
continue; renamedCls.addMethod(rename(method));
implementor.addMethod(bridge);
ctorBlock.add(exit); proxyClass.addMethod(ctor);
private MethodHolder createConstructor(ClassHierarchy hierarchy, ClassHolder implementor, ValueType[] types, TextLocation location) { ValueType[] signature = Arrays.copyOf(types, types.length + 1); signature[types.length] = ValueType.VOID; MethodHolder ctor = new MethodHolder("<init>", signature); ctor.setLevel(AccessLevel.PUBLIC); ProgramEmitter pe = ProgramEmitter.create(ctor, hierarchy); pe.setCurrentLocation(location); ValueEmitter thisVar = pe.var(0, implementor); thisVar.invokeSpecial(implementor.getParent(), "<init>"); for (int i = 0; i < types.length; ++i) { FieldHolder field = new FieldHolder("_" + i); field.setLevel(AccessLevel.PRIVATE); field.setType(types[i]); implementor.addField(field); thisVar.setField(field.getName(), pe.var(i + 1, types[i])); } pe.exit(); implementor.addMethod(ctor); return ctor; }
createMethod.getModifiers().add(ElementModifier.NATIVE); createMethod.getModifiers().add(ElementModifier.STATIC); cls.addMethod(createMethod); AnnotationHolder genAnnot = new AnnotationHolder(GeneratedBy.class.getName()); genAnnot.getValues().put("value", new AnnotationValue(ValueType.object(
classHolder.addMethod(exportedMethod); MethodHolder methodToCall = classHolder.getMethod(method); JSClassProcessor.makeSync(methodToCall != null ? methodToCall : exportedMethod);
implementor.addMethod(worker);
method.getModifiers().add(ElementModifier.STATIC); method.setProgram(program); cls.addMethod(method);
cls.addMethod(callerMethod); processProgram(callerMethod);
void processMemberMethods(ClassHolder cls) { for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { if (method.hasModifier(ElementModifier.STATIC)) { continue; } if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { ValueType[] staticSignature = getStaticSignature(method.getReference()); MethodHolder callerMethod = new MethodHolder(new MethodDescriptor(method.getName() + "$static", staticSignature)); callerMethod.getModifiers().add(ElementModifier.STATIC); Program program = ProgramUtils.copy(method.getProgram()); program.createVariable(); InstructionVariableMapper variableMapper = new InstructionVariableMapper(var -> program.variableAt(var.getIndex() + 1)); for (int i = program.variableCount() - 1; i > 0; --i) { program.variableAt(i).setDebugName(program.variableAt(i - 1).getDebugName()); program.variableAt(i).setLabel(program.variableAt(i - 1).getLabel()); } for (int i = 0; i < program.basicBlockCount(); ++i) { BasicBlock block = program.basicBlockAt(i); variableMapper.apply(block); } callerMethod.setProgram(program); ModelUtils.copyAnnotations(method.getAnnotations(), callerMethod.getAnnotations()); cls.addMethod(callerMethod); } } }
cls.addMethod(methodHolder); } finally { returnType = returnTypeBackup;