public void renderModelAt(TileEntityGenerator tile, double x, double y, double z, float par8) { int meta = tile.worldObj.getBlockMetadata(tile.xCoord, tile.yCoord, tile.zCoord); float angle; switch (meta & 3) { case 0: angle = 0; break; case 1: angle = 90; break; case 2: angle = 180; break; case 3: angle = -90; break; default: angle = 45; break; } GL11.glPushMatrix(); GL11.glEnable(GL12.GL_RESCALE_NORMAL); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glTranslatef((float) x, (float) y + 1.0F, (float) z + 1.0F); GL11.glScalef(1.0F, -1.0F, -1.0F); GL11.glTranslatef(0.5F, 0.5F - 1F, 0.5F); bindTextureByName("/jaffas_generator.png"); GL11.glRotatef(angle, 0, 1.0f, 0); generator.render(0.0625F, BlockGenerator.isBurning(meta)); GL11.glDisable(GL12.GL_RESCALE_NORMAL); GL11.glPopMatrix(); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); label.renderLivingLabel(tile, "Generator\nEnergy: 10/20\nConnected: No", 128, x, y, z, 40, 3); }
private ContainerRoot executeOnDirectory(File dir) throws MojoExecutionException { ContainerRoot mergedModel = KevoreeFactory.createContainerRoot(); if (dir.listFiles() != null) { for (File f : dir.listFiles()) { if (f.isDirectory()) { ContainerRoot model = executeOnDirectory(f); mergedModel = mergerComponent.merge(mergedModel, model); } else { // try { ContainerRoot model = ModelGenerator.generate(f.getAbsolutePath(), project); mergedModel = mergerComponent.merge(mergedModel, model); /*} catch (Exception e) { getLog().error("Unable to parse the source file", e); }*/ } } } return mergedModel; }
@Test public void testCreateModel() { ModelBean modelBean = ModelGenerator.createModel(BeanWithAnnotationsDisablePaging.class); assertThat(modelBean.getReadMethod()).isEqualTo("read"); assertThat(modelBean.getCreateMethod()).isNull(); assertThat(modelBean.getUpdateMethod()).isNull(); assertThat(modelBean.getDestroyMethod()).isNull(); assertThat(modelBean.getIdProperty()).isEqualTo("id"); assertThat(modelBean.getVersionProperty()).isNull(); assertThat(modelBean.isDisablePagingParameters()).isTrue(); assertThat(modelBean.isPaging()).isFalse(); assertThat(modelBean.getMessageProperty()).isEqualTo("theMessageProperty"); assertThat(modelBean.getName()).isEqualTo("Sch.Bean2"); assertThat(modelBean.getFields()).hasSize(3); assertThat(BeanWithAnnotationsDisablePaging.expectedFields).hasSize(3); for (ModelFieldBean expectedField : BeanWithAnnotationsDisablePaging.expectedFields) { ModelFieldBean field = modelBean.getFields().get(expectedField.getName()); assertThat(field).isEqualsToByComparingFields(expectedField); } }
private void implementAction( TreeLogger logger, ModelGenerator generator, JMethod method, ModelMagic models, Annotation[] annos) { boolean fluent = ModelGeneratorGwt.isFluent(method); JPrimitiveType primitive = method.getReturnType().isPrimitive(); if (primitive != JPrimitiveType.VOID) { if (!fluent) { // non-fluent, non-void return type is not an action // TODO change this! // implementGetter(logger, mb, method, models, annos, // method.getReturnType().getSimpleSourceName()); logger.log( Type.ERROR, "No getter for " + method.getJsniSignature() + "; " + "If your type does not use javabean getField() naming conventions, " + "then you MUST annotate a getter field with @GetterField"); } return; } MethodBuffer mb = generator.createMethod( method.getReturnType().getQualifiedSourceName(), method.getName(), ModelGeneratorGwt.typeToParameterString(method.getParameterTypes())); if (method.getName().equals("clear")) { // implement clear } if (fluent) { mb.println("return this;"); } }
public void build( TreeLogger logger, SourceBuilder<ModelMagic> builder, GeneratorContext ctx, JClassType type) throws UnableToCompleteException { ModelMagic models = builder.getPayload(); ModelGenerator generator = new ModelGenerator(builder); // Step one; determine if we already have a concrete type or not. // TODO if JClassType is final, we need to wrap it using a delegate model. JClassType concrete; // ClassBuffer cb = builder.getClassBuffer(); JClassType root = models.getRootType(logger, ctx); if (type.isInterface() == null && type != root) { concrete = type; generator.setSuperClass(type.getQualifiedSourceName()); } else { // We have an interface on our hands; search for an existing or // buildable model. // search for a supertype to inherit. Anything that extends Model, // does not implement any method we do implement, preferred type // being the one that implements the most interfaces possible final JClassType model; try { model = ctx.getTypeOracle().getType(Model.class.getName()); } catch (NotFoundException e) { logger.log( Type.ERROR, "Cannot load " + Model.class.getName() + "; " + "make sure you have xapi-gwt-model:sources.jar on classpath."); throw new UnableToCompleteException(); } concrete = model; for (JClassType supertype : concrete.getFlattenedSupertypeHierarchy()) { // Only interfaces explicitly extending Model become concrete. if (ModelGeneratorGwt.canBeSupertype(type, supertype)) { // prefer the concrete type with the most methods in common. concrete = supertype; } } if (concrete == null || concrete == model) { concrete = models.getRootType(logger, ctx); generator.setSuperClass(concrete.getQualifiedSourceName()); } else { // We have to make sure this concrete supertype is created. if (!models.hasModel(concrete.getQualifiedSourceName())) { // Concrete type is not cached. Build it now. RebindResult result = ModelGeneratorGwt.execImpl(logger, ctx, concrete.getQualifiedSourceName()); generator.setSuperClass(result.getResultTypeName()); } } } // This will probably become jsni, if we can avoid jso interface sickness... generator.createFactory(type.getQualifiedSourceName()); HasModelFields fieldMap = new HasModelFields(); fieldMap.setDefaultSerializable(type.getAnnotation(Serializable.class)); for (JMethod method : methods.keySet()) { if (!toGenerate.contains(ModelGeneratorGwt.toSignature(method))) { logger.log( Type.TRACE, "Skipping method defined in supertype: " + method.getJsniSignature()); continue; } Annotation[] annos = methods.get(method); String methodName = method.getName(); String returnType = method.getReturnType().getQualifiedSourceName(); String params = ModelGeneratorGwt.typeToParameterString(method.getParameterTypes()); // TODO: check imports if we are safe to use simple name. returnType = method.getReturnType().getSimpleSourceName(); IsType returns = X_Source.binaryToSource(method.getReturnType().getQualifiedBinaryName()); IsType[] parameters = ModelGeneratorGwt.toTypes(method.getParameterTypes()); GetterFor getter = method.getAnnotation(GetterFor.class); if (getter != null) { String name = getter.value(); if (name.length() == 0) { name = ModelUtil.stripGetter(method.getName()); } ModelField field = fieldMap.getOrMakeField(name); field.setType(returnType); assert parameters.length == 0 : "A getter method cannot have parameters. " + "Generated code requires using getter methods without args. You provided " + method.getJsniSignature(); grabAnnotations(logger, models, fieldMap, method, annos, type); field.addGetter(returns, methodName); continue; } SetterFor setter = method.getAnnotation(SetterFor.class); if (setter != null) { String name = setter.value(); if (name.length() == 0) name = ModelUtil.stripSetter(method.getName()); grabAnnotations(logger, models, fieldMap, method, annos, type); continue; } if (method.getAnnotation(DeleterFor.class) != null) { implementAction(logger, generator, method, models, annos); continue; } // No annotation. We have to guess the type. boolean isVoid = method.getReturnType().isPrimitive() == JPrimitiveType.VOID; boolean isGetter = methodName.startsWith("get") || methodName.startsWith("is") || methodName.startsWith("has"); boolean isSetter, isAction; if (isGetter) { assert !isVoid : "Cannot have a void return type with method name " + methodName + "; getter prefixes get(), is() and has() must return a type."; isSetter = false; isAction = false; } else { isSetter = methodName.startsWith("set") || methodName.startsWith("add") || methodName.startsWith("put") || methodName.startsWith("rem") || methodName.startsWith("remove"); if (isSetter) { isAction = false; } else { isAction = true; } } if (isVoid) { // definitely a setter / action method. if (isSetter) { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementSetter(logger, mb, method, models, fieldMap, annos); } else if (isAction) { implementAction(logger, generator, method, models, annos); } else { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementException(logger, mb, method); } } else { if (isGetter) { String name = ModelUtil.stripGetter(method.getName()); ModelField field = fieldMap.getOrMakeField(name); field.setType(returnType); field.addGetter(returns, methodName); grabAnnotations(logger, models, fieldMap, method, annos, type); } else if (isSetter) { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementSetter(logger, mb, method, models, fieldMap, annos); } else if (isAction) { implementAction(logger, generator, method, models, annos); } else { MethodBuffer mb = generator.createMethod(returnType, methodName, params); implementException(logger, mb, method); } } } generator.generateModel( X_Source.toType(builder.getPackage(), builder.getClassBuffer().getSimpleName()), fieldMap); }
@Before public void clearCaches() { ModelGenerator.clearCaches(); }
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); boolean enableDebug = false; options.addOption( OptionBuilder.withArgName("help") .withLongOpt("help") .hasArg(false) .withDescription("Show help") .create("h")); options.addOption( OptionBuilder.withArgName("class") .withLongOpt("class") .hasArg(true) .withDescription("Model class name (with package)") .isRequired(true) .create("m")); options.addOption( OptionBuilder.withArgName("package") .withLongOpt("package") .hasArg(true) .withDescription("Package for json class") .isRequired(true) .create("p")); options.addOption( OptionBuilder.withArgName("name") .withLongOpt("name") .hasArg(true) .withDescription("Name of generated model") .isRequired(false) .create("n")); options.addOption( OptionBuilder.withArgName("debug") .withLongOpt("debug") .hasArg(false) .withDescription("Enable developer mode") .isRequired(false) .create("d")); try { CommandLineParser parser = new BasicParser(); CommandLine cl = parser.parse(options, args); if (cl.hasOption("h")) { Main.printHelp(options); } enableDebug = cl.hasOption("d"); Class<?> clazz = Class.forName(cl.getOptionValue("m")); String pack = cl.getOptionValue("p"); ModelGenerator gen = new ModelGenerator(clazz, pack, cl.getOptionValue("n")); gen.generate(); } catch (Exception ex) { if (enableDebug) { ex.printStackTrace(); } else { System.err.println(ex.getMessage()); } Main.printHelp(options); } }