private void processInclude(AnnotationContainerReader container) { AnnotationReader annot = container.get(Include.class.getName()); if (annot == null) { return; } String includeString = annot.getValue("value").getString(); AnnotationValue systemValue = annot.getValue("isSystem"); if (systemValue == null || systemValue.getBoolean()) { includeString = "<" + includeString + ">"; } else { includeString = "\"" + includeString + "\""; } includes.add(includeString); }
private void writeAnnotation(DataOutput output, AnnotationReader annotation) throws IOException { output.writeInt(symbolTable.lookup(annotation.getType())); int fieldCount = 0; for (@SuppressWarnings("unused") String field : annotation.getAvailableFields()) { ++fieldCount; } output.writeShort(fieldCount); for (String field : annotation.getAvailableFields()) { output.writeInt(symbolTable.lookup(field)); writeAnnotationValue(output, annotation.getValue(field)); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) { if (method.getName().equals("annotationType")) { return annotationType; } else { ClassReader cls = hierarchy.getClassSource().get(reader.getType()); return cache.computeIfAbsent(method.getName(), name -> { MethodDescriptor desc = new MethodDescriptor(name, ValueType.parse(method.getReturnType())); MethodReader methodReader = cls.getMethod(desc); AnnotationValue value = reader.getValue(name); if (value == null) { value = methodReader.getAnnotationDefault(); } try { return convertValue(value, desc.getResultType()); } catch (Exception e) { throw new RuntimeException(e); } }); } }
private void reachGetAnnotations(DependencyAgent agent, DependencyNode node) { node.getClassValueNode().addConsumer(type -> { String className = type.getName(); ClassReader cls = agent.getClassSource().get(className); if (cls == null) { return; } for (AnnotationReader annotation : cls.getAnnotations().all()) { agent.linkClass(annotation.getType()); } createAnnotationClass(agent, className); }); }
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) { if (!reachedMethods.add(method.getReference())) { return; } ValueType type = method.getMethod().getResultType(); while (type instanceof ValueType.Array) { type = ((ValueType.Array) type).getItemType(); } if (type instanceof ValueType.Object) { String className = ((ValueType.Object) type).getClassName(); ClassReader cls = agent.getClassSource().get(className); if (cls != null && cls.hasModifier(ElementModifier.ANNOTATION)) { agent.linkClass(className); } } if (method.getMethod().hasModifier(ElementModifier.STATIC) && method.getMethod().getName().equals("$$__readAnnotations__$$")) { ClassReader cls = agent.getClassSource().get(method.getReference().getClassName()); if (cls != null) { for (AnnotationReader annotation : cls.getAnnotations().all()) { agent.linkClass(annotation.getType()); } } } MethodReference methodRef = method.getMethod().getReference(); if (methodRef.getClassName().equals("java.lang.Class") && methodRef.getName().equals("getAnnotations")) { reachGetAnnotations(agent, method.getVariable(0)); } }
private String extractSuggestedPropertyName(MethodReader method) { AnnotationReader annot = method.getAnnotations().get(JSProperty.class.getName()); AnnotationValue value = annot.getValue("value"); return value != null ? value.getString() : null; }
private static AnnotationHolder copyAnnotation(AnnotationReader annot) { AnnotationHolder copy = new AnnotationHolder(annot.getType()); for (String fieldName : annot.getAvailableFields()) { copy.getValues().put(fieldName, copyAnnotationValue(annot.getValue(fieldName))); } return copy; }
private MethodHolder addReader(DependencyAgent agent, ClassReader cls) { MethodHolder readerMethod = new MethodHolder("getAnnotations", ValueType.parse(Annotation[].class)); readerMethod.setLevel(AccessLevel.PUBLIC); ProgramEmitter pe = ProgramEmitter.create(readerMethod, agent.getClassHierarchy()); List<AnnotationReader> annotations = new ArrayList<>(); for (AnnotationReader annot : cls.getAnnotations().all()) { ClassReader annotType = agent.getClassSource().get(annot.getType()); if (annotType == null) { continue; } AnnotationReader retention = annotType.getAnnotations().get(Retention.class.getName()); if (retention != null) { String retentionPolicy = retention.getValue("value").getEnumValue().getFieldName(); if (retentionPolicy.equals("RUNTIME")) { annotations.add(annot); } } } ValueEmitter array = pe.constructArray(Annotation.class, annotations.size()); for (int i = 0; i < annotations.size(); ++i) { array.setElement(i, generateAnnotationInstance(agent, pe, annotations.get(i))); } array.returnValue(); return readerMethod; }
private String getPublicAlias(MethodReader method) { AnnotationReader annot = method.getAnnotations().get(JSMethodToExpose.class.getName()); return annot != null ? annot.getValue("name").getString() : null; }
private void writeAnnotation(DataOutput output, AnnotationReader annotation) throws IOException { output.writeInt(symbolTable.lookup(annotation.getType())); int fieldCount = 0; for (@SuppressWarnings("unused") String field : annotation.getAvailableFields()) { ++fieldCount; } output.writeShort(fieldCount); for (String field : annotation.getAvailableFields()) { output.writeInt(symbolTable.lookup(field)); writeAnnotationValue(output, annotation.getValue(field)); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("annotationType")) { return annotationType; } else { ClassReader cls = classSource.get(reader.getType()); return cache.computeIfAbsent(method.getName(), name -> { MethodDescriptor desc = new MethodDescriptor(name, ValueType.parse(method.getReturnType())); MethodReader methodReader = cls.getMethod(desc); AnnotationValue value = reader.getValue(name); if (value == null) { value = methodReader.getAnnotationDefault(); } try { return convertValue(value, desc.getResultType()); } catch (Exception e) { throw new RuntimeException(e); } }); } }
private MarkerKind isMarker(MemberReader member) { AnnotationReader annot = member.getAnnotations().get(PlatformMarker.class.getName()); if (annot == null) { return null; } AnnotationValue value = annot.getValue("value"); if (value == null) { return MarkerKind.TRUE; } String tagToMatch = value.getString(); for (String tag : tags) { if (tag.equals(tagToMatch)) { return MarkerKind.TRUE; } } return MarkerKind.FALSE; }
private static AnnotationHolder copyAnnotation(AnnotationReader annot) { AnnotationHolder copy = new AnnotationHolder(annot.getType()); for (String fieldName : annot.getAvailableFields()) { copy.getValues().put(fieldName, copyAnnotationValue(annot.getValue(fieldName))); } return copy; }
private String getSpecialName(MethodReference methodReference) { MethodReader method = classSource.resolve(methodReference); if (method == null) { return null; } AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName()); if (exportAnnot != null) { return exportAnnot.getValue("name").getString(); } AnnotationReader importAnnot = method.getAnnotations().get(Import.class.getName()); if (importAnnot != null) { return importAnnot.getValue("name").getString(); } return null; }
private void attachDependencyPlugin(MethodDependency methodDep) { if (methodDep.dependencyPluginAttached) { return; } methodDep.dependencyPluginAttached = true; methodDep.dependencyPlugin = dependencyPlugins.get(methodDep.getReference()); if (methodDep.dependencyPlugin != null || isBootstrap()) { return; } AnnotationReader depAnnot = methodDep.getMethod().getAnnotations().get(PluggableDependency.class.getName()); if (depAnnot == null) { return; } ValueType depType = depAnnot.getValue("value").getJavaClass(); String depClassName = ((ValueType.Object) depType).getClassName(); Class<?> depClass; try { depClass = Class.forName(depClassName, true, classLoader); } catch (ClassNotFoundException e) { throw new RuntimeException("Dependency plugin not found: " + depClassName, e); } try { methodDep.dependencyPlugin = (DependencyPlugin) depClass.newInstance(); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException("Can't instantiate dependency plugin " + depClassName, e); } }
public ImportedMethod getImportedMethod(MethodReference reference) { return importedMethods.computeIfAbsent(reference, ref -> { ClassReader cls = classSource.get(ref.getClassName()); if (cls == null) { return null; } MethodReader method = cls.getMethod(ref.getDescriptor()); if (method == null) { return null; } AnnotationReader annotation = method.getAnnotations().get(Import.class.getName()); if (annotation == null) { return null; } String name = annotation.getValue("name").getString(); AnnotationValue moduleValue = annotation.getValue("module"); String module = moduleValue != null ? moduleValue.getString() : null; if (module != null && module.isEmpty()) { module = null; } return new ImportedMethod(name, module); }); }
public static MetadataGenerator createMetadataGenerator(ClassLoader classLoader, MethodReader method, CallLocation callLocation, Diagnostics diagnostics) { AnnotationReader annot = method.getAnnotations().get(MetadataProvider.class.getName()); ValueType generatorType = annot.getValue("value").getJavaClass(); String generatorClassName = ((ValueType.Object) generatorType).getClassName();
@Override public void begin(RenderingManager manager, BuildTarget buildTarget) throws IOException { boolean hasOneResource = false; for (String className : manager.getClassSource().getClassNames()) { ClassReader cls = manager.getClassSource().get(className); AnnotationReader annot = cls.getAnnotations().get(JavaScriptResource.class.getName()); if (annot == null) { continue; } String path = annot.getValue("value").getString(); String packageName = className.substring(0, className.lastIndexOf('.')); String resourceName = packageName.replace('.', '/') + "/" + path; try (InputStream input = manager.getClassLoader().getResourceAsStream(resourceName)) { if (input == null) { throw new RenderingException("Error processing JavaScriptResource annotation on class " + className + ". Resource not found: " + resourceName); } StringWriter writer = new StringWriter(); IOUtils.copy(input, writer); writer.close(); manager.getWriter().append("// Resource " + path + " included by " + className).newLine(); manager.getWriter().append(writer.toString()).newLine().newLine(); } hasOneResource = true; } if (hasOneResource) { manager.getWriter().append("// TeaVM generated classes").newLine(); } } }
public Injector getInjector(MethodReference ref) { InjectorHolder holder = injectorMap.get(ref); if (holder == null) { holder = new InjectorHolder(null); if (!isBootstrap()) { ClassReader cls = classSource.get(ref.getClassName()); if (cls != null) { MethodReader method = cls.getMethod(ref.getDescriptor()); if (method != null) { AnnotationReader injectedByAnnot = method.getAnnotations().get(InjectedBy.class.getName()); if (injectedByAnnot != null) { ValueType type = injectedByAnnot.getValue("value").getJavaClass(); holder = new InjectorHolder(instantiateInjector(((ValueType.Object) type).getClassName())); } } } } injectorMap.put(ref, holder); } return holder.injector; }