/** Adds a set associated with a name/type. The data contained in the set is abstract. */ protected void addSet(Name name, short type, TypedObject set) { TypeMap nameInfo = findName(name); if (nameInfo == null) data.put(name, nameInfo = new TypeMap()); synchronized (nameInfo) { nameInfo.put(type, set); } }
/** Converts the NameSet to a String */ public String toString() { StringBuffer sb = new StringBuffer(); Iterator it = data.values().iterator(); while (it.hasNext()) { TypeMap nameInfo = (TypeMap) it.next(); Object[] elements = nameInfo.getAll(); if (elements == null) continue; for (int i = 0; i < elements.length; i++) { sb.append(elements[i]); sb.append("\n"); } } return sb.toString(); }
/** Removes the given set with the name and type. The data contained in the set is abstract. */ protected void removeSet(Name name, short type, TypedObject set) { TypeMap nameInfo = findName(name); if (nameInfo == null) return; Object o = nameInfo.get(type); if (o != set && type != Type.CNAME) { type = Type.CNAME; o = nameInfo.get(type); } if (o == set) { synchronized (nameInfo) { nameInfo.remove(type); } if (nameInfo.isEmpty()) data.remove(name); } }
/** Returns the contents of a Zone in master file format. */ public String toMasterFile() { Iterator znames = names(); StringBuffer sb = new StringBuffer(); while (znames.hasNext()) { Name name = (Name) znames.next(); TypeMap tm = findName(name); Object[] sets = tm.getAll(); for (int i = 0; i < sets.length; i++) { RRset rrset = (RRset) sets[i]; Iterator it = rrset.rrs(); while (it.hasNext()) sb.append(it.next() + "\n"); it = rrset.sigs(); while (it.hasNext()) sb.append(it.next() + "\n"); } } return sb.toString(); }
public Object next() { if (sentLastSOA) return null; if (!sentFirstSOA) { sentFirstSOA = true; return (RRset) findExactSet(origin, Type.SOA); } if (!sentNS) { sentNS = true; return getNS(); } if (!sentOrigin) { if (currentName == null) { currentName = getOrigin(); TypeMap tm = findName(currentName); current = (Object[]) tm.getAll(); count = 0; } while (count < current.length) { RRset rrset = (RRset) current[count]; if (rrset.getType() != Type.SOA && rrset.getType() != Type.NS) return current[count++]; count++; } current = null; sentOrigin = true; } if (current != null && count < current.length) return current[count++]; while (znames.hasNext()) { Name currentName = (Name) znames.next(); if (currentName.equals(getOrigin())) continue; TypeMap tm = findName(currentName); current = (Object[]) tm.getAll(); count = 0; if (count < current.length) return current[count++]; } sentLastSOA = true; RRset rrset = new RRset(); rrset.addRR(getSOA()); return rrset; }
private static boolean printMethodCallArgument( PrintWriter writer, ExecutableElement method, VariableElement param, Map<VariableElement, TypeInfo> typeinfos_instance, Mode mode, boolean first_parameter, TypeMap type_map) { if (!first_parameter) { writer.print(", "); } AnnotationMirror auto_annotation = Utils.getParameterAutoAnnotation(param); Constant constant_annotation = param.getAnnotation(Constant.class); if (constant_annotation != null) { writer.print(constant_annotation.value()); } else if (auto_annotation != null && mode == Mode.NORMAL) { Class param_type = NativeTypeTranslator.getClassFromType(auto_annotation.getAnnotationType()); if (AutoType.class.equals(param_type)) { final AutoType auto_type_annotation = param.getAnnotation(AutoType.class); final VariableElement auto_parameter = Utils.findParameter(method, auto_type_annotation.value()); final String auto_type = typeinfos_instance.get(auto_parameter).getAutoType(); if (auto_type == null) { throw new RuntimeException( "No auto type for parameter " + param.getSimpleName() + " in method " + method); } writer.print(auto_type); } else if (AutoSize.class.equals(param_type)) { final AutoSize auto_size_annotation = param.getAnnotation(AutoSize.class); if (!auto_size_annotation.useExpression()) { final String auto_parameter_name = auto_size_annotation.value(); final VariableElement auto_target_param = Utils.findParameter(method, auto_parameter_name); final TypeInfo auto_target_type_info = typeinfos_instance.get(auto_target_param); final boolean shift_remaining = !hasAnyParameterAutoTypeAnnotation(method, auto_target_param) && Utils.isParameterMultiTyped(auto_target_param); int shifting = 0; if (shift_remaining) { shifting = getBufferElementSizeExponent(auto_target_type_info.getType()); if (shifting > 0) { writer.print("("); } } if (auto_size_annotation.canBeNull()) { writer.print( "(" + auto_parameter_name + " == null ? 0 : " + auto_parameter_name + ".remaining())"); } else { writer.print(auto_parameter_name + ".remaining()"); } // Shift the remaining if the target parameter is multityped and there's no AutoType to // track type if (shift_remaining && shifting > 0) { writer.print(" << " + shifting); writer.print(")"); } } writer.print(auto_size_annotation.expression()); } else { throw new RuntimeException("Unknown auto annotation " + param_type); } } else { if (mode == Mode.BUFFEROBJECT && param.getAnnotation(BufferObject.class) != null) { writer.print(param.getSimpleName() + Utils.BUFFER_OBJECT_PARAMETER_POSTFIX); } else { Class type = typeinfos_instance.get(param).getType(); Check check_annotation = param.getAnnotation(Check.class); boolean hide_buffer = mode == Mode.AUTOS && getAutoTypeParameter(method, param) != null; if (hide_buffer) { writer.print("0L"); } else { if (type == CharSequence.class || type == CharSequence[].class) { final String offset = Utils.getStringOffset(method, param); writer.print("APIUtil.getBuffer"); if (param.getAnnotation(NullTerminated.class) != null) { writer.print("NT"); } writer.print('('); writer.print(type_map.getAPIUtilParam(true)); writer.print(param.getSimpleName()); if (offset != null) { writer.print(", " + offset); } writer.print(")"); } else { final AutoSize auto_size_annotation = param.getAnnotation(AutoSize.class); if (auto_size_annotation != null) { writer.print(auto_size_annotation.value() + "_"); } final Class buffer_type = Utils.getNIOBufferType(param.asType()); if (buffer_type == null) { writer.print(param.getSimpleName()); } else { writer.print("MemoryUtil.getAddress"); if (check_annotation != null && check_annotation.canBeNull()) { writer.print("Safe"); } writer.print("("); writer.print(param.getSimpleName()); writer.print(")"); } } } if (type != long.class) { PointerWrapper pointer_annotation = param.getAnnotation(PointerWrapper.class); if (pointer_annotation != null) { if (pointer_annotation.canBeNull()) { writer.print(" == null ? 0 : " + param.getSimpleName()); } writer.print(".getPointer()"); } } } } return false; }
private static void printMethodWithMultiType( ProcessingEnvironment env, TypeMap type_map, PrintWriter writer, TypeElement interface_decl, ExecutableElement method, Map<VariableElement, TypeInfo> typeinfos_instance, Mode mode, boolean generate_error_checks, boolean context_specific) { Utils.printDocComment(writer, method, env); if (method.getAnnotation(Deprecated.class) != null) { writer.println("\t@Deprecated"); } if (interface_decl.getAnnotation(Private.class) == null && method.getAnnotation(Private.class) == null) { writer.print("\tpublic static "); } else { writer.print("\tstatic "); } writer.print(getResultType(method, false)); StripPostfix strip_annotation = method.getAnnotation(StripPostfix.class); String method_name; Alternate alt_annotation = method.getAnnotation(Alternate.class); method_name = alt_annotation == null || alt_annotation.javaAlt() ? method.getSimpleName().toString() : alt_annotation.value(); if (strip_annotation != null && mode == Mode.NORMAL) { method_name = getPostfixStrippedName(type_map, interface_decl, method); } writer.print(" " + method_name + "("); generateParametersJava(writer, method, typeinfos_instance, false, true, mode); writer.println(") {"); final TypeMirror result_type = Utils.getMethodReturnType(method); boolean has_result = !result_type.equals(env.getTypeUtils().getNoType(TypeKind.VOID)); final Reuse reuse_annotation = method.getAnnotation(Reuse.class); if (reuse_annotation != null) { writer.print("\t\t"); if (has_result || method.getAnnotation(GLreturn.class) != null) { writer.print("return "); } writer.print( reuse_annotation.value() + "." + (reuse_annotation.method().length() > 0 ? reuse_annotation.method() : method_name) + "("); generateParametersJava(writer, method, typeinfos_instance, false, false, mode); writer.println(");\n\t}"); return; } if (context_specific) { type_map.printCapabilitiesInit(writer); writer.print( "\t\tlong " + Utils.FUNCTION_POINTER_VAR_NAME + " = " + type_map.getCapabilities() + "."); writer.println(Utils.getFunctionAddressName(interface_decl, method, true) + ";"); writer.print("\t\tBufferChecks.checkFunctionAddress("); writer.println(Utils.FUNCTION_POINTER_VAR_NAME + ");"); } final Code code_annotation = method.getAnnotation(Code.class); if (code_annotation != null && code_annotation.value().length() > 0) { writer.println(code_annotation.value()); } printBufferObjectChecks(writer, method, mode, context_specific); printParameterChecks(writer, method, typeinfos_instance, mode, generate_error_checks); printParameterCaching(writer, interface_decl, method, mode, context_specific); if (code_annotation != null && code_annotation.javaBeforeNative().length() > 0) { writer.println(code_annotation.javaBeforeNative()); } writer.print("\t\t"); final PointerWrapper pointer_wrapper_annotation = method.getAnnotation(PointerWrapper.class); if (has_result) { writer.print(getResultType(method, false) + " " + Utils.RESULT_VAR_NAME); if (code_annotation != null && code_annotation.tryBlock()) { writer.print(" = " + getDefaultResultValue(method)); writer.println(";\n\t\ttry {"); writer.print("\t\t\t" + Utils.RESULT_VAR_NAME); } writer.print(" = "); if (pointer_wrapper_annotation != null) { if (pointer_wrapper_annotation.factory().length() > 0) { writer.print(pointer_wrapper_annotation.factory() + "("); } else { writer.print("new " + getResultType(method, false) + "("); } } } else if (method.getAnnotation(GLreturn.class) != null) { has_result = true; Utils.printGLReturnPre(writer, method, method.getAnnotation(GLreturn.class), type_map); } writer.print(Utils.getSimpleNativeMethodName(method, generate_error_checks, context_specific)); if (mode == Mode.BUFFEROBJECT) { writer.print(Utils.BUFFER_OBJECT_METHOD_POSTFIX); } writer.print("("); boolean first_parameter = printMethodCallArguments(writer, method, typeinfos_instance, mode, type_map); if (context_specific) { if (!first_parameter) { writer.print(", "); } writer.print(Utils.FUNCTION_POINTER_VAR_NAME); } if (has_result && pointer_wrapper_annotation != null) { writer.print(")"); if (pointer_wrapper_annotation.params().length() > 0) { writer.print(", " + pointer_wrapper_annotation.params()); } } writer.println(");"); if (code_annotation != null && code_annotation.javaAfterNative().length() > 0) { writer.println(code_annotation.javaAfterNative()); } final String tabs = code_annotation != null && code_annotation.tryBlock() ? "\t\t\t" : "\t\t"; if (generate_error_checks && method.getAnnotation(NoErrorCheck.class) == null) { type_map.printErrorCheckMethod(writer, method, tabs); } // DISABLED: indirect buffer support // printNondirectParameterCopies(writer, method, mode); if (has_result) { if (method.getAnnotation(GLreturn.class) == null) { if (ByteBuffer.class.equals(Utils.getJavaType(result_type))) { writer.println( tabs + "return LWJGLUtil.CHECKS && " + Utils.RESULT_VAR_NAME + " == null ? null : " + Utils.RESULT_VAR_NAME + ".order(ByteOrder.nativeOrder());"); // safeNewBuffer returns a direct // ByteBuffer with BIG_ENDIAN order. } else { writer.println(tabs + "return " + Utils.RESULT_VAR_NAME + ";"); } } else { Utils.printGLReturnPost(writer, method, method.getAnnotation(GLreturn.class), type_map); } } if (code_annotation != null && code_annotation.tryBlock()) { writer.println("\t\t} finally {"); writer.println(code_annotation.javaFinally()); writer.println("\t\t}"); } writer.println("\t}"); }
private static boolean generateBufferParameterAddress( TypeMap type_map, PrintWriter writer, MethodDeclaration method, ParameterDeclaration param, Mode mode, boolean loopDeclared) { NativeTypeTranslator translator = new NativeTypeTranslator(type_map, param); param.getType().accept(translator); writer.print("\t" + translator.getSignature() + param.getSimpleName()); writer.print(BUFFER_ADDRESS_POSTFIX + " = (("); writer.print(translator.getSignature()); Check check_annotation = param.getAnnotation(Check.class); writer.print(")"); if (mode == Mode.BUFFEROBJECT && param.getAnnotation(BufferObject.class) != null) { writer.print( "offsetToPointer(" + param.getSimpleName() + Utils.BUFFER_OBJECT_PARAMETER_POSTFIX + "))"); } else { Class java_type = Utils.getJavaType(param.getType()); if (Buffer.class.isAssignableFrom(java_type) || java_type.equals(CharSequence.class) || java_type.equals(CharSequence[].class)) { boolean explicitly_byte_sized = java_type.equals(Buffer.class) || translator.getAnnotationType().equals(type_map.getVoidType()); if (explicitly_byte_sized) writer.print("(((char *)"); if (method.getAnnotation(GenerateAutos.class) != null || (check_annotation != null && check_annotation.canBeNull())) { writer.print("safeGetBufferAddress(env, " + param.getSimpleName()); } else { writer.print("(*env)->GetDirectBufferAddress(env, " + param.getSimpleName()); } writer.print("))"); writer.print(" + " + param.getSimpleName() + BUFFER_POSITION_POSTFIX); if (explicitly_byte_sized) writer.print("))"); } else if (java_type.equals(String.class)) { writer.print("GetStringNativeChars(env, " + param.getSimpleName() + "))"); } else throw new RuntimeException("Illegal type " + java_type); } writer.println(";"); if (param.getAnnotation(StringList.class) != null) { if (Utils.getJavaType(param.getType()) != CharSequence[].class && (param.getAnnotation(GLchar.class) == null || param.getAnnotation(NullTerminated.class) == null || param.getAnnotation(NullTerminated.class).value().length() == 0)) throw new RuntimeException( "StringList annotation can only be applied on null-terminated GLchar buffers."); if ("_str".equals(param.getSimpleName())) throw new RuntimeException( "The name '_str' is not valid for arguments annotated with StringList"); // Declare loop counters and allocate string array if (!loopDeclared) { writer.println("\tunsigned int _str_i;"); writer.println("\tGLchar *_str_address;"); loopDeclared = true; } writer.println( "\tGLchar **" + param.getSimpleName() + STRING_LIST_POSTFIX + " = (GLchar **) malloc(" + param.getAnnotation(StringList.class).value() + "*sizeof(GLchar*));"); } return loopDeclared; }
/** Finds all matching sets or something that causes the lookup to stop. */ protected Object findSets(Name name, short type) { Object bestns = null; Object o; Name tname; int labels; int olabels; int tlabels; if (!name.subdomain(origin)) return null; labels = name.labels(); olabels = origin.labels(); for (tlabels = olabels; tlabels <= labels; tlabels++) { if (tlabels == olabels) tname = origin; else if (tlabels == labels) tname = name; else tname = new Name(name, labels - tlabels); TypeMap nameInfo = findName(tname); if (nameInfo == null) continue; /* If this is an ANY lookup, return everything. */ if (tlabels == labels && type == Type.ANY) return nameInfo.getAll(); /* Look for an NS */ if (tlabels > olabels || isCache) { o = nameInfo.get(Type.NS); if (o != null) { if (isCache) bestns = o; else return o; } } /* If this is the name, look for the actual type. */ if (tlabels == labels) { o = nameInfo.get(type); if (o != null) return o; } /* If this is the name, look for a CNAME */ if (tlabels == labels) { o = nameInfo.get(Type.CNAME); if (o != null) return o; } /* Look for a DNAME, unless this is the actual name */ if (tlabels < labels) { o = nameInfo.get(Type.DNAME); if (o != null) return o; } /* * If this is the name and this is a cache, look for an * NXDOMAIN entry. */ if (tlabels == labels && isCache) { o = nameInfo.get((short) 0); if (o != null) return o; } /* * If this is the name and we haven't matched anything, * just return the name. */ if (tlabels == labels) return nameInfo; } if (bestns == null) return null; else return bestns; }
/** * Finds all sets that exactly match. This does not traverse CNAMEs or handle Type ANY queries. */ protected Object findExactSet(Name name, short type) { TypeMap nameInfo = findName(name); if (nameInfo == null) return null; return nameInfo.get(type); }