boolean isProperFunctor(ClassReader type) { if (!type.hasModifier(ElementModifier.INTERFACE)) { return false; } return type.getMethods().stream() .filter(method -> method.hasModifier(ElementModifier.ABSTRACT)) .count() == 1; }
@Override public ReflectMethod[] getDeclaredMethods() { resolve(); if (classReader == null) { return new ReflectMethod[0]; } return classReader.getMethods().stream() .filter(method -> !method.getName().equals("<clinit>")) .map(method -> getDeclaredMethod(method.getDescriptor())) .toArray(ReflectMethod[]::new); }
private boolean hasClassesToExpose() { for (String className : classSource.getClassNames()) { ClassReader cls = classSource.get(className); if (cls.getMethods().stream().anyMatch(method -> getPublicAlias(method) != null)) { return true; } } return false; }
private void getFunctorMethods(String className, Map<MethodDescriptor, MethodReference> methods) { classSource.getAncestors(className).forEach(cls -> { if (cls.getAnnotations().get(JSFunctor.class.getName()) != null && marshaller.isProperFunctor(cls)) { MethodReference method = cls.getMethods().iterator().next().getReference(); if (!methods.containsKey(method.getDescriptor())) { methods.put(method.getDescriptor(), method); } } }); }
private int indexOfMethod(ClassReader cls, MethodReader method) { int index = 0; for (MethodReader m : cls.getMethods()) { if (m.getDescriptor().equals(method.getDescriptor())) { return index; } ++index; } return -1; }
private void addPreservedMethods(String ifaceName, Set<MethodDescriptor> methods) { ClassReader iface = classSource.get(ifaceName); for (MethodReader method : iface.getMethods()) { methods.add(method.getDescriptor()); } for (String superIfaceName : iface.getInterfaces()) { addPreservedMethods(superIfaceName, methods); } }
private void printStats(TeaVM vm, long startTime) { if (vm.getWrittenClasses() != null) { int classCount = vm.getWrittenClasses().getClassNames().size(); int methodCount = 0; for (String className : vm.getWrittenClasses().getClassNames()) { ClassReader cls = vm.getWrittenClasses().get(className); methodCount += cls.getMethods().size(); } log.info("Classes compiled: " + classCount); log.info("Methods compiled: " + methodCount); } log.info("Compilation took " + (System.currentTimeMillis() - startTime) + " ms"); }
@Override public void classReached(DependencyAgent agent, String className) { for (MethodReader reader : agent.getClassSource().get(className).getMethods()) { AnnotationReader annotation = reader.getAnnotations().get(Export.class.getName()); if (annotation != null) { agent.linkMethod(reader.getReference()).use(); } } }
private void printStats() { if (vm == null || vm.getWrittenClasses() == null) { return; } int classCount = vm.getWrittenClasses().getClassNames().size(); int methodCount = 0; for (String className : vm.getWrittenClasses().getClassNames()) { ClassReader cls = vm.getWrittenClasses().get(className); methodCount += cls.getMethods().size(); } log.info("Classes compiled: " + classCount); log.info("Methods compiled: " + methodCount); }
public void addClasses(ListableClassReaderSource classReader) { ProgramSourceAggregator sourceAggregator = new ProgramSourceAggregator(sourceFiles); for (String className : classReader.getClassNames()) { ClassReader cls = classReader.get(className); for (MethodReader method : cls.getMethods()) { if (method.getProgram() == null) { continue; } sourceAggregator.addLocationsOfProgram(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)); } } }
private void reachFunctorMethods(DependencyAgent agent, String type) { ClassReader cls = agent.getClassSource().get(type); if (cls != null) { for (MethodReader method : cls.getMethods()) { if (!method.hasModifier(ElementModifier.STATIC)) { agent.linkMethod(method.getReference()).use(); } } } }
private Program generateAfterProgram(MethodHolder method, ClassHierarchy hierarchy) { ProgramEmitter pe = ProgramEmitter.create(method, hierarchy); ValueEmitter testCaseVar = pe.getField(TestEntryPoint.class, "testCase", Object.class); List<ClassReader> classes = collectSuperClasses(pe.getClassSource(), testMethod.getClassName()); classes.stream() .flatMap(cls -> cls.getMethods().stream()) .filter(m -> m.getAnnotations().get(JUNIT4_AFTER) != null) .forEach(m -> testCaseVar.cast(ValueType.object(m.getOwnerName())).invokeVirtual(m.getReference())); if (hierarchy.isSuperType(JUNIT3_BASE_CLASS, testMethod.getClassName(), false)) { testCaseVar.cast(ValueType.object(JUNIT3_BASE_CLASS)).invokeVirtual(JUNIT3_AFTER); } pe.exit(); return pe.getProgram(); }
private boolean hasAsyncMethods() { boolean result = false; loop: for (String clsName : classSource.getClassNames()) { ClassReader cls = classSource.get(clsName); for (MethodReader method : cls.getMethods()) { if (!asyncMethods.contains(method.getReference()) || method.getProgram() == null) { continue; } if (hasMonitor(method)) { result = true; break loop; } } } ClassReader cls = classSource.get("java.lang.Thread"); MethodReader method = cls != null ? cls.getMethod(new MethodDescriptor("start", void.class)) : null; return result && method != null; }
@Override public void classReached(DependencyAgent agent, String className) { ClassReader cls = agent.getClassSource().get(className); for (MethodReader method : cls.getMethods()) { AnnotationReader exposeAnnot = method.getAnnotations().get(JSMethodToExpose.class.getName()); if (exposeAnnot != null) { MethodDependency methodDep = agent.linkMethod(method.getReference()); methodDep.getVariable(0).propagate(agent.getType(className)); methodDep.use(); } } } }
private ValueEmitter generateAnnotationInstance(DependencyAgent agent, ProgramEmitter pe, AnnotationReader annotation) { ClassReader annotationClass = agent.getClassSource().get(annotation.getType()); if (annotationClass == null) { return pe.constantNull(ValueType.object(annotation.getType())); } String className = getAnnotationImplementor(agent, annotation.getType()); List<ValueEmitter> params = new ArrayList<>(); for (MethodReader methodDecl : annotationClass.getMethods()) { AnnotationValue value = annotation.getValue(methodDecl.getName()); if (value == null) { value = methodDecl.getAnnotationDefault(); } params.add(generateAnnotationValue(agent, pe, methodDecl.getResultType(), value) .cast(methodDecl.getResultType())); } return pe.construct(className, params.toArray(new ValueEmitter[0])); }
@Override public void methodReached(DependencyAgent agent, MethodDependency method) { AnnotationReader delegateAnnot = method.getMethod().getAnnotations().get(DelegateTo.class.getName()); if (delegateAnnot != null) { String delegateMethodName = delegateAnnot.getValue("value").getString(); ClassReader cls = agent.getClassSource().get(method.getReference().getClassName()); for (MethodReader delegate : cls.getMethods()) { if (delegate.getName().equals(delegateMethodName)) { if (delegate != method.getMethod()) { MethodDependency delegateDep = agent.linkMethod(delegate.getReference()); method.addLocationListener(delegateDep::addLocation); delegateDep.use(); } } } } }
@Override public void methodReached(DependencyAgent agent, MethodDependency method) { AnnotationReader delegateAnnot = method.getMethod().getAnnotations().get(DelegateTo.class.getName()); if (delegateAnnot != null) { String delegateMethodName = delegateAnnot.getValue("value").getString(); ClassReader cls = agent.getClassSource().get(method.getReference().getClassName()); for (MethodReader delegate : cls.getMethods()) { if (delegate.getName().equals(delegateMethodName)) { if (delegate != method.getMethod()) { MethodDependency dep = agent.linkMethod(delegate.getReference()); dep.use(); method.addLocationListener(dep::addLocation); } } } } }
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; }
@Override public void methodReached(DependencyAgent agent, MethodDependency methodDep) { MethodModel proxy = describer.getMethod(methodDep.getReference()); if (proxy != null && installedProxies.add(proxy)) { agent.getIncrementalCache().setNoCache(methodDep.getReference()); ClassReader cls = agent.getClassSource().get(methodDep.getMethod().getOwnerName()); int index = 0; for (MethodReader method : cls.getMethods()) { if (method.getDescriptor().equals(methodDep.getMethod().getDescriptor())) { break; } ++index; } new UsageGenerator(agent, proxy, methodDep, proxyClassLoader, index).installProxyEmitter(); } }