List of usage examples for org.eclipse.jdt.core.dom PrimitiveType toCode
public static PrimitiveType.Code toCode(String token)
null
if none. From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java
License:Open Source License
/** * Creates a {@link Type} from the given name, owned by the given AST. *//*from ww w . jav a 2s. co m*/ protected Type getTypeFromString(AST ast, String name) { if (name == null) { return ast.newPrimitiveType(PrimitiveType.VOID); } PrimitiveType.Code primitiveTypeCode = PrimitiveType.toCode(name); if (primitiveTypeCode != null) { return ast.newPrimitiveType(primitiveTypeCode); } ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_STATEMENTS); parser.setSource((name + " a;").toCharArray()); Block block = (Block) parser.createAST(null); VariableDeclarationStatement declaration = (VariableDeclarationStatement) block.statements().get(0); return (Type) ASTNode.copySubtree(ast, declaration.getType()); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java
License:Open Source License
/** * Creates a new appropriate {@link Type} from the given <code>value</code>. * <p>//from ww w.j av a2 s. c o m * Note that if <code>value</code> is <code>null</code> returns * {@link PrimitiveType#VOID}. * * @param ast * {@link AST} of the {@link ASTNode ASTElement} needing a new * Type * @param value * the name of the {@link Type} to be created. * @param canBePrimitiveType * if <code>true</code> try to create a primitive from the * given <code>value</code> if possible, otherwise create a new * Type without checking primitives * @author Luiza * * @flowerModelElementId _zVs8hZiOEd6aNMdNFvR5WQ */ public static Type getJavaTypeFromString(AST ast, String value, boolean canBePrimitiveType) { if (canBePrimitiveType) { PrimitiveType.Code primitiveTypeCode = null; if (value == null) primitiveTypeCode = PrimitiveType.VOID; else { primitiveTypeCode = PrimitiveType.toCode(value); } if (primitiveTypeCode != null) return ast.newPrimitiveType(primitiveTypeCode); } // not a primitive ASTParser statementParser = ASTParser.newParser(AST.JLS3); statementParser.setKind(ASTParser.K_STATEMENTS); statementParser.setSource((value + " a;").toCharArray()); // try to parse a variable declaration Block block = (Block) statementParser.createAST(null); VariableDeclarationStatement declaration = (VariableDeclarationStatement) block.statements().get(0); return (Type) ASTNode.copySubtree(ast, declaration.getType()); // detach the type from the parent node }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.JavaNewMethod.java
License:Open Source License
public String generate(Object argument) { final StringBuffer stringBuffer = new StringBuffer(); Operation method = (Operation) argument; if (method.getType() == null) { } else {//from w w w . j a v a 2 s .co m String returnType = method.getType().getName(); PrimitiveType.Code primitiveTypeCode = PrimitiveType.toCode(returnType); if (primitiveTypeCode != null) { if (primitiveTypeCode.equals(PrimitiveType.VOID)) { } else { stringBuffer.append(TEXT_1); } } else { stringBuffer.append(TEXT_2); } } return stringBuffer.toString(); }
From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java
License:Open Source License
/** * Create a new Type for the given type binding. If the binding * represents a parameterized type reference, the returned Type * is a ParameterizedType with the same type parameters. Otherwise * the returned Type is a SimpleType./* ww w . ja v a2 s .co m*/ */ @SuppressWarnings("unchecked") private Type createType(AST ast, ITypeBinding scope, ITypeBinding type) { Type newType; if (type.isArray()) { newType = ast.newArrayType(createType(ast, scope, type.getElementType()), type.getDimensions()); } else if (type.isPrimitive()) { newType = ast.newPrimitiveType(PrimitiveType.toCode(type.getName())); } else if (type.isWildcardType()) { WildcardType wildType = ast.newWildcardType(); ITypeBinding bound = type.getBound(); if (bound != null) { wildType.setBound(createType(ast, scope, bound), type.isUpperbound()); } newType = wildType; } else if (!type.isParameterizedType()) { String name = inScope(type, scope) ? type.getName() : type.getQualifiedName(); newType = ast.newSimpleType(ast.newName(name)); } else { ITypeBinding erasure = type.getErasure(); String name = inScope(type, scope) ? erasure.getName() : erasure.getQualifiedName(); Type rawType = ast.newSimpleType(ast.newName(name)); ParameterizedType paramType = ast.newParameterizedType(rawType); ITypeBinding[] typeArgs = type.getTypeArguments(); for (ITypeBinding param : typeArgs) { paramType.typeArguments().add(createType(ast, scope, param)); } newType = paramType; } generatedTypes.put(newType, type); return newType; }
From source file:com.google.devtools.j2cpp.types.Types.java
License:Open Source License
/** * Returns a Type AST node for a specific type binding. *///from w ww . jav a 2 s . com public static Type makeType(ITypeBinding binding) { Type type; if (binding.isPrimitive()) { PrimitiveType.Code typeCode = PrimitiveType.toCode(binding.getName()); type = instance.ast.newPrimitiveType(typeCode); } else if (binding.isArray() && !(binding instanceof IOSArrayTypeBinding)) { Type componentType = makeType(binding.getComponentType()); type = instance.ast.newArrayType(componentType); } else { String typeName = binding.getErasure().getName(); if (typeName == "") { // Debugging aid for anonymous (no-name) classes. typeName = "$Local$"; } SimpleName name = instance.ast.newSimpleName(typeName); addBinding(name, binding); type = instance.ast.newSimpleType(name); } addBinding(type, binding); return type; }
From source file:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static Type newType(AST ast, ITypeBinding binding) { Type type;/*from w ww . j av a 2s .c om*/ if (binding.isPrimitive()) { type = ast.newPrimitiveType(PrimitiveType.toCode(binding.getName())); } else if (binding.isArray()) { type = ast.newArrayType(newType(ast, binding.getComponentType())); } else if (binding instanceof PointerTypeBinding) { type = newType(ast, ((PointerTypeBinding) binding).getPointeeType()); } else { type = ast.newSimpleType(newSimpleName(ast, binding.getErasure())); } Types.addBinding(type, binding); return type; }
From source file:com.google.gwt.eclipse.core.util.Util.java
License:Open Source License
/** * Sync method has same return type as parameterization of last async * parameter (AsyncCallback). If the async callback parameter type is raw, * just assume sync return type of void. * * @param ast {@link AST} associated with the destination compilation unit * @param asyncMethod the GWT RPC async method declaration * @param imports {@link ImportRewrite} associated with the destination * compilation unit/*from www . j ava 2s .c om*/ * @return the computed return {@link Type} */ public static Type computeSyncReturnType(AST ast, MethodDeclaration asyncMethod, ImportRewrite imports) { Type returnType = ast.newPrimitiveType(PrimitiveType.VOID); @SuppressWarnings("unchecked") List<SingleVariableDeclaration> asyncParameters = asyncMethod.parameters(); // Check for no parameters on async method... just in case if (asyncParameters.isEmpty()) { return returnType; } // Grab the last parameter type, which should be the callback Type callbackType = asyncParameters.get(asyncParameters.size() - 1).getType(); // Make sure we have a parameterized callback type; otherwise, we can't // infer the return type of the sync method. if (callbackType.isParameterizedType()) { ParameterizedType callbackParamType = (ParameterizedType) callbackType; ITypeBinding callbackBinding = callbackParamType.getType().resolveBinding(); if (callbackBinding == null) { return returnType; } // Make sure the callback is of type AsyncCallback String callbackBaseTypeName = callbackBinding.getErasure().getQualifiedName(); if (callbackBaseTypeName.equals(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME)) { @SuppressWarnings("unchecked") List<Type> callbackTypeArgs = callbackParamType.typeArguments(); // Make sure we only have one type argument if (callbackTypeArgs.size() == 1) { Type callbackTypeParameter = callbackTypeArgs.get(0); // Check for primitive wrapper type; if we have one use the actual // primitive for the sync return type. // TODO(): Maybe used linked mode to let the user choose whether to // return the primitive or its wrapper type. String qualifiedName = callbackTypeParameter.resolveBinding().getQualifiedName(); String primitiveTypeName = JavaASTUtils.getPrimitiveTypeName(qualifiedName); if (primitiveTypeName != null) { return ast.newPrimitiveType(PrimitiveType.toCode(primitiveTypeName)); } returnType = JavaASTUtils.normalizeTypeAndAddImport(ast, callbackTypeParameter, imports); } } } return returnType; }
From source file:com.idega.eclipse.ejbwizards.BeanCreator.java
License:Open Source License
protected Type getType(AST ast, String type) { Type returnType = null;//from w ww .j av a 2 s. c o m if (type != null) { boolean isArray = false; boolean isGenericType = false; if (type.indexOf("[") != -1) { isArray = true; } if (type.indexOf("<") != -1) { isGenericType = true; } try { if (isArray) { returnType = ast.newArrayType( ast.newSimpleType(ast.newSimpleName(type.substring(0, type.indexOf("["))))); } else if (isGenericType) { returnType = ast.newParameterizedType( ast.newSimpleType(ast.newSimpleName(type.substring(0, type.indexOf("<"))))); StringTokenizer tokens = new StringTokenizer( type.substring(type.indexOf("<") + 1, type.indexOf(">")), ","); while (tokens.hasMoreTokens()) { ((ParameterizedType) returnType).typeArguments() .add(getType(ast, tokens.nextToken().trim())); } } else { returnType = ast.newSimpleType(ast.newSimpleName(type)); } } catch (IllegalArgumentException iae) { if (isArray) { returnType = ast.newArrayType( ast.newPrimitiveType(PrimitiveType.toCode(type.substring(0, type.indexOf("["))))); } else { returnType = ast.newPrimitiveType(PrimitiveType.toCode(type)); } } } else { returnType = ast.newPrimitiveType(PrimitiveType.VOID); } return returnType; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
private void visitArgumentItem(ASTNode element, String clazzName, String methodName, String parameterTypeName, int position) { /*/*w w w . jav a 2s . c om*/ Object[] ignores = new Object[] { "java.lang.String", "indexOf", new int[] { 0 }, "java.lang.String", "lastIndexOf", new int[] { 0 }, "java.lang.StringBuffer", "append", new int[] { 0 }, "java.lang.StringBuilder", "append", new int[] { 0 } }; */ String typeStr = null; if (element instanceof CastExpression) { CastExpression castExp = (CastExpression) element; Expression exp = castExp.getExpression(); if (exp instanceof NullLiteral) { ITypeBinding nullTypeBinding = castExp.resolveTypeBinding(); if (nullTypeBinding != null) { if (nullTypeBinding.isArray()) { typeStr = "Array"; } else if (nullTypeBinding.isPrimitive()) { Code code = PrimitiveType.toCode(nullTypeBinding.getName()); if (code == PrimitiveType.BOOLEAN) { typeStr = "Boolean"; } else { typeStr = "Number"; } } else if (!nullTypeBinding.isTypeVariable()) { typeStr = assureQualifiedName(shortenQualifiedName(nullTypeBinding.getQualifiedName())); } } } } if (typeStr != null) { buffer.append("Clazz.castNullAs (\""); buffer.append(typeStr.replaceFirst("^\\$wt.", "org.eclipse.swt.")); buffer.append("\")"); } else { Expression exp = (Expression) element; ITypeBinding typeBinding = exp.resolveTypeBinding(); String typeName = null; if (typeBinding != null) { typeName = typeBinding.getName(); } int idx1 = buffer.length(); if ("char".equals(typeName) && !"char".equals(parameterTypeName)) { boolean ignored = false; /* for (int j = 0; j < ignores.length / 3; j++) { int[] indexes = (int[]) ignores[i + i + i + 2]; boolean existed = false; for (int k = 0; k < indexes.length; k++) { if (indexes[k] == i) { existed = true; break; } } if (existed) { if (ignores[0].equals(Bindings.removeBrackets(clazzName)) && ignores[1].equals(methodName)) { ignored = true; break; } } } */ // Keep String#indexOf(int) and String#lastIndexOf(int)'s first char argument ignored = (position == 0 && (/*"append".equals(methodName) || */"indexOf".equals(methodName) || "lastIndexOf".equals(methodName)) && ("java.lang.String".equals(Bindings.removeBrackets(clazzName)))); if (!ignored && exp instanceof CharacterLiteral) { CharacterLiteral cl = (CharacterLiteral) exp; buffer.append(0 + cl.charValue()); ignored = true; } else { boxingNode(element); } if (!ignored) { boolean appendingCode = true; int length = buffer.length(); if (exp instanceof MethodInvocation) { MethodInvocation m = (MethodInvocation) exp; if ("charAt".equals(m.getName().toString())) { int idx2 = buffer.indexOf(".charAt ", idx1); if (idx2 != -1) { StringBuffer newMethodBuffer = new StringBuffer(); newMethodBuffer.append(buffer.substring(idx1, idx2)); newMethodBuffer.append(".charCodeAt "); newMethodBuffer.append(buffer.substring(idx2 + 8, length)); buffer.delete(idx1, length); buffer.append(newMethodBuffer.toString()); appendingCode = false; } } } if (appendingCode) { buffer.append(".charCodeAt (0)"); } } } else { boxingNode(element); } } }
From source file:net.sf.j2s.core.astvisitors.ASTTigerVisitor.java
License:Open Source License
protected void boxingNode(ASTNode element) { if (element instanceof Expression) { Expression exp = (Expression) element; if (exp.resolveBoxing()) { ITypeBinding typeBinding = exp.resolveTypeBinding(); if (typeBinding.isPrimitive()) { String name = typeBinding.getName(); Code type = PrimitiveType.toCode(name); String primitiveTypeName = null; if (type == PrimitiveType.INT) { primitiveTypeName = "Integer"; } else if (type == PrimitiveType.LONG) { primitiveTypeName = "Long"; } else if (type == PrimitiveType.FLOAT) { primitiveTypeName = "Float"; } else if (type == PrimitiveType.DOUBLE) { primitiveTypeName = "Double"; } else if (type == PrimitiveType.BOOLEAN) { primitiveTypeName = "Boolean"; } else if (type == PrimitiveType.BYTE) { primitiveTypeName = "Byte"; } else if (type == PrimitiveType.SHORT) { primitiveTypeName = "Short"; } else if (type == PrimitiveType.CHAR) { primitiveTypeName = "Character"; }/* ww w .jav a 2s . c om*/ if (primitiveTypeName != null) { getBuffer().append("new " + primitiveTypeName + " ("); element.accept(this.visitor); getBuffer().append(")"); return; } } } else if (exp.resolveUnboxing()) { ITypeBinding typeBinding = exp.resolveTypeBinding(); if (!typeBinding.isPrimitive()) { String name = typeBinding.getQualifiedName(); String primitiveName = null; if ("java.lang.Integer".equals(name)) { primitiveName = "int"; } else if ("java.lang.Long".equals(name)) { primitiveName = "long"; } else if ("java.lang.Float".equals(name)) { primitiveName = "float"; } else if ("java.lang.Double".equals(name)) { primitiveName = "double"; } else if ("java.lang.Boolean".equals(name)) { primitiveName = "boolean"; } else if ("java.lang.Byte".equals(name)) { primitiveName = "byte"; } else if ("java.lang.Short".equals(name)) { primitiveName = "short"; } else if ("java.lang.Character".equals(name)) { primitiveName = "char"; } if (primitiveName != null) { getBuffer().append("("); element.accept(this.visitor); getBuffer().append(")." + primitiveName + "Value ()"); return; } } } } element.accept(this.visitor); }