private static boolean isInstantiableWithNewOperator (JClassType t) { if (!t.isDefaultInstantiable() || t instanceof JArrayType || t instanceof JEnumType) return false; try { JConstructor constructor = t.getConstructor(new JType[0]); return constructor != null && constructor.isPublic(); } catch (NotFoundException e) { return false; } }
private boolean isVisible (JType type) { if (type == null) return false; if (type instanceof JClassType) { if (type instanceof JArrayType) { JType componentType = ((JArrayType)type).getComponentType(); while (componentType instanceof JArrayType) { componentType = ((JArrayType)componentType).getComponentType(); } if (componentType instanceof JClassType) { return ((JClassType)componentType).isPublic(); } } else { return ((JClassType)type).isPublic(); } } return true; }
public ReflectionCacheSourceCreator (TreeLogger logger, GeneratorContext context, JClassType type) { this.logger = logger; this.context = context; this.type = type; this.packageName = type.getPackage().getName(); this.simpleName = type.getSimpleSourceName() + "Generated"; logger.log(Type.INFO, type.getQualifiedSourceName()); }
for (JAbstractMethod m : methodTypes) { MethodStub stub = new MethodStub(); stub.isPublic = m.isPublic(); stub.enclosingType = getType(c); if (m.isMethod() != null) { stub.isMethod = true; stub.returnType = getType(m.isMethod().getReturnType()); stub.isStatic = m.isMethod().isStatic(); stub.isAbstract = m.isMethod().isAbstract(); stub.isNative = m.isMethod().isAbstract(); stub.isFinal = m.isMethod().isFinal(); } else { if (m.isPrivate() || m.isDefaultAccess()) { logger.log(Type.INFO, "Skipping non-visible constructor for class " + c.getName()); continue; if (m.getEnclosingType().isFinal() && !m.isPublic()) { logger.log(Type.INFO, "Skipping non-public constructor for final class" + c.getName()); continue; stub.name = m.getName(); methodStubs.add(stub); pbn(" new " + methodType + "(\"" + m.getName() + "\", "); pbn(stub.enclosingType + ", "); pbn(stub.returnType + ", "); if (m.getParameters() != null && m.getParameters().length > 0) { pbn("new Parameter[] {");
private String getElementTypes (JField f) { StringBuilder b = new StringBuilder(); JParameterizedType params = f.getType().isParameterized(); if (params != null) { JClassType[] typeArgs = params.getTypeArgs(); b.append("new Class[] {"); for (JClassType typeArg : typeArgs) { if (typeArg.isWildcard() != null) b.append("null"); else if (!isVisible(typeArg)) b.append("null"); else if (typeArg.isClassOrInterface() != null) b.append(typeArg.isClassOrInterface().getQualifiedSourceName()).append(".class"); else if (typeArg.isParameterized() != null) b.append(typeArg.isParameterized().getQualifiedBinaryName()).append(".class"); else b.append("null"); b.append(", "); } b.append("}"); return b.toString(); } return "null"; }
private void createProxy (JClassType type) { ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(type.getPackage().getName(), type.getSimpleSourceName() + "Proxy"); PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); if (printWriter == null) { return; } SourceWriter writer = composer.createSourceWriter(context, printWriter); writer.commit(logger); }
private String getType (JType type) { if (!isVisible(type)) return null; return type.getErasedType().getQualifiedSourceName() + ".class"; }
@Override public String generate (TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); assert (oracle != null); JClassType type = oracle.findType(typeName); if (type == null) { logger.log(ERROR, "Couldn't find type '" + typeName + "'"); throw new UnableToCompleteException(); } if (type.isInterface() == null) { logger.log(ERROR, "Type '" + typeName + "' must be an interface"); throw new UnableToCompleteException(); } ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type); return source.create(); } }
public int compare (JType o1, JType o2) { return o1.getQualifiedSourceName().compareTo(o2.getQualifiedSourceName()); } });
for (JAbstractMethod m : methodTypes) { MethodStub stub = new MethodStub(); stub.isPublic = m.isPublic(); stub.enclosingType = getType(c); if (m.isMethod() != null) { stub.isMethod = true; stub.returnType = getType(m.isMethod().getReturnType()); stub.isStatic = m.isMethod().isStatic(); stub.isAbstract = m.isMethod().isAbstract(); stub.isNative = m.isMethod().isAbstract(); stub.isFinal = m.isMethod().isFinal(); } else { if (m.isPrivate() || m.isDefaultAccess()) { logger.log(Type.INFO, "Skipping non-visible constructor for class " + c.getName()); continue; if (m.getEnclosingType().isFinal() && !m.isPublic()) { logger.log(Type.INFO, "Skipping non-public constructor for final class" + c.getName()); continue; stub.name = m.getName(); methodStubs.add(stub); pbn(" new " + methodType + "(\"" + m.getName() + "\", "); pbn(stub.enclosingType + ", "); pbn(stub.returnType + ", "); if (m.getParameters() != null && m.getParameters().length > 0) { pbn("new Parameter[] {");
public ReflectionCacheSourceCreator (TreeLogger logger, GeneratorContext context, JClassType type) { this.logger = logger; this.context = context; this.type = type; this.packageName = type.getPackage().getName(); this.simpleName = type.getSimpleSourceName() + "Generated"; logger.log(Type.INFO, type.getQualifiedSourceName()); }
private String getElementTypes (JField f) { StringBuilder b = new StringBuilder(); JParameterizedType params = f.getType().isParameterized(); if (params != null) { JClassType[] typeArgs = params.getTypeArgs(); b.append("new Class[] {"); for (JClassType typeArg : typeArgs) { if (typeArg.isWildcard() != null) b.append("null"); else if (!isVisible(typeArg)) b.append("null"); else if (typeArg.isClassOrInterface() != null) b.append(typeArg.isClassOrInterface().getQualifiedSourceName()).append(".class"); else if (typeArg.isParameterized() != null) b.append(typeArg.isParameterized().getQualifiedBinaryName()).append(".class"); else b.append("null"); b.append(", "); } b.append("}"); return b.toString(); } return "null"; }
private void createProxy (JClassType type) { ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(type.getPackage().getName(), type.getSimpleSourceName() + "Proxy"); PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName); if (printWriter == null) { return; } SourceWriter writer = composer.createSourceWriter(context, printWriter); writer.commit(logger); }
private String getType (JType type) { if (!isVisible(type)) return null; return type.getErasedType().getQualifiedSourceName() + ".class"; }
@Override public String generate (TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); assert (oracle != null); JClassType type = oracle.findType(typeName); if (type == null) { logger.log(ERROR, "Couldn't find type '" + typeName + "'"); throw new UnableToCompleteException(); } if (type.isInterface() == null) { logger.log(ERROR, "Type '" + typeName + "' must be an interface"); throw new UnableToCompleteException(); } ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type); return source.create(); } }
public int compare (JType o1, JType o2) { return o1.getQualifiedSourceName().compareTo(o2.getQualifiedSourceName()); } });
private boolean isVisible (JType type) { if (type == null) return false; if (type instanceof JClassType) { if (type instanceof JArrayType) { JType componentType = ((JArrayType)type).getComponentType(); while (componentType instanceof JArrayType) { componentType = ((JArrayType)componentType).getComponentType(); } if (componentType instanceof JClassType) { return ((JClassType)componentType).isPublic(); } } else { return ((JClassType)type).isPublic(); } } return true; }
private static boolean isInstantiableWithNewOperator (JClassType t) { if (!t.isDefaultInstantiable() || t instanceof JArrayType || t instanceof JEnumType) return false; try { JConstructor constructor = t.getConstructor(new JType[0]); return constructor != null && constructor.isPublic(); } catch (NotFoundException e) { return false; } }
private void newArrayC () { p("public Object newArray (Type t, int size) {"); p(" if (t != null) {"); SwitchedCodeBlock pc = new SwitchedCodeBlock("t.id"); for (JType type : types) { if (type.getQualifiedSourceName().equals("void")) continue; if (type.getQualifiedSourceName().endsWith("Void")) continue; String arrayType = type.getErasedType().getQualifiedSourceName() + "[size]"; if (arrayType.contains("[]")) { arrayType = type.getErasedType().getQualifiedSourceName(); arrayType = arrayType.replaceFirst("\\[\\]", "[size]") + "[]"; } pc.add(typeNames2typeIds.get(type.getQualifiedSourceName()), "return new " + arrayType + ";"); } pc.print(); p(" }"); p(" throw new RuntimeException(\"Couldn't create array\");"); p("}"); }
private void newArrayC () { p("public Object newArray (Type t, int size) {"); p(" if (t != null) {"); SwitchedCodeBlock pc = new SwitchedCodeBlock("t.id"); for (JType type : types) { if (type.getQualifiedSourceName().equals("void")) continue; if (type.getQualifiedSourceName().endsWith("Void")) continue; String arrayType = type.getErasedType().getQualifiedSourceName() + "[size]"; if (arrayType.contains("[]")) { arrayType = type.getErasedType().getQualifiedSourceName(); arrayType = arrayType.replaceFirst("\\[\\]", "[size]") + "[]"; } pc.add(typeNames2typeIds.get(type.getQualifiedSourceName()), "return new " + arrayType + ";"); } pc.print(); p(" }"); p(" throw new RuntimeException(\"Couldn't create array\");"); p("}"); }