private static String enclosingClassAndMemberNameToString(ClassTemplateSpec enclosingClass, String memberName) { final StringBuilder sb = new StringBuilder(); if (memberName != null) { sb.append(" in "); sb.append(memberName); } if (enclosingClass != null) { sb.append(" in "); sb.append(enclosingClass.getFullName()); } return sb.toString(); }
/** * Instead of generate spec for the specify {@link com.linkedin.data.schema.DataSchema}, assume it is already defined in the system. */ public void registerDefinedSchema(DataSchema schema) { final ClassTemplateSpec spec = createFromDataSchema(schema); _schemaToClassMap.put(schema, spec); _classNameToSchemaMap.put(spec.getFullName(), schema); }
/** * Register a new class TemplateSpec. * <p/> * Registration is necessary to associate the {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec} with the source file for which it was generated. This may be used later to determine if generated class should be emitted * based on the location of the source file. * <p/> * Registration also associates the {@link com.linkedin.data.schema.DataSchema} to the generated {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec} and the generated class's full name to the the {@link com.linkedin.pegasus.generator.spec.ClassTemplateSpec}. * * @param schema provides the {@link com.linkedin.data.schema.DataSchema} of the generated class. * @param classTemplateSpec provides the generated class. */ private void registerClassTemplateSpec(DataSchema schema, ClassTemplateSpec classTemplateSpec) { classTemplateSpec.setLocation(currentLocation().toString()); _schemaToClassMap.put(schema, classTemplateSpec); _classNameToSchemaMap.put(classTemplateSpec.getFullName(), schema); _classToDataSchemaLocationMap.put(classTemplateSpec, currentLocation()); if (schema instanceof NamedDataSchema) { checkClassNameForSpecialSuffix(classTemplateSpec.getFullName()); } _classTemplateSpecs.add(classTemplateSpec); }
@Override public GeneratedCode generate(ClassTemplateSpec templateSpec) { if (predef.contains(templateSpec.getSchema())) { return null; } JavaDataTemplateGenerator.Config config = new JavaDataTemplateGenerator.Config(); JavaDataTemplateGenerator generator = new JavaDataTemplateGenerator(config); JClass result = generator.generate(templateSpec); ByteArrayOutputStream out = new ByteArrayOutputStream(); String code; try { generator.getCodeModel().build( new CapturingCodeWriter(templateSpec.getNamespace(), templateSpec.getClassName(), out)); out.flush(); out.close(); code = out.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Error generating code for " + templateSpec.getFullName(), e); } if (code.trim().equals("")) { throw new RuntimeException("Failed to generate code for " + templateSpec.getFullName()); } return new GeneratedCode( new JavaCompilationUnit( result.name(), result._package().name()), code); }
if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName()))
"Internal error in generator while processing " + templateSpec.getFullName(), e); templateSpec.getFullName(), ""); code = formatter.format(code); return new GeneratedCode(compilationUnit, code);
/** * See {@link org.coursera.courier.swift.SwiftProperties} for customization options. */ @Override public GeneratedCode generate(ClassTemplateSpec templateSpec) { String code; SwiftProperties swiftProperties = globalConfig.lookupSwiftProperties(templateSpec); if (swiftProperties.omit) return null; SwiftSyntax syntax = new SwiftSyntax(templateSpec, swiftProperties, globalConfig); SwiftyJSON swifty = new SwiftyJSON(syntax); try { if (templateSpec instanceof RecordTemplateSpec) { code = engine.render("rythm-swift/record.txt", templateSpec, syntax, swifty); } else if (templateSpec instanceof EnumTemplateSpec) { code = engine.render("rythm-swift/enum.txt", templateSpec); } else if (templateSpec instanceof UnionTemplateSpec) { code = engine.render("rythm-swift/union.txt", templateSpec, syntax, swifty); } else { return null; // Indicates that we are declining to generate code for the type (e.g. map or array) } } catch (RythmException e) { throw new RuntimeException( "Internal error in generator while processing " + templateSpec.getFullName(), e); } SwiftCompilationUnit compilationUnit = new SwiftCompilationUnit( templateSpec.getClassName(), ""); code = formatter.format(code); return new GeneratedCode(compilationUnit, code); }
return new ClassInfo(enclosingClass.getFullName(), CodeUtil.capitalize(memberName));