public ClassHolder rename(ClassHolder cls) { ClassHolder renamedCls = new ClassHolder(classNameMapper.map(cls.getName())); renamedCls.getModifiers().addAll(cls.getModifiers()); renamedCls.setLevel(cls.getLevel()); String parent = cls.getParent(); AnnotationHolder superclassAnnot = cls.getAnnotations().get(Superclass.class.getName()); if (superclassAnnot != null) { parent = superclassAnnot.getValues().get("value").getString(); renamedCls.setParent(parent != null ? classNameMapper.map(parent) : null); if (renamedCls.getName().equals(renamedCls.getParent())) { renamedCls.setParent(null); for (MethodHolder method : cls.getMethods()) { if (method.getAnnotations().get(Remove.class.getName()) != null) { continue; renamedCls.addMethod(rename(method)); for (FieldHolder field : cls.getFields().toArray(new FieldHolder[0])) { renamedCls.addField(rename(field)); if (cls.getOwnerName() != null) { renamedCls.setOwnerName(classNameMapper.map(cls.getOwnerName())); rename(cls.getAnnotations(), renamedCls.getAnnotations()); for (String iface : cls.getInterfaces()) { String mappedIfaceName = classNameMapper.map(iface); if (!mappedIfaceName.equals(renamedCls.getName())) { renamedCls.getInterfaces().add(mappedIfaceName);
public void link(ClassHolder cls) { for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { MethodReference methodRef = method.getReference(); MethodDependencyInfo methodDep = dependency.getMethod(methodRef); if (methodDep == null) { cls.removeMethod(method); } else if (!methodDep.isUsed()) { method.getModifiers().add(ElementModifier.ABSTRACT); method.setProgram(null); } else if (method.getProgram() != null) { link(method.getReference(), method.getProgram()); } } for (FieldHolder field : cls.getFields().toArray(new FieldHolder[0])) { FieldReference fieldRef = new FieldReference(cls.getName(), field.getName()); if (dependency.getField(fieldRef) == null) { cls.removeField(field); } } }
private void writeClass(OutputStream stream, ClassHolder cls) throws IOException { DataOutput output = new DataOutputStream(stream); output.writeByte(cls.getLevel().ordinal()); output.writeInt(packModifiers(cls.getModifiers())); output.writeInt(cls.getParent() != null ? symbolTable.lookup(cls.getParent()) : -1); output.writeInt(cls.getOwnerName() != null ? symbolTable.lookup(cls.getOwnerName()) : -1); output.writeByte(cls.getInterfaces().size()); for (String iface : cls.getInterfaces()) { output.writeInt(symbolTable.lookup(iface)); } writeAnnotations(output, cls.getAnnotations()); output.writeShort(cls.getFields().size()); for (FieldHolder field : cls.getFields()) { writeField(output, field); } output.writeShort(cls.getMethods().size()); for (MethodHolder method : cls.getMethods()) { writeMethod(stream, method); } }
@Override public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) { if (!cls.getName().equals("java.lang.Class")) { return; } for (MethodHolder method : cls.getMethods()) { patchProgram(method.getProgram()); } }
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)); } } }
ClassHolder implementor = new ClassHolder(key + "$lambda$_" + id + "_" + subId); lambdaIdsByMethod.put(callSite.getCaller(), subId + 1); implementor.setLevel(AccessLevel.PUBLIC); if (samClass != null && samClass.hasModifier(ElementModifier.INTERFACE)) { implementor.setParent("java.lang.Object"); implementor.getInterfaces().add(samName); } else { implementor.setParent(samName); implementor.addMethod(worker); implementor.getInterfaces().add("java.io.Serializable"); for (int i = 0; i < markerCount; ++i) { ValueType markerType = callSite.getBootstrapArguments().get(bootstrapArgIndex++).getValueType(); implementor.getInterfaces().add(((ValueType.Object) markerType).getClassName()); dependencies.addAll(implementor.getInterfaces()); if (!implementor.getParent().equals("java.lang.Object")) { dependencies.add(implementor.getParent()); callSite.getAgent().getIncrementalCache().addDependencies(implementor.getName(), dependencies.toArray(new String[0]));
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; }
public ClassHolder parseClass(ClassNode node) { ClassHolder cls = new ClassHolder(referenceCache.getCached(node.name.replace('/', '.'))); parseModifiers(node.access, cls, DECL_CLASS); if (node.superName != null) { cls.setParent(referenceCache.getCached(node.superName.replace('/', '.'))); if (cls.getName().equals("java.lang.Object")) { cls.setParent(null); cls.getInterfaces().add(referenceCache.getCached(iface.replace('/', '.'))); FieldNode fieldNode = (FieldNode) obj; FieldHolder field = parseField(fieldNode); cls.addField(field); field.updateReference(referenceCache); for (MethodNode methodNode : node.methods) { MethodHolder method = parseMethod(methodNode, fullFileName); cls.addMethod(method); method.updateReference(referenceCache); cls.setOwnerName(node.outerClass.replace('/', '.')); } else { int lastIndex = node.name.lastIndexOf('$'); if (lastIndex != -1) { cls.setOwnerName(node.name.substring(0, lastIndex).replace('/', '.')); parseAnnotations(cls.getAnnotations(), node.visibleAnnotations, node.invisibleAnnotations); return cls;
public ClassNode decompile(ClassHolder cls) { ClassNode clsNode = new ClassNode(cls.getName(), cls.getParent()); for (FieldHolder field : cls.getFields()) { FieldNode fieldNode = new FieldNode(field.getReference(), field.getType()); fieldNode.getModifiers().addAll(field.getModifiers()); fieldNode.setInitialValue(field.getInitialValue()); clsNode.getFields().add(fieldNode); } for (MethodHolder method : cls.getMethods()) { if (method.getModifiers().contains(ElementModifier.ABSTRACT)) { continue; } if ((!isBootstrap() && method.getAnnotations().get(InjectedBy.class.getName()) != null) || methodsToSkip.contains(method.getReference())) { continue; } MethodNode methodNode = decompile(method); clsNode.getMethods().add(methodNode); } clsNode.getInterfaces().addAll(cls.getInterfaces()); clsNode.getModifiers().addAll(cls.getModifiers()); clsNode.setAccessLevel(cls.getLevel()); return clsNode; }
private boolean isSystemClass(ClassHolder cls) { switch (cls.getName()) { case "java.lang.Object": case "java.lang.Class": case "java.lang.String": return true; default: return false; } }
private ClassHolder createImplementor(ClassHierarchy hierarchy, String annotationType, String implementorName) { ClassHolder implementor = new ClassHolder(implementorName); implementor.setParent("java.lang.Object"); implementor.getInterfaces().add(annotationType); implementor.getModifiers().add(ElementModifier.FINAL); implementor.setLevel(AccessLevel.PUBLIC); field.setType(methodDecl.getResultType()); field.setLevel(AccessLevel.PRIVATE); implementor.addField(field); implementor.addMethod(accessor); implementor.addMethod(ctor); implementor.addMethod(annotTypeMethod);
void makeSync(ClassHolder cls) { Set<MethodDescriptor> methods = new HashSet<>(); findInheritedMethods(cls, methods, new HashSet<>()); for (MethodHolder method : cls.getMethods()) { if (methods.contains(method.getDescriptor())) { makeSync(method); } } }
if (typeHelper.isJavaScriptClass(cls.getName())) { processor.processMemberMethods(cls); if (typeHelper.isJavaScriptImplementation(cls.getName())) { processor.makeSync(cls); for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { if (method.getProgram() != null) { processor.processProgram(method); MethodReference functorMethod = processor.isFunctor(cls.getName()); if (functorMethod != null) { if (processor.isFunctor(cls.getParent()) != null) { functorMethod = null; ClassReader originalClass = hierarchy.getClassSource().get(cls.getName()); ExposedClass exposedClass; if (originalClass != null) { exposedClass = getExposedClass(cls.getName()); } else { exposedClass = new ExposedClass();
private void replaceMethods(ClassHolder cls, Class<?> emuType, ClassReaderSource innerSource, List<MethodDescriptor> descList) { ClassReader emuCls = innerSource.get(emuType.getName()); for (MethodDescriptor methodDesc : descList) { cls.removeMethod(cls.getMethod(methodDesc)); cls.addMethod(ModelUtils.copyMethod(emuCls.getMethod(methodDesc))); } }
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); }
int index = 0; for (MethodDescriptor method : classToExpose.methods.keySet()) { MethodReference methodRef = new MethodReference(classHolder.getName(), method); CallLocation callLocation = new CallLocation(methodRef); classHolder.addMethod(exportedMethod); MethodHolder methodToCall = classHolder.getMethod(method); JSClassProcessor.makeSync(methodToCall != null ? methodToCall : exportedMethod);
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; }
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); } } } }
field.setLevel(AccessLevel.PRIVATE); field.getModifiers().add(ElementModifier.STATIC); cls.addField(field); 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(